第一部分:使用vi/vim进行shell基础程序设计
1、编写脚本实现:从键盘输入两个数,然后比较两个数大小,并将结果输出。
#! /bin/bash
read -p "input two numbers:" n1 n2
max=$n1
min=$n2
if ((n2>max));thenmax=$n2min=$n1
fi
echo "$max>$min"
2、编写脚本计算两个整数的平方和。例:执行xx.sh 3 4时,输出结果为25。
#!/bin/bashread -p "input two numbers:" n1 n2echo "The sum of the squares of $n1 and $n2 is" `expr $n1 \* $n1 + $n2 \* $n2`
expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。
3、编写一个脚本,使用函数来判断从键盘输入的数是否为整数,若不是则重新输入。
#!/bin/bashjugment(){n=0while :doread pexpr $p + 0 &> /dev/null
#Linux 中的/dev/null是一个空设备文件。这将丢弃写入它的任何内容,并将在读取时返回EOF。
#$? 上个命令的退出状态,或函数的返回值。if [ $? -eq 0 ]
#若输入不为整数,则返回值小于0thenecho "The number you entered is $p"echon=$pbreakelseecho "The $p you entered is not an integer"echo -ne "Please enter an integer:"echocontinuefidone}echo "Enter an integer:"jugment
#执行函数jugmentnu=$n
注:
1、$? 上个命令的退出状态,或函数的返回值。
4、编写一个脚本,实现一个简单的加减乘除运算的计算器。要求:在两个操作数之间进行,并通过命令行菜单向用户提示输入变量和运算符。
#!/bin/bashecho " ----------------------------------------"echo "|!The dai Calculator is at your service!|"echo " ----------------------------------------"echopanduan(){n=0while :doread pexpr $p + 0 &> /dev/nullif [ $? -eq 0 ]thenecho "The number you entered is:"echo $pn=$pbreakelseecho "The $p you entered is not an integer"echo "Please enter an integer:"continuefidone}echo "Please enter the first integer:"panduannu1=$necho "Please enter the second integer:"panduannu2=$necho " ------------------"
echo "| 1.add |"
echo "| 2.subtract |"
echo "| 3.multiply |"
echo "| 4.divide |"
echo " ------------------"while :doread -p "Please enter the algorithm you want to execute:" acase $a in"1")sum=`expr $nu1 + $nu2`echo "$nu1+$nu2=$sum"break;;"2")jian=`expr $nu1 - $nu2`echo "$nu1-$nu2=$jian"break;;"3")chen=`expr $nu1 \* $nu2`echo "$nu1*$nu2=$chen"break;;"4")chu=`expr $nu1 / $nu2`echo "$nu1/$nu2=$chu"break;;*)echo "Please chose 1、2、3、4"echo "Please select again"esacdone
5、编写脚本实现猜价格游戏。具体过程:脚本一开始运行就自动生成一个0-100之间的数来表示实际价格,然后提示用户输入猜测的价格,然后和实际价格进行比较。若高了,就提示用户猜低一点;否则,就提示用户猜高一点。以此类推,一直到用户最终猜中。在程序运行过程中还要统计用户猜测的次数,并在最后回显。程序还能根据猜测次数给用户打分,满分100,大致规则是猜的次数越少分数越高,比如只用1次就猜中就是100分。具体打分规则自己来定。
#!/bin/bashtimes=0time=10luck=$[$RANDOM%100]#$RANDOM 是0~32767s=100score=0while truedoread -p "Enter the number you guess(0-100):" acklet times++if [ $luck -eq $ack ] && [ $times -le $time ]thenecho "You guessed it, the correct answer is:$luck,You used the $times opportunity"echo "Your score is:"`expr $s - $times \* 10`breakelif [ $luck -gt $ack ] && [ $times -le $time ]thenecho "Your $times guess is small. Your number is:$ack"elif [ $luck -lt $ack ] && [ $times -le $time ]thenecho "You guessed big, you have used $times the chance"elseecho "Sorry, we've run out of times"echo "The right answer is:$luck"echo "Your score is:0"breakfidone
6、编写脚本,提示用户输入一个小于100的整数,并计算从1到该数之间的所有整数的和。
#!/bin/bashread -p "Please enter an integer less than 100:" numif [ $num -eq 1 ];thenecho "And is equal to the: $num"sum=0elif [ $num -gt 1 ] && [ $num -lt 100 ];thenfor ((i=1; i<=$num; i++))dolet sum=$sum+$idoneecho "The sum from 1 to $num is $sum"elseecho "Input error!"fi
7、判断一个数是不是完数。打印出1-1000之间的完数。完数就是约数的和等于自身2倍的数。(6,28,496)
这款代码速度太慢。
#! /bin/bashfor((i=1;i<=1000;i++))dosum=0for((j=i;j>1;j--))dolet temp=i%jlet temp1=i/jif [ $temp -eq 0 ];thenlet sum+=temp1fidoneif [ $sum -eq $i ];thenecho $ifidone
这款代码特别快。
#!/bin/bashfor (( i=1; $[(2**i-1)*(2**(i-1))]<1000;i++ ))doif [ `factor $i $[2**i-1] |awk 'NF==2' |wc -l` -eq 2 ];thenecho $[(2**i-1)*(2**(i-1))]fidone
注:
- AWK 是一种处理文本文件的语言,是一个强大的文本分析工具。
8、绘制一个由“*”组成的菱形,菱形两个对角线上“*”的个数由用户输入。
#! /bin/bashread -p "Please enter the size of the diamond:" gfor ((i=1;i<=$g;i++))dofor ((j=$g;j>$i;j--))doecho -n " "donefor ((p=1;p<=$i;p++))doecho -n "* "doneecho ""donefor ((k=$g-1;k>=1;k--))dofor ((m=$g;m>$k;m--))doecho -n " "donefor ((l=1;l<=$k;l++))doecho -n "* "doneecho ""done