我正在編輯網路安裝點腳本檔案。當變數被組裝時,它包含訊息,但是當它被卸載時,它顯示為空,並且如果腳本要求組裝,則變數的內容不會更新。
VARIABLE=$(df -h | awk '{print $1}'| grep //user@IP/path/user)
卸載時:
echo $VARIABLE
空,如果我使用命令安裝:
open smb://user:passwd@IP/path/user
並鍵入df -h | awk '{print $1}' | grep //user@IP/path/user
似乎//user@IP/path/user
但如果我給出檢查命令回顯$VARIABLE
似乎是空的
有人可以幫我嗎?
答案1
謝謝!我真的知道問題是變數是動態的。我設法產生另一個變數呼叫CHECK,我的程式碼並不漂亮,我希望你回來檢查它是否已安裝,以減少程式碼行數。遵循我的程式碼 shell 腳本
#!/bin/sh
#Thunderbird Backup Script by VPN in MAC OS X
#Variables description
SOURCE=/Users/username/Library/Thunderbird/Profiles/tsg0o7yk.default-release
TARGET=/Volumes/username
MOUNTED=$(df -h | awk '{print $1}' | grep //[email protected]/BackupThunderbird/username)
PATHDST='//[email protected]/BackupThunderbird/username'
SUCCESS='echo "YOUR BACKUP THUNDERBIRD WAS MADE!"'
ERROR='echo "COULD NOT PERFORM THUNDERBIRD BACKUP, CHECK LAN/WAN CONNECTIVITY AND AVALIABILITY OF THE UNIT STORAGE REMOTE STATUS"'
DATE=`date +%d/%m/%Y-%H-%M-%S`
#Connection test, if to fail will connect TunnelBlick
ping -c2 192.168.0.123 > /dev/null
if [ $? -eq 0 ];
then
echo "Connection OK time wait 30s for disconnect mount for test!"
else
echo "Disconected Tunnelblick, wait to connect..."
osascript -e "tell application \"/Applications/Tunnelblick.app\"" -e "connect \"QNAP-MYVPN\"" -e "end tell" #call OpenVPN Tunnelblick MAC OS
sleep 30
fi
backup () {
echo "Please don't disconnect or unmount 192.168.0.123, running proccess backup..."
rsync -rltpDhu $SOURCE/abook.* $TARGET 2> $TARGET/abook.log
rsync -rltpDhu $SOURCE/history.* $TARGET 2> $TARGET/history.log
rsync -rltpDhu --stats --delete-after $SOURCE/ImapMail/email-ssl.com.br $TARGET | mail -s "Report Thunderbird Backup" myemail@provider
sleep 30
$SUCCESS | mail -s "$DATE - VPN Thunderbird Backup" username@localhost -c myemail@provider
diskutil unmount /Volumes/username
}
no_backup () {
echo "BACKUP COULD NOT BE PERFORMED, CONTACT YOUR ADMINISTRATOR"
$ERROR | mail -s "$DATE - ERROR VPN Thunderbird Backup" username@localhost
}
mount_function () {
open smb://username:[email protected]/BackupThunderbird/username # user remote unit storage
sleep 60
}
mounted () {
echo $MOUNTED
if [ $MOUNTED ]
then
echo "Mounted Unit"
backup # call backup function
exit
elif [ !$MOUNTED ]
then
echo "Unmounted Unit, mounting..."
mount_function # call mount function
fi
CHECK=$(df -h | awk '{print $1}' | grep $PATHDST)
echo $CHECK
if [ $CHECK ]
then
backup
echo "Script Verification when successfully"
else
no_backup
echo "Script verification no successfully"
fi
}
mounted
# Tunnelblick disconnecting
echo "Tunnelblick connection disconnecting"
osascript -e "tell application \"/Applications/Tunnelblick.app\"" -e "disconnect \"QNAP-MYVPN\"" -e "end tell"