bash shell 脚本语言发生变化?

bash shell 脚本语言发生变化?

几年前我编写了一个调用 rsync 的多部分脚本,并且它一直有效。经过一年的未使用和大量 debian apt-get 更新后,现在该脚本停止工作了。

Main2Network1.sh 调用 Main2Network2,有第 3 和 4 部分,但脚本 Main2Network1.sh 第 27 行包含:

 Main2Network1.sh: [[ not found 

27号线是

if [[ -z $VAR ]];

当我编写脚本时,脚本 [[ ]] 对于 if then 来说是必需的,只需按 Enter 键即可工作,即https://stackoverflow.com/a/20201920/9383998

也许有人可以告诉我他们是否在我的代码中第 27 行附近看到了最近不推荐使用的语法(这就是我运行脚本时遇到错误的地方)谢谢大家

Main2Network1.sh

 #!/bin/bash
#renames files changed files on NETWORK to _day_mnth_yr_NETWORK1 then puts maincomp version of file on network.
#THIS SHOULD BE PUT TO AUTOMATICALLY RUN AFTER TRAVEL2NETWORK SYNC
#info=NAME1 lists just changed Files/Dirs, -b must preceed --suffix,
# this will change existing dup file on travel to date_TRAVEL replacing base file with network version
#SOURCE File Dups replace network and network copy renamed to _NETWORK1
echo "source MAIN dir?"
read SOURCE
SO=$(echo ${SOURCE}/ | tr -d "[='=]")
echo "destination NETWORK dir?"
read DEST
DE=$(echo ${DEST}/ | tr -d "[='=]")
echo "exclude file?"
read EXCLUDE
EX=$(echo ${EXCLUDE} | tr -d "[='=]")
echo "=========================================="
echo "Exclude is $EXCLUDE"
echo "rsync destination is $DE"
echo "rsync Source is $SO"
echo "rsync exclude is $EX"
echo "=========================================="
mkdir "${0%/*}"/`eval date +"%d"_%m_%Y`log/
LOG=$(cd `dirname $0` && pwd)/`eval date +"%d"_%m_%Y`log/
# ~/ issue
rsync -aEv --info=NAME1 -c -n --numeric-ids -z -b --suffix=_%d%m%Y_%H%M_NETWORK1 --exclude-from=${EX} ${SO} ${DE} > $LOG/Main2NetworkStep1LOG.txt

echo 'check output if acceptable continue?'
read VAR
if [[ -z $VAR ]];
then
     echo "backing up Main To Network"
     echo ==================================================
     source /`dirname $0`/Main2Network2.sh
     echo 'Main System backed up'
else
    echo "Ok"
    exit
fi

Main2Network2.sh

#!/bin/bash
DAY=`eval date +"%d"`
MONTH=`eval date +"%m"`
YEAR=`eval date +"%Y"`
echo 'beginning REAL Main to network backup'
echo ================================================================
rsync -aEv --info=NAME1 -c --numeric-ids -z -b --suffix=_${MONTH}_${DAY}_${YEAR}_NETWORK1 --exclude-from=${EX}  ${SO} ${DE} > ${LOG}/Main2NetworkRealBackupStep2Log.txt
echo 'all done Main to Network sync'
leafpad ${LOG}/Main2NetworkRealBackupStep2Log.txt
echo 'begin Network to Main backup process?'
echo ========================================
read VAR
if [[ -z $VAR ]];
then
     echo "Generating Network to Main Backup Dry Run Report"
     source /`dirname $0`/Main2Network3.sh
else
    echo "Ok"
    exit
fi

答案1

您的问题就在第一行Main2Network1.sh

 #!/bin/bash
#renames files changed files on NETWORK to _day_mnth_yr_NETWORK1 then puts maincomp version of file on network.

看到开头的那个杂散空间了吗?这将改变你的舍邦行到另一个注释中,因此内核默认使用 运行您的脚本/bin/sh,它不理解该[[ ]]构造。

删除该空间,一切都会好起来。

答案2

以下对我有用,如果点击“just Enter”,或者任意数量的空格,或者几个选项卡,则返回 true。任何字母数字等都会返回 false。

if [ -z $VAR ]
then
 ..... do your backup stuff here
else
  "ok, not backing up"
fi

FWIW我会更改提示以使其更清楚“只需按回车键即可运行备份,任何字符即可退出”

相关内容