Bash:否则在case语句中的if/else语句不工作

Bash:否则在case语句中的if/else语句不工作

问题描述:

我想检查用户是否在命令行中使用case和if/else语句键入多个参数。有什么不对的是,我一直得到默认情况下,而不是相同的命令,但有2多个参数。例如,我的条件之一是:Bash:否则在case语句中的if/else语句不工作

del) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: removes a file" 
    else 
    echo "using Bash command: rm $2 $3" 
    rm $2 $3 
    echo done 
    fi 

打印的首要条件,但如果我输入,也就是说,德尔AAA BBB,我得到默认情况下,它是:

echo "ERROR: Unrecognized command" 

我如果有帮助,也可以使用它读取用户的输入。

read -p "wcl> " -r wcl $2 $3 

我真的不知道是否有解决这个不杀我所有的代码,并从头开始一个更好的办法。 这是全码:

#!/bin/bash 
#use read command 

echo Welcome to the Windows Command Line simulator! 
echo Enter your commands below 
while true 
do 
read -p "wcl> " -r wcl $2 $3 
    case $wcl in 
    dir) 
    echo "using Bash command: ls $2 $3" 
    ls 
    continue 
      ;; 
    copy) 
    FILE="$2" 
    if [ "$#" -ne 3 ] 
    then 
     echo "Usage: copy sourcefile destinationfile" 
    else 
    echo "using Bash command: cp $2 $3" 
    if [ -f "$FILE" ] 
    then 
    cp $2 $3 
    else 
    echo "cannot stat $FILE: No such file or directory">&2 
    fi 
    echo done 
    fi 
    continue 
      ;; 
    del) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: removes a file" 
    else 
    echo "using Bash command: rm $2 $3" 
    rm $2 $3 
    echo done 
    fi 
    continue 
      ;; 
    move) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: moves a file to another file name and location" 
    else 
    echo "using Bash command: mv $2 $3" 
    mv $2 $3 
    echo done 
    fi 
    continue 
      ;; 
    rename) 
    if [ -z "$2" ] || [ -z "$3" ] 
    then 
      echo "Usage: renames a file" 
    else 
    echo "using Bash command: mv $2 $3" 
    mv $2 $3 
    echo done 
    fi 
    continue 
      ;; 
    ipconfig) 
      ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1 
    continue 
      ;; 
     exit) 
      echo "Goodbye" 
      exit 1 
      ;; 
    ^c) 
      echo "Goodbye" 
      exit 1 
      ;; 
*) 
      echo "ERROR: Unrecognized command" 
    continue 
esac 
done 
+0

检查命令行参数个数的最好方法是测试'$#',例如'if(($# cdarke

+0

你需要显示你的代码的其余部分,特别是'case .. in'部分。 – SiKing

您不能使用read设置位置参数,虽然目前尚不清楚为什么你会需要在这里。只需使用常规参数。

while true 
do 
    read -p "wcl> " -r wcl arg1 arg2 
    case $wcl in 
    dir) 
    echo "using Bash command: ls $arg1 $arg2" 
    ls "$arg1" "$arg2" 
    continue 
      ;; 

    # ... 
    esac 
done 

执行方式read -r wcl $2 $3$2$3首先膨胀,得到的是read将用来设置变量的名称。如果没有设置,那么该命令将减少到read -r wcl,因此整个命令行分配给变量wcl,而不仅仅是命令。

但是,read本身不会执行与shell已经执行的相同的分析,如果您的目标是编写自己的shell。

+0

我试过,现在它只是打印第一个条件,而不是其他条件,即“回声”用法:...“而不是”使用...“ – anon

+0

这是因为'$#'不受'read “所有的事情都需要你重新思考。 – chepner

如果你真的在使用bash,你可以通过数组插入你读到的位置参数。 (你也可以只将它们留在阵中,但指的是位置参数的语法比较简单。)

# -a: read the successive words into an array 
read -r -p "wcl> " -a params 
# set the positional parameters to the expansion of the array 
set -- "${params[@]}" 
wcl=$1 # Or you could do the case on "$1" 

这也将设置$#单词读的数量,设置的副作用位置参数。

由于@chepner points outsread是有问题的:它只是将输入到空格分开的话,不尊重引号,反斜杠,以及其它shell元字符,你可能要实现。在bash中对命令行进行完整的bash风格解析本身就是一项相当困难的工作。