Bash 显示菜单的Shell脚本
我的Shell脚本中有一些问题, 我已经写了一个很短的例子,但是我没有得到预期的结果, 我在case语句中使用了if条件,但有一些失败。。 我的问题是: 当我按2键时,用户应该首先给出搜索文件的路径,如果没有给出路径,那么我将按照我提到的默认路径查找, 但是我不能在这里得到这个函数,有人能在这方面帮助我吗?请 我将感谢您的帮助: 我的代码是:Bash 显示菜单的Shell脚本,bash,shell,menu,choice,Bash,Shell,Menu,Choice,我的Shell脚本中有一些问题, 我已经写了一个很短的例子,但是我没有得到预期的结果, 我在case语句中使用了if条件,但有一些失败。。 我的问题是: 当我按2键时,用户应该首先给出搜索文件的路径,如果没有给出路径,那么我将按照我提到的默认路径查找, 但是我不能在这里得到这个函数,有人能在这方面帮助我吗?请 我将感谢您的帮助: 我的代码是: #!/bin/bash trap '' 2 while true do clear echo -e " \t *********************
#!/bin/bash
trap '' 2
while true
do
clear
echo -e " \t *******************************************************************"
echo -e " \t ******************** TEST-MENU ********************************"
echo -e "\t *******************************************************************"
echo -e "\n"
echo -e "\t\t 1)Show Date/Time"
echo -e "\t\t 2)File-Search"
echo -e "\t\t e)End \n"
echo -e "\t\t Select your choice:\c" ; read answer
echo -e "\t*******************************************************************"
case $answer in
1)date +'%y%m%d %H:%M:%S' ;;
2)echo -e "Please give your dir:\c" ; read directory
if [ "$directory" = "" ] then
$directory = "[/test/sample/]" fi
echo -e "Enter your file [$directory]:\c" ; read search
find "$directory" -name "*$search*" -type f -print|xargs ls -l ;;
e) exit ;;
esac
echo -e "Enter return to continue \c"
read answer
done
两个问题:
变量赋值
$directory=[/test/sample/]fi
赋值时不使用$,所以应该使用like
目录=[/test/sample/]fi
您的默认目录
$directory=[/test/sample/]fi
为什么需要方括号?你在做ls[/test/sample]吗?把它拿走
你的如果
如果[$directory=]
如果用户只需按enter键,则它将不起作用,因此您应按如下方式操作:
如果[X$directory=X]
您可以组合ls并找到如下内容:
查找$directory-name$search-type f-exec ls-l{}\
下面是一个工作示例,其中添加了一些格式编辑和注释。不过,您需要对搜索文件名进行一些null测试,就好像它不存在一样,它会显示当前目录中的文件:
#!/bin/bash
trap '' 2
# Set up the default value. If you ever want to change it, just do it here.
# -r means make it read-only so this makes it a CONSTANT.
declare -r DEFAULT=1
while true
do
clear
echo -e " \t *******************************************************************"
echo -e " \t ************************ TEST-MENU ********************************"
echo -e "\t *******************************************************************"
echo -e "\n"
echo -e "\t\t 1)Show Date/Time"
echo -e "\t\t 2)File-Search"
echo -e "\t\t e)End \n"
# Typically in a prompt like this
# when there is a value in square brackets that means if you just
# press enter you will get that value as the default.
echo -e "\t\t Select your choice [$DEFAULT]: \c" ; read answer
echo -e "\t*******************************************************************"
# If nothing was selected, use the default.
# -z tests for null.
if [[ -z "$answer" ]]
then answer=$DEFAULT
fi
case $answer in
1) date +'%y%m%d %H:%M:%S'
;;
2) echo -e "Please give your dir: \c"
read directory
if [[ -z "$directory" ]] # Test for null
then directory="/test/sample/"
fi
echo -e "Enter your file [$directory]: \c"
read search
find "$directory" -name "*$search*" -type f -print|xargs ls -l
;;
e) exit
;;
# Always expect the unexpected! The * is the default for a case
# statement when no match is found.
*) echo -e "[$answer] is an invalid selection"
;;
esac
echo -e "Enter return to continue \c"
read answer
done
如果您不坚持要精确显示菜单,请查看select命令。它已经提供了一种显示选项菜单、选择一个项目以及对该选项执行操作的方法。