我正在尝试制作一个bash脚本,该脚本回声一个菜单,用户可以从中归档、提取和更新文件。我一直在遇到语法错误,但是在我的case条件语句中,我最近才开始使用bash regex和其他linux工具,所以我在语法上有点混乱,有人能为这个例子的case结构*中的条件语句指出正确的语法吗?以下是我目前掌握的一些代码:
#!/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 Menun"
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
或者这个(现在then
与if
在同一行,由;
分隔):
if [[ "$dest" == null ]] ; then
tar -cvf $path
tar -tvf $path
exit 0
fi