What's wrong with this shell script program?

为当前最终发行版之前的所有版本提供支持
回复
windvalley
帖子: 4
注册时间: 2010-11-05 15:48
送出感谢: 0
接收感谢: 0

What's wrong with this shell script program?

#1

帖子 windvalley » 2010-11-05 15:56

Hello everyone. I'm a new user of Linux. I tried to write my first shell script program on my computer. But it couldn't work rightly.

The code is

#!/bin/bash
# Program:
# Try do calculate 1+2+...+$(your_input)
# History:
# 2010/11/05 windvalley First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "Please input a number, I'll count for 1+2+...+your_input:" nu

s=0
for (( i=1 ; i <= $nu ; i=i+1 ))
do
s=$( ($s+$i) )
done
echo "The result is" $s



When I ran it, it would show: line 14: 0+1: command not found

..........................


I don't know how to fix it.

Thanks in advance!
windvalley
帖子: 4
注册时间: 2010-11-05 15:48
送出感谢: 0
接收感谢: 0

Re: What's wrong with this shell script program?

#2

帖子 windvalley » 2010-11-05 16:01

Sorry. It seems I posted it in a wrong place. :em06
头像
trigger
帖子: 1585
注册时间: 2006-10-25 18:08
送出感谢: 0
接收感谢: 0

Re: What's wrong with this shell script program?

#3

帖子 trigger » 2010-11-05 16:01

shell is not C
you can not use i=i+1 in shell
楼主真是一派胡言,真可谓:“两个黄鹂鸣翠柳,不知所云;一行白鹭上青天,不知所止“。本来不想和你辩论,今天气愤不过,和你理论一番。我国宪法写得清清楚楚:“一夜夫妻百日恩,七楼以上才有电梯”。这个想必你知道,既然知道,你就不能断章取义,就算是天气预报,它还有不准的时候呢!!!再者说了,那中国银行也不是你一家开的。人家马拉多纳都结婚了,你还拿着粮票顶什么用呢。真是滑天下之大稽。前些日子,全国人大刚刚开过会,郑重声明:“中国不搞多party制,存栏母猪给补贴”。多好的事呢,楞让你这号人给搅混了。
fuxiu
帖子: 208
注册时间: 2009-12-01 20:02
送出感谢: 0
接收感谢: 0

Re: What's wrong with this shell script program?

#4

帖子 fuxiu » 2010-11-05 16:03

( ($s+$i) )
my english is poor. :em03
头像
kingkongmok
帖子: 338
注册时间: 2006-03-23 14:48
送出感谢: 2 次
接收感谢: 2 次

Re: What's wrong with this shell script program?

#5

帖子 kingkongmok » 2010-11-05 16:06

put $(($s+$i)) instead of $( ($s+$i) )
头像
trigger
帖子: 1585
注册时间: 2006-10-25 18:08
送出感谢: 0
接收感谢: 0

Re: What's wrong with this shell script program?

#6

帖子 trigger » 2010-11-05 16:10

shell style:

代码: 全选

#!/bin/bash
#sh012.sh
read -p "Please input a number, I'll count for 1+2+...+your_input:" nu
echo `seq $nu`|tr -s ' ' '+'|bc
./sh012.sh
Please input a number, I'll count for 1+2+...+your_input:10
55
楼主真是一派胡言,真可谓:“两个黄鹂鸣翠柳,不知所云;一行白鹭上青天,不知所止“。本来不想和你辩论,今天气愤不过,和你理论一番。我国宪法写得清清楚楚:“一夜夫妻百日恩,七楼以上才有电梯”。这个想必你知道,既然知道,你就不能断章取义,就算是天气预报,它还有不准的时候呢!!!再者说了,那中国银行也不是你一家开的。人家马拉多纳都结婚了,你还拿着粮票顶什么用呢。真是滑天下之大稽。前些日子,全国人大刚刚开过会,郑重声明:“中国不搞多party制,存栏母猪给补贴”。多好的事呢,楞让你这号人给搅混了。
头像
brace
帖子: 242
注册时间: 2008-10-11 22:49
来自: null
送出感谢: 0
接收感谢: 1 次

Re: What's wrong with this shell script program?

#7

帖子 brace » 2010-11-05 23:27

首先给这个脚本加上x权限
然后在终端中进入到脚本所在的目录下,然后执行./scriptname
上帝也是单亲家长
头像
astolia
论坛版主
帖子: 3105
注册时间: 2008-09-18 13:11
送出感谢: 1 次
接收感谢: 512 次

