初学shell 懵懂
-
- 帖子: 5
- 注册时间: 2007-10-10 15:57
初学shell 懵懂
#!/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
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
-
- 帖子: 5
- 注册时间: 2007-10-10 15:57
#!/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$
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$
- yaoms
- 帖子: 4952
- 注册时间: 2007-10-19 14:51
- 来自: 深圳
- percy
- 帖子: 508
- 注册时间: 2006-09-10 8:19
- 系统: Gentoo/Mac OS X
- 来自: Shanghai,China
- 联系:
测试了一下第一个,没问题啊
代码: 全选
./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
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
-
- 帖子: 5
- 注册时间: 2007-10-10 15:57
- yang119345
- 帖子: 570
- 注册时间: 2005-06-19 14:22
- 来自: 上海
- 联系:
- Jianying
- 帖子: 70
- 注册时间: 2007-05-06 23:35
- 联系:
我也出现了楼主说的第一个问题。
用gedit打开之后没看到^M,关了之后在vi里也没看到.
我是直接在Ubuntu7.04里弄得。
ps:这个是<Beginning linux programming>书上的例子吧,hehe,我也是独学.遇到问题有时候挺郁闷。
用gedit打开之后没看到^M,关了之后在vi里也没看到.
我是直接在Ubuntu7.04里弄得。
ps:这个是<Beginning linux programming>书上的例子吧,hehe,我也是独学.遇到问题有时候挺郁闷。
muyuyuzhong 写了:我用GEDIT打开过后在VIM里才看到^M,怪了.把^M删掉后就一切正常了.谢谢了!yaoms 写了:你的shell脚本里肯定有 ^M 字符。
CPU AMD Athlon 64 X2 3600+
主板 升技AN52
内存 DDR2 667 1GB
显卡 小影霸 Nvidia GeForce 7300 GT 256M
硬盘 ST380021A 80GB
OS Ubuntu 7.04
#I believe I can fly!
主板 升技AN52
内存 DDR2 667 1GB
显卡 小影霸 Nvidia GeForce 7300 GT 256M
硬盘 ST380021A 80GB
OS Ubuntu 7.04
#I believe I can fly!
- Jianying
- 帖子: 70
- 注册时间: 2007-05-06 23:35
- 联系:
刚刚找到原因了,第一行应该是
#!/bin/sh
我把它写成
#!bin/sh
了,漏了一个/
寒...
#!/bin/sh
我把它写成
#!bin/sh
了,漏了一个/
寒...
Jianying 写了:我也出现了楼主说的第一个问题。
用gedit打开之后没看到^M,关了之后在vi里也没看到.
我是直接在Ubuntu7.04里弄得。
ps:这个是<Beginning linux programming>书上的例子吧,hehe,我也是独学.遇到问题有时候挺郁闷。
muyuyuzhong 写了:我用GEDIT打开过后在VIM里才看到^M,怪了.把^M删掉后就一切正常了.谢谢了!yaoms 写了:你的shell脚本里肯定有 ^M 字符。
CPU AMD Athlon 64 X2 3600+
主板 升技AN52
内存 DDR2 667 1GB
显卡 小影霸 Nvidia GeForce 7300 GT 256M
硬盘 ST380021A 80GB
OS Ubuntu 7.04
#I believe I can fly!
主板 升技AN52
内存 DDR2 667 1GB
显卡 小影霸 Nvidia GeForce 7300 GT 256M
硬盘 ST380021A 80GB
OS Ubuntu 7.04
#I believe I can fly!