更新变量内容

更新变量内容

我正在编辑网络安装点脚本文件。当变量被组装时,它包含信息,但是当它被卸载时,它显示为空,并且如果脚本要求组装,则变量的内容不会更新。

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"

相关内容