带有case语句的bash脚本

时间:2017-07-18 09:03:24

标签: bash shell

我有一个名为Namebook的文件,其中包含以下数据:

$ cat Namebook
Kamala Hasan  123
Rajini kanth  345
Vijay         567
Ajith kumar   908
$

然后我有一个bash脚本在文件Namebook中添加一个新名称,名为add_name.sh

$ cat add_name.sh 
#!/bin/bash

echo "$1   $2"  >> Namebook
$

接下来,我有一个脚本可以查看来自此Namebook的人look_up.sh

$ cat look_up.sh
#!/bin/bash

grep "$1" Namebook
$

然后我又有一个脚本可以从此Namebook删除某个人,名为remove.sh

$ cat remove.sh
#!/bin/bash

grep -v "$1" Namebook > tmp/Namebook    
mv tmp/Namebook Namebook
$

这些脚本在Namebook文件中添加,查找和删除用户。

基于这三个脚本的组合,我创建了一个脚本all_action.sh来执行所有上述操作

$cat all_action.sh
#!/bin/bash

echo 'Select the option
      1. Lookup some one from the Namebook
      2. Add name to Namebook
      3. Remove name from the Namebook

Select the options range of (1-3): \c '

read choice

case "$choice"
in
   1) "Enter the name to lookup: \c"
       read name
       look_up "$name" ;;

   2) "Enter the name to be add: \c"
       read name
      "Enter the number to be add: \c"
       read number
       add_name "$name" "$number" ;;

   3)  "Enter the name to be remove: \c"     
        read name
        remove "$name ;;
esac

我的问题:当我执行程序all_action.sh时,它会抛出错误

例如:我将运行./all_action.sh

Select the option
          1. Lookup some one from the Namebook
          2. Add name to Namebook
          3. Remove name from the Namebook

     Select the options range of (1-3): \c
1
Enter name to be lookup
Kamala Hasa
./all_action.sh: line no : look_up: command not found

你能帮忙解决一下吗?

1 个答案:

答案 0 :(得分:1)

找不到命令:look_up,add_name,remove。将完整路径放在脚本中并指定Interpreter:

sh /home/myuser/myscript.sh

或相对路径:

sh ./myscript.sh