运行脚本出错,请好心人看看

嘤鸣求友 2013-08-19 03:30:53
给脚本get_perform设置权限后运行失败,不知道为什么
Administrator@AS-201210251145 ~/labw/nsp132
$ chmod +x get_perform

Administrator@AS-201210251145 ~/labw/nsp132
$ ./get_perform
./get_perform: line 10: syntax error near unexpected token `elif'
'/get_perform: line 10: `elif ["$i" -le 250 ]; then

get_perform内容为:
————————————————————————————————————————————————————————————————————
#!/bin/sh
i=1
while [ "$i" -le 300 ]; do
ns w_w.tcl $i
gawk –f delay.awk rate=$i wired_wireless.tr >>delay
gawk –f loss.awk rate=$i wired_wireless.tr >>loss_rate
gawk -f throughtput.awk rate=$i wired_wireless.tr >>throughtput
if ["$i" -le 225 ]; then
i=$(($i+20))
elif ["$i" -le 250 ]; then 【此处为第十行,代码中无此括号内容】
i=$(($i+5))
else
i=$(($i+2))
fi
done
exit 0

————————————————————————————————————————————————————————————————————
...全文
259 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayer 2013-08-19
  • 打赏
  • 举报
回复
引用 6 楼 ljc007 的回复:
方括号的左右两边都需要空格
正解!+1
pix77 2013-08-19
  • 打赏
  • 举报
回复
引用 15 楼 weiran2009 的回复:
[quote=引用 14 楼 mp777323 的回复:] [quote=引用 13 楼 weiran2009 的回复:] [quote=引用 6 楼 ljc007 的回复:] 方括号的左右两边都需要空格
前辈,我尝试了,错误一样,不过若是方括号2边都要空格,那为什么第三行while语句没有报错?[/quote] 也要的 #!/bin/bash i=1 while ["$i" -le 300 ]; do echo $i if [ "$i" -le 225 ]; then i=$(($i+20)) elif [ "$i" -le 250 ]; then i=$(($i+5)) else i=$(($i+2)) fi done # bash a.sh a.sh: line 3: [1: command not found # 因为[也是一个命令, 既然是命令,那么接收参数的格式, 就是[ 空格 参数1 空格 参数2 [/quote] w我的就没有提示,并且版本高,不工作也不行啊,看来cygwin不管用,只能装虚拟机了%……[/quote] 嗯,装个吧
嘤鸣求友 2013-08-19
  • 打赏
  • 举报
回复
引用 14 楼 mp777323 的回复:
[quote=引用 13 楼 weiran2009 的回复:] [quote=引用 6 楼 ljc007 的回复:] 方括号的左右两边都需要空格
前辈,我尝试了,错误一样,不过若是方括号2边都要空格,那为什么第三行while语句没有报错?[/quote] 也要的 #!/bin/bash i=1 while ["$i" -le 300 ]; do echo $i if [ "$i" -le 225 ]; then i=$(($i+20)) elif [ "$i" -le 250 ]; then i=$(($i+5)) else i=$(($i+2)) fi done # bash a.sh a.sh: line 3: [1: command not found # 因为[也是一个命令, 既然是命令,那么接收参数的格式, 就是[ 空格 参数1 空格 参数2 [/quote] w我的就没有提示,并且版本高,不工作也不行啊,看来cygwin不管用,只能装虚拟机了%……
pix77 2013-08-19
  • 打赏
  • 举报
回复
引用 13 楼 weiran2009 的回复:
[quote=引用 6 楼 ljc007 的回复:] 方括号的左右两边都需要空格
前辈,我尝试了,错误一样,不过若是方括号2边都要空格,那为什么第三行while语句没有报错?[/quote] 也要的 #!/bin/bash i=1 while ["$i" -le 300 ]; do echo $i if [ "$i" -le 225 ]; then i=$(($i+20)) elif [ "$i" -le 250 ]; then i=$(($i+5)) else i=$(($i+2)) fi done # bash a.sh a.sh: line 3: [1: command not found # 因为[也是一个命令, 既然是命令,那么接收参数的格式, 就是[ 空格 参数1 空格 参数2
嘤鸣求友 2013-08-19
  • 打赏
  • 举报
回复
引用 6 楼 ljc007 的回复:
方括号的左右两边都需要空格
前辈,我尝试了,错误一样,不过若是方括号2边都要空格,那为什么第三行while语句没有报错?
pix77 2013-08-19
  • 打赏
  • 举报
