分页: 1 / 1

What's wrong with this shell script program?

发表于 : 2010-11-05 15:56
windvalley
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!

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

发表于 : 2010-11-05 16:01
windvalley
Sorry. It seems I posted it in a wrong place. :em06

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

发表于 : 2010-11-05 16:01
trigger
shell is not C
you can not use i=i+1 in shell

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

发表于 : 2010-11-05 16:03
fuxiu
( ($s+$i) )
my english is poor. :em03

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

发表于 : 2010-11-05 16:06
kingkongmok
put $(($s+$i)) instead of $( ($s+$i) )

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

发表于 : 2010-11-05 16:10
trigger
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

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

发表于 : 2010-11-05 23:27
brace
首先给这个脚本加上x权限
然后在终端中进入到脚本所在的目录下,然后执行./scriptname

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

发表于 : 2010-11-05 23:50
astolia

代码: 全选

#!/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

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

发表于 : 2010-11-06 11:03
windvalley
Thanks for all of your help. After I changed $( ($s+$i) ) to $(($s+$i)), the problem finally can work.

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

发表于 : 2010-11-06 11:16
windvalley
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?

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

发表于 : 2010-11-06 13:53
trigger
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`