“if if”在“case esac”中

时间:2013-10-20 08:41:38

标签: bash if-statement case

我尝试使用以下代码:

read choice
case $choice in 
"1")    less /var/log/messages
    if [$? -ne 0]; then 
        less /var/log/syslog
    fi
    ;;
etc
etc
etc
*) echo "Please enter a choice between 1 and 20";;
esac

执行./myScript.sh时我得到了这个:

./myScript.sh: line 4: [1: command not found

我找不到问题!

2 个答案:

答案 0 :(得分:7)

if语句中,在[之后和]之前添加空格,为

if [ $? -ne 0 ]; then 

答案 1 :(得分:1)

问题是(4,8)和(4,16)。在(4,7)和(4,8)之间放置一个空格,在(4,15)和(4,16)之间放置另一个空格。这些有序对就像(行,列)。

在第4行上这样做:

    if [ $? -ne 0 ]; then
相关问题