我想编写自己的脚本以舒适地使用 tar(并且在 shell 方面也变得更有经验),我的脚本落在了:
cmd=$1
tarname=$2
files=${*:3}
echo_green(){
echo -e "\033[1;32m$@\033[0m"
}
echo_blue(){
echo -e "\033[1;34m$@\033[0m"
}
create(){
echo_blue 'create'
case $tarname in
*.tar) tar -cvf $tarname $files; echo_green $tarname created;;
*.tar.gz) tar -cvzf $tarname $files; echo $tarname created;;
*.tar.bz2) tar -cvjf $tarname $files; echo_green $tarname created;;
esac
}
show(){
_cmd='tar -tv'
case $tarname in
*.tar) _cmd+='f' ;;
*.tar.gz) _cmd+='zf' ;;
*.tar.bz2) _cmd+='jf' ;;
esac
_cmd_all=$_cmd" $tarname | awk '{print \$6}' | cut -d'/' -f 1"
echo $_cmd_all
content="$($_cmd_all)"
exit
IFS='
'
_cmd_folder=$_cmd" $tarname | head -1 | awk '{print \$6}' | cut -d'/' -f 1"
echo $_cmd_folder
folder="$($_cmd_folder)"
all_in_one=1
for l in $content; do
if [ ! $l = $folder ]; then
all_in_one=0;
break;
fi
done
echo $all_in_one
}
case $cmd in
"create") create;;
"show") show;;
esac
错误:
$ bash bash_lesson.sh show 4.tar
tar -tvf 4.tar | awk '{print $6}' | cut -d'/' -f 1
tar: You may not specify more than one `-Acdtrux' or `--test-label' option
Try `tar --help' or `tar --usage' for more information.
但如果我只是在控制台中运行有问题的字符串,一切都很好:
$ tar -tvf 4.tar | awk '{print $6}' | cut -d'/' -f 1
test_dir
bash_lesson