当前位置: 技术问答>linux和unix
shell编程问题 unary operator expected
来源: 互联网 发布时间:2017-02-17
本文导语: 编写一个shell程序,实现如下所示的菜单。 [1] Add Directory [2] Delete Directory [3] Change Directory [4] View Selected Directory [5] Exit 并为每个菜单项编写一个处理函数。用户选择一个有效菜单项之后,程序执行对应的函数,...
编写一个shell程序,实现如下所示的菜单。
[1] Add Directory
[2] Delete Directory
[3] Change Directory
[4] View Selected Directory
[5] Exit
并为每个菜单项编写一个处理函数。用户选择一个有效菜单项之后,程序执行对应的函数,然后询问用户是否想再看一遍菜单。如果用户输入菜单项无效,则显示信息:
Invalid entry, try again.
然后重新显示菜单。
下面是我自己写的代码
//////
#!/bin/bash
#homework1
display()
{
echo "[1]Add Directoty"
echo "[2]Delete Directory"
echo "[3]Change Directory"
echo "[4]View Selected Directory"
echo "[5]Exi"
}
my_add()
{
echo "Enter the directory name(add): "
read dir
mkdir $dir
}
my_del()
{
echo "Enter the directory name(delete): "
read dir
rm -rf $dir
}
my_change()
{
echo "Enter the directory name(change): "
read dir
cd $dir
}
my_view()
{
echo "The selected directory is : "
pwd
}
show_again()
{
echo "Show again?(y/n)"
read show
if [ $show="n" -o $show="N" ]
then return 0
else return 1
fi
}
func()
{
running=1
while [ $running -eq 1 ]
do
display
read choise
case $choise in
1) my_add
show_again
running=$show_again
;;
2) my_del
show_again
running=$show_again
;;
3) my_change
show_again
running=$show_again
;;
4) my_view
show_again
running=$show_again
;;
5) running=0;;
*) echo "Invalid entry,try again"
;;
esac
done
}
func
////////////////////
运行效果:
[1] Add Directory
[2] Delete Directory
[3] Change Directory
[4] View Selected Directory
[5] Exit
1
Enter the directory name(add):
b
Show again?(y/n)
y
./1.sh: line 50: [: -eq: unary operator expected
因为是初学shell, 找了半天,百度了好久还是没有明白,大家帮忙看看 ···谢谢了
[1] Add Directory
[2] Delete Directory
[3] Change Directory
[4] View Selected Directory
[5] Exit
并为每个菜单项编写一个处理函数。用户选择一个有效菜单项之后,程序执行对应的函数,然后询问用户是否想再看一遍菜单。如果用户输入菜单项无效,则显示信息:
Invalid entry, try again.
然后重新显示菜单。
下面是我自己写的代码
//////
#!/bin/bash
#homework1
display()
{
echo "[1]Add Directoty"
echo "[2]Delete Directory"
echo "[3]Change Directory"
echo "[4]View Selected Directory"
echo "[5]Exi"
}
my_add()
{
echo "Enter the directory name(add): "
read dir
mkdir $dir
}
my_del()
{
echo "Enter the directory name(delete): "
read dir
rm -rf $dir
}
my_change()
{
echo "Enter the directory name(change): "
read dir
cd $dir
}
my_view()
{
echo "The selected directory is : "
pwd
}
show_again()
{
echo "Show again?(y/n)"
read show
if [ $show="n" -o $show="N" ]
then return 0
else return 1
fi
}
func()
{
running=1
while [ $running -eq 1 ]
do
display
read choise
case $choise in
1) my_add
show_again
running=$show_again
;;
2) my_del
show_again
running=$show_again
;;
3) my_change
show_again
running=$show_again
;;
4) my_view
show_again
running=$show_again
;;
5) running=0;;
*) echo "Invalid entry,try again"
;;
esac
done
}
func
////////////////////
运行效果:
[1] Add Directory
[2] Delete Directory
[3] Change Directory
[4] View Selected Directory
[5] Exit
1
Enter the directory name(add):
b
Show again?(y/n)
y
./1.sh: line 50: [: -eq: unary operator expected
因为是初学shell, 找了半天,百度了好久还是没有明白,大家帮忙看看 ···谢谢了
|
running=$show_again
这样是把变量show_again的值赋值给变量running,而变量show_again的值为空,那么变量running的值也是空
后续while [ $running -eq 1 ]
判断的时候因为$running为空,所有语法错误
这样是把变量show_again的值赋值给变量running,而变量show_again的值为空,那么变量running的值也是空
后续while [ $running -eq 1 ]
判断的时候因为$running为空,所有语法错误