Re: What's wrong with this shell script program?

#8

帖子 astolia » 2010-11-05 23:50

代码: 全选

#!/bin/bash
# Program:
# Try do calculate 1+2+...+$(your_input)
# History:
# 2010/11/05 windvalley First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "Please input a number, I'll count for 1+2+...+your_input:" nu

s=0
i=0
while [ $i -le $nu ]
do
	s=$(($s+$i))
	i=$(($i+1))
done
echo "The result is" $s
windvalley
帖子: 4
注册时间: 2010-11-05 15:48
送出感谢: 0
接收感谢: 0

Re: What's wrong with this shell script program?

#9

帖子 windvalley » 2010-11-06 11:03

Thanks for all of your help. After I changed $( ($s+$i) ) to $(($s+$i)), the problem finally can work.
windvalley
帖子: 4
注册时间: 2010-11-05 15:48
送出感谢: 0
接收感谢: 0

Re: What's wrong with this shell script program?

#10

帖子 windvalley » 2010-11-06 11:16

Hi, another shell script program. It seems so hard to me.

#!/bin/bash
# Program:
# User inputs his salary. Program shows his personal income tax and his after-tax income.
# History:
# 2010/11/05 windvalley First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

read -p "Please input your salary: " salary #Prompt user to input his salary
if [ "$salary" < "0" ]; then
echo "Invalid input."
exit 1
elif [ "$salary" <= "2000" ]; then
tax=0
elif [ "$salary" <= "2500" ]; then
tax=$((($salary-2000)*0.05))
elif [ "$salary" <= "4000" ]; then
tax=$((($salary-2500)*0.1+25))
elif [" $salary" <= "7000" ]; then
tax=$((($salary-4000)*0.15+175))
elif [ "$salary" <= "22000" ]; then
tax=$((($salary-7000)*0.2+625))
elif [ "$salary" <= "42000" ]; then
tax=$((($salary-22000)*0.25+3625))
else
echo "Oh, you're quite rich."
exit 2
fi

echo "Your personal income tax is: " $tax ", and your after-tax income is: " $(($salary-$tax))
exit 0

Besides, could you introduce some book about shell script programming which is useful to beginners like me?
头像
trigger
帖子: 1585
注册时间: 2006-10-25 18:08
送出感谢: 0
接收感谢: 0

Re: What's wrong with this shell script program?

#11

帖子 trigger » 2010-11-06 13:53

shell是极端灵活的,怎么写怎么有,不见得就得if。。。
看这个,当然,你得给个整数

代码: 全选

#!/bin/bash
a=`echo {1..2000}|tr -s ' ' '|'`
b=`echo {2001..2500}|tr -s ' ' '|'`
c=`echo {2501..4000}|tr -s ' ' '|'`
d=`echo {4001..7000}|tr -s ' ' '|'`
e=`echo {7000..22000}|tr -s ' ' '|'`
f=`echo {22000..42000}|tr -s ' ' '|'`
# echo $a
read -p "Please input your salary: " s #Prompt user to input his salary
(($s<=0)) && echo "hi , man , its funny" && exit
(($s>42000)) && echo "hi , RICH ASS !" && exit
eval "
case $s in
$a) tax=0;;
$b) tax=`echo "($s-2000)*0.05"|bc`;;
$c) tax=`echo "($s-2500)*0.1+25"|bc`;;
$d) tax=`echo "($s-4000)*0.15+175"|bc`;;
$e) tax=`echo "($s-7000)*0.2+625"|bc`;;
$f) tax=`echo "($s-22000)*0.25+3625"|bc`;;
esac
"
echo "Your personal income tax is: "$tax
echo "And your after-tax income is: "`echo "$s-$tax"|bc` 
楼主真是一派胡言,真可谓:“两个黄鹂鸣翠柳,不知所云;一行白鹭上青天,不知所止“。本来不想和你辩论,今天气愤不过,和你理论一番。我国宪法写得清清楚楚:“一夜夫妻百日恩,七楼以上才有电梯”。这个想必你知道,既然知道,你就不能断章取义,就算是天气预报,它还有不准的时候呢!!!再者说了,那中国银行也不是你一家开的。人家马拉多纳都结婚了,你还拿着粮票顶什么用呢。真是滑天下之大稽。前些日子,全国人大刚刚开过会,郑重声明:“中国不搞多party制,存栏母猪给补贴”。多好的事呢,楞让你这号人给搅混了。
回复

回到 “老旧版本支持”