分页: 1 / 1

初学shell 懵懂

发表于 : 2008-04-07 16:48
muyuyuzhong
#!/bin/sh

salutation="Hello"
echo $salutation
echo "The program $0 is now running"
echo "The second parameter was $2"
echo "The first parameter was $1"
echo "The parameter list was $*"
echo "The user's home directory is $HOME"

echo "Please enter a new greeting"
read salutation

echo $salutation
echo "The script is now complete"

exit 0
我的shell源文件如上

执行时为什么有这样的提示,执行起来也是对的啊,为什么会出现:not found 之类的语句呢? 不明白...

lucky@lucky-desktop:~/linux程序设计/chapter02$ /bin/sh try_var
: not found
Hello
The program try_var is now running
The second parameter was
The first parameter was
The parameter list was
The user's home directory is /home/lucky
: not found:
Please enter a new greeting
Sire
: bad variable namen
: not found:
Hello
The script is now complete
: not found:
exit: 17: Illegal number: 0

发表于 : 2008-04-07 16:51
muyuyuzhong
#!/bin/sh

yes_or_no() {
echo "Is your name $* ?"
while true
do
echo -n "Enter yes or no: "
read x
case "$x" in
y | yes ) return 0;;
n | no ) return 1;;
* ) echo "Answer yes or no"
esac
done
}

echo "Original parameters are $*"

if yes_or_no "$1"
then
echo "Hi $1, nice name"
else
echo "Never mind"

上面的源文件编译后执行得出这样的结果的,为什么?身边没人学这个,自己单挑,真是辛苦


lucky@lucky-desktop:~/linux程序设计/chapter02$ chmod +x my_name
lucky@lucky-desktop:~/linux程序设计/chapter02$ ./my_name
bash: ./my_name:/bin/sh^M:损坏的解释器: No such file or directory
lucky@lucky-desktop:~/linux程序设计/chapter02$

发表于 : 2008-04-07 16:51
yaoms
你的shell脚本里肯定有 ^M 字符。

发表于 : 2008-04-07 20:27
percy
测试了一下第一个,没问题啊

代码: 全选

 ./hello.sh 
Hello
The program ./hello.sh is now running
The second parameter was 
The first parameter was 
The parameter list was 
The user's home directory is /home/pjq
Please enter a new greeting
hello
hello
The script is now complete

发表于 : 2008-04-07 20:30
BigSnake.NET
可能是在Windows下编辑的
行结束符就有了^M

发表于 : 2008-04-07 20:31
percy
测试了第二个,在最后加上fi
再测试没问题


代码: 全选

./hello2.sh pjq
Original parameters are pjq
Is your name pjq ?
Enter yes or no: yes
Hi pjq, nice name

发表于 : 2008-04-08 14:01
muyuyuzhong
yaoms 写了:你的shell脚本里肯定有 ^M 字符。
我用GEDIT打开过后在VIM里才看到^M,怪了.把^M删掉后就一切正常了.谢谢了!

发表于 : 2008-04-13 15:16
yang119345
dos2unix处理下

发表于 : 2008-05-08 11:07
Jianying
我也出现了楼主说的第一个问题。

用gedit打开之后没看到^M,关了之后在vi里也没看到.

我是直接在Ubuntu7.04里弄得。

ps:这个是<Beginning linux programming>书上的例子吧,hehe,我也是独学.遇到问题有时候挺郁闷。
muyuyuzhong 写了:
yaoms 写了:你的shell脚本里肯定有 ^M 字符。
我用GEDIT打开过后在VIM里才看到^M,怪了.把^M删掉后就一切正常了.谢谢了!

发表于 : 2008-05-08 11:14
Jianying
刚刚找到原因了,第一行应该是
#!/bin/sh
我把它写成
#!bin/sh
了,漏了一个/
寒...

Jianying 写了:我也出现了楼主说的第一个问题。

用gedit打开之后没看到^M,关了之后在vi里也没看到.

我是直接在Ubuntu7.04里弄得。

ps:这个是<Beginning linux programming>书上的例子吧,hehe,我也是独学.遇到问题有时候挺郁闷。
muyuyuzhong 写了:
yaoms 写了:你的shell脚本里肯定有 ^M 字符。
我用GEDIT打开过后在VIM里才看到^M,怪了.把^M删掉后就一切正常了.谢谢了!