我可以為 shell 腳本建立設定檔嗎?

我可以為 shell 腳本建立設定檔嗎?

是否可以使用設定檔建​​立.shshell 或檔案?.command

我需要透過終端備份網站,並能夠將一些資料放入配置文件,並讓 shell 執行它。

例如,使用者名稱、密碼、ssh id 等。

#!/bin/bash
ssh [email protected] "tar cjvf webfilesbackup-date-`date +%Y%m%d`.tar.bz2 public_html/"    
ssh [email protected] "mysqldump -u user_admin -ppass database_1 > databasebackup-`date +%Y%m%d`-db.sql"
scp [email protected]:webfilesbackup-date-`date +%Y%m%d`.tar.bz2 ~/backup/
scp [email protected]:databasebackup-`date +%Y%m%d`-db.sql ~/backup/
ssh [email protected] "rm -f webfilesbackup-date-`date +%Y%m%d`.tar.bz2"
ssh [email protected] "rm -f databasebackup-`date +%Y%m%d`-db.sql"

答案1

settings使用您的設定作為變數建立一個文件(例如):

username='foo'
pass='bar'
website='baz'
database='yak'

然後從腳本中獲取文件,您將能夠存取變數:

. settings
echo $username     # <- this will print "foo"

關於你的第二個“問題”,如果你有多個主機,你可以只擁有多個配置文件,並獲取作為參數傳遞給腳本的文件。例如,建立一個名為site1.

呼叫你的腳本./backup.sh site1

. $1               # <- this will load the file "site1"
echo $username     # <- this will print "foo"

對於您的備份腳本,這意味著:

#!/bin/bash
. $1
ssh [email protected] "tar cjvf webfilesbackup-date-`date +%Y%m%d`.tar.bz2 public_html/"
ssh [email protected] "mysqldump -u $user -p$pass $database > databasebackup-`date +%Y%m%d`-db.sql"
# ... and so on

答案2

另一個選項是建立一個文字檔案並使用 while 循環逐行存取該檔案。

答案3

mkdir -p Example.app/Contents/MacOS`
cp script.sh Example.app/Contents/MacOS/Example

……那會起作用的。 本文有更詳細的說明,包括如何取得自訂圖示。

您可能也會將 Automator 或 AppleScript Studio 視為建置可以正常運作的應用程式的方法,而無需手動執行。

答案4

     # parse the ini like $0.hostname.conf and set the variables
     # place a $0.`hostname -s`.conf file in the same dir as your script
     # set here any VarName=VarValue
     # use ; for comments - you will get host independant conf file 
     # for your bash scripts - [examle usage :][1] 
     doParseIniFile(){
        eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
            -e 's/;.*$//' \
            -e 's/[[:space:]]*$//' \
            -e 's/^[[:space:]]*//' \
            -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
           < $IniFile \
            | sed -n -e "/^\[$IniSection\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
     }
     #eof function doParseIniFile

相關內容