Bash

更改 bash shell 腳本語言?

  • February 13, 2019

幾年前我寫了一個呼叫 rsync 的多部分腳本,它一直有效。經過一年沒有使用和大量 debian apt-get 更新,現在腳本停止工作。

Main2Network1.sh 呼叫 Main2Network2 有第 3 和第 4 部分,但腳本 Main2Network1.sh 第 27 行:

Main2Network1.sh: [[ not found 

第 27 行是

if [[ -z $VAR ]];

當我編寫腳本時,腳本

$$ [ $$] 是 if then 工作所必需的,只需按下輸入鍵即可,即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

以下對我有用,如果點擊“just enter”或任意數量的空格或幾個製表符,則返回 true。任何字母數字等都返回 false。

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

FWIW我會更改提示以使其更清楚“只需按Enter鍵即可執行備份,任何字元都可以退出”

你的問題是在第一行Main2Network1.sh

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

看到一開始的那個雜散空間了嗎?這會將您的shebang行變成另一個註釋,因此核心預設使用 執行您的腳本/bin/sh,這不理解該[[ ]]構造。

刪除那個空間,一切都會好起來的。

引用自:https://unix.stackexchange.com/questions/500299