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