tar菜单的Bash脚本,case条件中的语法错误
我正在尝试制作一个bash脚本,它响应一个菜单,用户可以从中归档、提取和更新文件。我经常遇到语法错误,但是在我的case条件中,我最近才开始使用bash regex和其他linux工具,所以我在语法上有点混乱,有人能指出在这个例子中,*case结构*中使用条件的正确语法吗? 以下是我目前掌握的一些代码:tar菜单的Bash脚本,case条件中的语法错误,bash,Bash,我正在尝试制作一个bash脚本,它响应一个菜单,用户可以从中归档、提取和更新文件。我经常遇到语法错误,但是在我的case条件中,我最近才开始使用bash regex和其他linux工具,所以我在语法上有点混乱,有人能指出在这个例子中,*case结构*中使用条件的正确语法吗? 以下是我目前掌握的一些代码: #!/bin/bash echo "Today's date is $(date)" echo "The current directory $(pwd)" ec
#!/bin/bash
echo "Today's date is $(date)"
echo "The current directory $(pwd)"
echo -e "A full list of contents follows: $(ls -R)\n"
echo -e "\n Archive Menu\n"
echo " a. Archive a file or directory."
echo " b. Extract a file or directory from an archive."
echo " c. Update archive of current directory based on timestamp."
echo -e " d. Exit.\n"
read -p "Enter a, b, c, or d: " answer
echo
#
case "$answer" in
a)
read -p "Please enter full path to the file you wish to archive: " path
read -p "Please enter destination for the archived file (leave empty for current drectory)" dest
if [[ "$dest" == null]] ;
then
tar -cvf $path
tar -tvf $path
exit 0
fi;
if [[ "dest" == ""]];
then
tar -cvf $path $dest
tar -tvf $path
exit 0
fi;
;;
b)
read -p "Please enter full path to the file you wish to extract: " path
read -p "Please enter destination for extracted file (leave empty for current directory)" dest
if [ "$dest" == null]
then
tar -xvf $path
tar -tvf $path
exit 0
fi
if [ "$dest" == ""]
then
tar -xvf $path $dest
tar -tvf $path
exit 0
fi
;;
c)
esac
我收到的一些错误消息如下:
metar.sh:line 18: syntax error in conditional expression: unexpected token';'
metar.sh:line 18: syntax error near ';'
metar.sh:line 18 ' if[[ "$dest == null]] ;'
在“if”语句的右括号前添加空格,即
if [[ "$x" == "$y" ]]
您错过了
]]
前面的空格,它应该是这样的:
if [[ "$dest" == null ]]
then
tar -cvf $path
tar -tvf $path
exit 0
fi
或者这个(现在有然后和在同一行,如果,用分隔;
):
您能编辑您的问题以显示您收到的错误消息吗?这将使我们能够准确地指出您的问题,而不必费力地完成整个脚本。
if [[ "$dest" == null ]] ; then
tar -cvf $path
tar -tvf $path
exit 0
fi