tar菜单的Bash脚本,大小写条件下的语法错误

时间:2013-12-11 17:22:19

标签: bash

我正在尝试制作一个bash脚本,它回显用户可以存档,提取和更新文件的菜单。我不断得到语法错误但是在我的情况下,我最近才开始使用bash正则表达式和其他linux工具,所以我在语法上有点混乱,有人能指出正确的语法用于* 条件>此示例的案例结构 * ? 这是我到目前为止的一些代码:

    #!/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]] ;'

2 个答案:

答案 0 :(得分:1)

在“if”语句的结束括号前添加一个空格,即

if [[ "$x" == "$y" ]]

答案 1 :(得分:0)

你错过了]]之前的空格,它应该是这样的:

if [[ "$dest" == null  ]]
then 
    tar -cvf $path
    tar -tvf $path
    exit 0
fi

或者这个(现在thenif在同一行 - 由;分隔):

if [[ "$dest" == null  ]] ; then 
    tar -cvf $path
    tar -tvf $path
    exit 0
fi