小弟初学SHELL脚本的编写,遇到declare错误

sh/bash/dash/ksh/zsh等Shell脚本
回复
windlycn
帖子: 9
注册时间: 2007-07-16 19:56
来自: 福州福建

小弟初学SHELL脚本的编写,遇到declare错误

#1

帖子 windlycn » 2007-09-09 21:21

希望各位高手帮我看看。
只是一段非常 简单的 Script

#!/bin/bash
#Using for and loop

declare -i s #

for (( i=1; i<=100; i=i+1 ))
do
s=s+i;
done
echo "The count is => $s"

这是一段非常 简单的 计算 1+2+3+……100的小程序,在执行后提示出错
test-11-loop.sh: 5: declare: not found
test-11-loop.sh: 7: Syntax error: Bad for loop variable

对于 declare的错误我始终不明白,在SHELL状态下输入没有错误,在脚本里就是一运行 就出错,
请各位大人帮着看看吧,谢谢了!
lb_bn
帖子: 1261
注册时间: 2007-02-25 16:56

#2

帖子 lb_bn » 2007-09-09 22:17

这个问题怪怪的。。。 :D
头像
magicsky
帖子: 225
注册时间: 2007-03-30 9:46

#3

帖子 magicsky » 2007-09-09 22:45

试了下你的脚本,在我的机器上是好好的,得到结果The count is => 5050
那个也许是你的bash的问题,
实在不行的话重新安装一遍bash试试吧,最好是重新从源代码编译一遍
在国外,众多程序员为了理想而夜以继日;
而在国内,程序员在为填饱自己和家人的肚子而埋头苦干。
abf
帖子: 8
注册时间: 2006-01-20 16:28

#4

帖子 abf » 2007-10-04 17:10

估计是你用这种方式运行的: sh test-11-loop.sh
试试: chmod +x test-11-loop.sh
./test-11-loop.sh
或者: bash test-11-loop.sh
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#5

帖子 eexpress » 2007-10-04 18:57

declare从来不知道作什么的。
● 鸣学
wkt
帖子: 849
注册时间: 2006-09-07 22:51
联系:

#6

帖子 wkt » 2007-10-04 19:07

for还可以这样用啊??
改天试试!!!
xiexbb
帖子: 11
注册时间: 2007-09-09 10:41

#7

帖子 xiexbb » 2007-10-06 23:20

我的没有问题啊:

代码: 全选

-bash-2.05b$ bash test
The count is => 5050
-bash-2.05b$ 
ashuaipyyz
帖子: 5
注册时间: 2008-03-22 19:51

#8

帖子 ashuaipyyz » 2008-07-19 10:33

abf 写了:估计是你用这种方式运行的: sh test-11-loop.sh
试试: chmod +x test-11-loop.sh
./test-11-loop.sh
或者: bash test-11-loop.sh
能解试一下这是为什么吗?
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

#9

帖子 xiooli » 2008-07-19 10:48

ashuaipyyz 写了:
abf 写了:估计是你用这种方式运行的: sh test-11-loop.sh
试试: chmod +x test-11-loop.sh
./test-11-loop.sh
或者: bash test-11-loop.sh
能解试一下这是为什么吗?
sh是链接的dash不是bash。
回复