回复
引用 11 楼 weiran2009 的回复:
[quote=引用 10 楼 mp777323 的回复:] sh也是软连接过去的 # ll /bin/sh lrwxrwxrwx. 1 root root 4 Jul 8 18:46 /bin/sh -> bash # 如果不行,一般都是版本问题, 我这儿的bash版本是, # bash --version bash --version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" # [quote=引用 9 楼 weiran2009 的回复:] [quote=引用 8 楼 mp777323 的回复:] 我写了主要代码,你看看
#!/bin/bash
i=1
while [ "$i" -le 300 ]; do
        echo $i
        if [ "$i" -le 225 ]; then
                i=$(($i+20))
        elif [ "$i" -le 250 ]; then
                i=$(($i+5))
        else
                i=$(($i+2))
        fi
done
奇怪奇怪,基本上一模一样的,只是我用cywin做ns2仿真,是不是这个不同啊? 因为我们第一行代码不一样的缘故?[/quote][/quote] 这有些麻烦了,我的: Administrator@AS-201210251145 ~/labw/nsp132 $ bash --version GNU bash, version 4.1.10(4)-release (i686-pc-cygwin) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. [/quote] 你的版本还比我高呢
嘤鸣求友 2013-08-19
  • 打赏
  • 举报
回复
引用 10 楼 mp777323 的回复:
sh也是软连接过去的 # ll /bin/sh lrwxrwxrwx. 1 root root 4 Jul 8 18:46 /bin/sh -> bash # 如果不行,一般都是版本问题, 我这儿的bash版本是, # bash --version bash --version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" # [quote=引用 9 楼 weiran2009 的回复:] [quote=引用 8 楼 mp777323 的回复:] 我写了主要代码,你看看
#!/bin/bash
i=1
while [ "$i" -le 300 ]; do
        echo $i
        if [ "$i" -le 225 ]; then
                i=$(($i+20))
        elif [ "$i" -le 250 ]; then
                i=$(($i+5))
        else
                i=$(($i+2))
        fi
done
奇怪奇怪,基本上一模一样的,只是我用cywin做ns2仿真,是不是这个不同啊? 因为我们第一行代码不一样的缘故?[/quote][/quote] 这有些麻烦了,我的: Administrator@AS-201210251145 ~/labw/nsp132 $ bash --version GNU bash, version 4.1.10(4)-release (i686-pc-cygwin) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
pix77 2013-08-19
  • 打赏
  • 举报
回复
sh也是软连接过去的 # ll /bin/sh lrwxrwxrwx. 1 root root 4 Jul 8 18:46 /bin/sh -> bash # 如果不行,一般都是版本问题, 我这儿的bash版本是, # bash --version bash --version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" #
引用 9 楼 weiran2009 的回复:
[quote=引用 8 楼 mp777323 的回复:] 我写了主要代码,你看看
#!/bin/bash
i=1
while [ "$i" -le 300 ]; do
        echo $i
        if [ "$i" -le 225 ]; then
                i=$(($i+20))
        elif [ "$i" -le 250 ]; then
                i=$(($i+5))
        else
                i=$(($i+2))
        fi
done
奇怪奇怪,基本上一模一样的,只是我用cywin做ns2仿真,是不是这个不同啊? 因为我们第一行代码不一样的缘故?[/quote]
嘤鸣求友 2013-08-19
  • 打赏
  • 举报
回复
引用 8 楼 mp777323 的回复:
我写了主要代码,你看看
#!/bin/bash
i=1
while [ "$i" -le 300 ]; do
        echo $i
        if [ "$i" -le 225 ]; then
                i=$(($i+20))
        elif [ "$i" -le 250 ]; then
                i=$(($i+5))
        else
                i=$(($i+2))
        fi
done
奇怪奇怪,基本上一模一样的,只是我用cywin做ns2仿真,是不是这个不同啊? 因为我们第一行代码不一样的缘故?
pix77 2013-08-19
  • 打赏
  • 举报
回复
我写了主要代码,你看看
#!/bin/bash
i=1
while [ "$i" -le 300 ]; do
        echo $i
        if [ "$i" -le 225 ]; then
                i=$(($i+20))
        elif [ "$i" -le 250 ]; then
                i=$(($i+5))
        else
                i=$(($i+2))
        fi
