ディレクトリのコンテンツを.tar
ファイルにバックアップするためのBashスクリプトを書いています。スクリプトをもっと整理し、移植性があり、共有可能なものに更新するつもりです。しかし、私の人生はコード内のバグを理解することができません。その場合、出力は$OUTDIR
を$INDIR
と同じパスに変更すべきではありません。ディレクトリをバックアップするためのbashスクリプトによる奇妙な出力
-d
がfileコマンドのパラメータに追加されると、スクリプトの実際の要点であるtar
コマンドで使用される文字列が表示され、独自のパラメータが定義されます。私はコードのその部分を2回書き直しました。なぜ出力が毎回違うのか理解できません。
#!/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
これは、単一引用符がthaの変数を展開することができていない出力
[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
それは働いた。どうもありがとうございました –