分页: 1 / 1

shell 程序错,

发表于 : 2008-05-30 16:50
luckyone
最近学习编写shell 脚本,发现了些问题;
问题 1,>

#!/bin/sh
echo "What is your favourite OS?"
select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
    break
done
echo "You have selected $var"

保存为 11.sh ,并将 11.sh 改为可执行文件
运行:
...................................................................
wyb@wyb-desktop:~/桌面$ chmod +x 11.sh
wyb@wyb-desktop:~/桌面$ ./11.sh
What is your favourite OS?
./11.sh: 3: select: not found
./11.sh: 4:     break: not found
./11.sh: 5: Syntax error: "done" unexpected
.........................................................................
请问这是为什么????


还有另一个程序也运行不成功,请问程序那里有错误???
...........................................................................................
#!/bin/sh

#备份目录函数
backup_dir()
{
dir_test
echo "backing ...."
tar xzvf /tmp/back.tar.gz $DIRECTOPY

}

#恢复目录函数
restore_dir()
{
dir_test
echo "正在恢复"
tar xzvf /tmp/backup.tar.gz
}

#验证目录函数
dir_test()
{
#输入要备份的目录名
echo -e "please intput the directory name of backup file :\c"
#度目录名
read DIRECTORY

#判断是否是目录
if [!-d &DIRECTORY]
then
echo "sorry ,&DIRECTORY is not a directory"
exit 1
fi
cd $DIRECTORY
}

clear
#ANS = y
while [&ANS = Y -o &ANS = y]

do
echo "======================================================="
echo "= backup-Restore Menu = "
echo "-------------------------------------------------------"
echo " 1:Backup Directory "
echo " 2:restore Directory "
echo " 0: Exit "
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "please input a choice (0-2): \c"

read CHOICE
case "$CHOICE" in

1) backup_dir ;;
2) restore_dir ;;
0) exit 1 ;;
*) echo "Invalid choice!"
exit 1 ;;
esac

if [$? -ne 0]
then
echo "Program encounter error!"
exit 2
else
echo "Operate sucessfully !"
fi

echo -e "would you like to continue ? y/Y to continue ,any other key to exit :\c"

read ANS

clear
done

...................................................................
运行时,提示:
[: 74: missing ]
./myshell.sh: 74: ANS: not found
./myshell.sh: 74: ANS: not found

请指点一下《《《,谢谢!!!!

发表于 : 2008-05-30 17:22
xiooli
应该是默认shell的问题,把第一行的改成#!/bin/bash试试。
ps:最好明确指明解析的shell,不要用sh这样的,因为不同的系统可能默认的shell不同。

发表于 : 2008-05-31 14:04
neuchen
第一个 用bash就可以

第二个看看我修改的吧
#!/bin/bash
backup_dir()
{
dir_test
echo "backing..."
tar -zcvf /tmp/back.tar.gz $DIRECTORY
}

restore_dir()
{
dir_test
echo "restoring.."
tar -zxvf /tmp/back.tar.gz
}

dir_test()
{
echo -e "please input the directory name of backup file"
read DIRECTORY

if [ ! -d $DIRECTORY ]; then
echo "sorry,$DIRECTORY is not a directory"
exit 1
fi
cd $DIRECTORY
}

clear

ANS=y
while [ $ANS = y -o $ANS = Y ];
do
echo "======================================"
echo "= backup - restore Menu ="
echo "--------------------------------------"
echo "1:baskup Directory"
echo "2:restore Directory"
echo "0:Exit"
echo "++++++++++++++++++++++++++++++++++++++"
echo -e "please input achoice(0-2):"

read choice
case $choice in
1)
backup_dir
;;
2)
restore_dir
;;
0)
exit 1
;;
*)
echo "Invalid choice!"
exit 1
;;
esac
if [ $? -ne 0 ]; then
echo "error"
else
echo "success"
fi

echo -e "y to continue, other to exit"


read ANS
clear
done

发表于 : 2008-06-06 12:06
luckyone
谢谢!!