done
嘤鸣求友 2013-08-19
  • 打赏
  • 举报
回复
引用 4 楼 mp777323 的回复:
[quote=引用 3 楼 weiran2009 的回复:] [quote=引用 2 楼 mp777323 的回复:] 注意空格 #!/bin/sh i=1 while [ "$i" -le 300 ]; do ns w_w.tcl $i gawk –f delay.awk rate=$i wired_wireless.tr >>delay gawk –f loss.awk rate=$i wired_wireless.tr >>loss_rate gawk -f throughtput.awk rate=$i wired_wireless.tr >>throughtput if [ "$i" -le 225 ]; then i=$(($i+20)) elif [ "$i" -le 250 ]; then 【此处为第十行,代码中无此括号内容】 i=$(($i+5)) else i=$(($i+2)) fi done exit 0
哪儿空格错了?我没看出来,我直接把阁下的代码粘贴进去还是同样的错误~[/quote] "$i"的左边需要一个空格[/quote] 左边加空格一样报错: Administrator@AS-201210251145 ~/labw/nsp132 $ ./get_perform ./get_perform: line 10: syntax error near unexpected token `elif' '/get_perform: line 10: `elif [ "$i" -le 250 ]; then
ljc007 2013-08-19
  • 打赏
  • 举报
回复
方括号的左右两边都需要空格
空的 2013-08-19
  • 打赏
  • 举报
回复
test [空格"$i" ]
pix77 2013-08-19
  • 打赏
  • 举报
回复
引用 3 楼 weiran2009 的回复:
[quote=引用 2 楼 mp777323 的回复:] 注意空格 #!/bin/sh i=1 while [ "$i" -le 300 ]; do ns w_w.tcl $i gawk –f delay.awk rate=$i wired_wireless.tr >>delay gawk –f loss.awk rate=$i wired_wireless.tr >>loss_rate gawk -f throughtput.awk rate=$i wired_wireless.tr >>throughtput if [ "$i" -le 225 ]; then i=$(($i+20)) elif [ "$i" -le 250 ]; then 【此处为第十行,代码中无此括号内容】 i=$(($i+5)) else i=$(($i+2)) fi done exit 0
哪儿空格错了?我没看出来,我直接把阁下的代码粘贴进去还是同样的错误~[/quote] "$i"的左边需要一个空格
嘤鸣求友 2013-08-19
  • 打赏
  • 举报
回复
引用 2 楼 mp777323 的回复:
注意空格 #!/bin/sh i=1 while [ "$i" -le 300 ]; do ns w_w.tcl $i gawk –f delay.awk rate=$i wired_wireless.tr >>delay gawk –f loss.awk rate=$i wired_wireless.tr >>loss_rate gawk -f throughtput.awk rate=$i wired_wireless.tr >>throughtput if [ "$i" -le 225 ]; then i=$(($i+20)) elif [ "$i" -le 250 ]; then 【此处为第十行,代码中无此括号内容】 i=$(($i+5)) else i=$(($i+2)) fi done exit 0
哪儿空格错了?我没看出来,我直接把阁下的代码粘贴进去还是同样的错误~
pix77 2013-08-19
  • 打赏
  • 举报
回复
注意空格 #!/bin/sh i=1 while [ "$i" -le 300 ]; do ns w_w.tcl $i gawk –f delay.awk rate=$i wired_wireless.tr >>delay gawk –f loss.awk rate=$i wired_wireless.tr >>loss_rate gawk -f throughtput.awk rate=$i wired_wireless.tr >>throughtput if [ "$i" -le 225 ]; then i=$(($i+20)) elif [ "$i" -le 250 ]; then 【此处为第十行,代码中无此括号内容】 i=$(($i+5)) else i=$(($i+2)) fi done exit 0
pix77 2013-08-19
  • 打赏
  • 举报
回复
#!/bin/sh
i=1
while [ "$i" -le 300 ]; do
ns w_w.tcl $i
gawk –f delay.awk rate=$i wired_wireless.tr >>delay
gawk –f loss.awk rate=$i wired_wireless.tr >>loss_rate
gawk -f throughtput.awk rate=$i wired_wireless.tr >>throughtput
if [ "$i" -le 225 ]; then
i=$(($i+20))
elif [ "$i" -le 250 ]; then  【此处为第十行,代码中无此括号内容】
i=$(($i+5))
else
i=$(($i+2))
fi
done
exit 0

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