Bash Shell 脚本 - Select 案例菜单不工作
Bash Shell Scripting - Select Case Menu Not Working
我对 shell 编程还很陌生.. 正在做项目
正在使用 Select Case
执行 Select 菜单
下面是脚本
#! /usr/bin/bash
echo "1) Add new book"
echo "2) Remove existing book info"
echo "3) Update book info and quantity"
echo "4) Search for book by title/author"
echo "5) Process a book sold"
echo "6) Inventory summary report"
echo "7) Quit"
PS3="Please enter your option: "
select book in Add Remove update search process inventory quit
#read book
do
case $book in
1) echo "Add new book"
echo "Title: "
read title
echo $title > BookDB.txt
echo "Author: "
read name
echo $name > BookDB.txt
echo "$title successfully added!"
break
;;
2) echo "Remove existing book info"
sed '$NAME' BookDB.txt
break
;;
3) echo "Update book info and quantity"
echo "Title: "
read title
echo $title < BookDB.txt
echo "Author: "
read name
echo $name < BookDB.txt
break
;;
4) echo "Search for book by title/author"
break
;;
5) echo "Process a book sold"
break
;;
6) echo "Inventory summary report"
break
;;
7) echo "Quit"
exit
;;
esac
done
及以下是 Ubuntu terminal
中的 shell commands
1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
1) Add 3) update 5) process 7) quit
2) Remove 4) search 6) inventory
Please enter your option: 1
Please enter your option:
在我输入1.and后,echo
中的文字不会出现,即使我选择退出,也不会返回到菜单。我如何让它工作? :(
非常感谢任何帮助。谢谢! :)
试试这个:
PS3="Please enter your option: "
select book in Add Remove Update Search Process Inventory Quit
do
case $book in
"Add")
...
;;
"Remove")
...
;;
"Update")
...
;;
etc...
esac
done
替换
select book in Add Remove update search process inventory quit
与:
select book in 1 2 3 4 5 6 7
来自 bash
联机帮助页(我的斜体和粗体):
select name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below).
The PS3 prompt is then displayed and a line read from the standard input.
If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word.
If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes.
Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed.
The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.
因此,在您的 case
声明中,您需要 "Add"
而不是 1
,其他人也一样。
您还应该调查每种情况下您实际在做什么。显然,后者是空的,因为您还没有找到它们,但是在第二种情况下使用 sed
不会从文件中删除任何内容。并且,对于第三种情况:
echo $title < BookDB.txt
在...有趣。如果它是 output 重定向,我可以理解计划的开始,但是你所拥有的那一行只会输出 $title
并完全忽略输入重定向的内容。
毫无疑问,您会及时修复它们,我只是想将它们作为需要查看的内容提请您注意。
在select语句下,如果你应该使用select中声明的字符串。在这里你应该这样做:
select book in Add Remove update search process inventory quit
do
case $book in
Add) echo "Add new book"
echo "Title: "
read title
echo $title > BookDB.txt
echo "Author: "
read name
echo $name > BookDB.txt
echo "$title successfully added!"
break
;;
Remove) echo "Remove existing book info"
sed '$NAME' BookDB.txt
break
;;
update) echo "Update book info and quantity"
echo "Title: "
read title
echo $title < BookDB.txt
echo "Author: "
read name
echo $name < BookDB.txt
break
;;
search) echo "Search for book by title/author"
break
;;
process) echo "Process a book sold"
break
;;
inventory) echo "Inventory summary report"
break
;;
quit) echo "Quit"
exit
;;
esac
done
您可以理解如下代码:
select book 准备了很多值,赋值是根据我们输入的内容。输入应该是从 1 开始的整数索引。
为了进一步理解,你应该学习很多关于bash和select系统调用。
我对 shell 编程还很陌生.. 正在做项目
正在使用 Select Case
下面是脚本
#! /usr/bin/bash
echo "1) Add new book"
echo "2) Remove existing book info"
echo "3) Update book info and quantity"
echo "4) Search for book by title/author"
echo "5) Process a book sold"
echo "6) Inventory summary report"
echo "7) Quit"
PS3="Please enter your option: "
select book in Add Remove update search process inventory quit
#read book
do
case $book in
1) echo "Add new book"
echo "Title: "
read title
echo $title > BookDB.txt
echo "Author: "
read name
echo $name > BookDB.txt
echo "$title successfully added!"
break
;;
2) echo "Remove existing book info"
sed '$NAME' BookDB.txt
break
;;
3) echo "Update book info and quantity"
echo "Title: "
read title
echo $title < BookDB.txt
echo "Author: "
read name
echo $name < BookDB.txt
break
;;
4) echo "Search for book by title/author"
break
;;
5) echo "Process a book sold"
break
;;
6) echo "Inventory summary report"
break
;;
7) echo "Quit"
exit
;;
esac
done
及以下是 Ubuntu terminal
shell commands
1) Add new book
2) Remove existing book info
3) Update book info and quantity
4) Search for book by title/author
5) Process a book sold
6) Inventory summary report
7) Quit
1) Add 3) update 5) process 7) quit
2) Remove 4) search 6) inventory
Please enter your option: 1
Please enter your option:
在我输入1.and后,echo
中的文字不会出现,即使我选择退出,也不会返回到菜单。我如何让它工作? :(
非常感谢任何帮助。谢谢! :)
试试这个:
PS3="Please enter your option: "
select book in Add Remove Update Search Process Inventory Quit
do
case $book in
"Add")
...
;;
"Remove")
...
;;
"Update")
...
;;
etc...
esac
done
替换
select book in Add Remove update search process inventory quit
与:
select book in 1 2 3 4 5 6 7
来自 bash
联机帮助页(我的斜体和粗体):
select name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below).
The PS3 prompt is then displayed and a line read from the standard input.
If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word.
If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes.
Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed.
The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.
因此,在您的 case
声明中,您需要 "Add"
而不是 1
,其他人也一样。
您还应该调查每种情况下您实际在做什么。显然,后者是空的,因为您还没有找到它们,但是在第二种情况下使用 sed
不会从文件中删除任何内容。并且,对于第三种情况:
echo $title < BookDB.txt
在...有趣。如果它是 output 重定向,我可以理解计划的开始,但是你所拥有的那一行只会输出 $title
并完全忽略输入重定向的内容。
毫无疑问,您会及时修复它们,我只是想将它们作为需要查看的内容提请您注意。
在select语句下,如果你应该使用select中声明的字符串。在这里你应该这样做:
select book in Add Remove update search process inventory quit
do
case $book in
Add) echo "Add new book"
echo "Title: "
read title
echo $title > BookDB.txt
echo "Author: "
read name
echo $name > BookDB.txt
echo "$title successfully added!"
break
;;
Remove) echo "Remove existing book info"
sed '$NAME' BookDB.txt
break
;;
update) echo "Update book info and quantity"
echo "Title: "
read title
echo $title < BookDB.txt
echo "Author: "
read name
echo $name < BookDB.txt
break
;;
search) echo "Search for book by title/author"
break
;;
process) echo "Process a book sold"
break
;;
inventory) echo "Inventory summary report"
break
;;
quit) echo "Quit"
exit
;;
esac
done
您可以理解如下代码:
select book 准备了很多值,赋值是根据我们输入的内容。输入应该是从 1 开始的整数索引。
为了进一步理解,你应该学习很多关于bash和select系统调用。