我編寫了一個Bash腳本,用於將目錄的內容備份到.tar
文件中。我打算將腳本更新爲更具組織性,可移植性和可共享的內容。然而,我不能在我的生活中理解我的代碼中的錯誤,在這種情況下,輸出不應將$OUTDIR
更改爲與$INDIR
相同的路徑。使用bash腳本備份目錄的奇怪輸出
只要將-d
附加到文件命令的參數(它應該顯示腳本的實際要點中使用的字符串tar
命令)來定義它自己的參數,就會發生這種情況。我已經將我的代碼的一部分重寫了兩次,我不明白爲什麼輸出每次都會有所不同。
#!/bin/bash
# Script will only back up an accessible directories
# Any directories that require 'sudo' will not work.
if [[ -z $1 ]]; then # This specifically
INDIR=$(pwd); debug=false; # is the part where
elif [[ $1 == '-d' ]]; then # the 'debug' variable
debug=true; # is tampering with
if [[ -z $2 ]]; then INDIR=$(pwd); # the output somehow.
else INDIR=$2; fi
else
INDIR=$1; debug=false; fi
if [[ -z $2 ]]; then
OUTDIR=$INDIR
elif [[ '$2' = '$INDIR' ]]; then
if [[ -z $3 ]]; then OUTDIR=$INDIR;
else OUTDIR=$3; fi
else
OUTDIR=$2; fi
FILENAME=bak_$(date +%Y%m%d_%H%M).tar
FILEPATH=$OUTDIR'/'$FILENAME
LOGPATH=$OUTDIR'/bak.log'
if [[ "$debug" = true ]]; then
echo "Input directory: "$INDIR
echo "Output directory: "$OUTDIR
echo "Output file path: "$FILEPATH
echo "Log file path: "$LOGPATH
else
tar --exclude=$FILEPATH --exclude=$LOGPATH \
-zcvf $FILEPATH $INDIR > $LOGPATH
fi
這是輸出
[email protected]:~$ bakdir -d
Input directory: /home/gnomop
Output directory: /home/gnomop
Output file path: /home/gnomop/bak_20170804_2123.tar
Log file path: /home/gnomop/bak.log
[email protected]:~$ bakdir -d /home/other
Input directory: /home/other
Output directory: /home/other
Output file path: /home/other/bak_20170804_2124.tar
Log file path: /home/other/bak.log
[email protected]:~$ bakdir -d /home/other /home/other/bak
Input directory: /home/other
Output directory: /home/other
Output file path: /home/other/bak_20170804_2124.tar
Log file path: /home/other/bak.log
它工作。非常感謝你 –