初学shell,给大家分享我的一下我的练习题目,有不对或者写的不好的地方还请大家指出。
1.编写脚本实现:从键盘输入两个数,然后比较两个数大小,并输出。
参考代码
#!/bin/bash
#work1
#Compare the size of two numbers
echo "Please input first number."
read x
echo "Please input seconde number11"
read y
if [ $x -eq $y ]; then
echo "equal!!!"
elif [ $x -gt $y ];then
echo "x > y!!!"
else
echo "x < y!!!"
fi
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w1.sh
Please input first number.
4
Please input seconde number11
6
x < y!!!
2.编写脚本计算两个整数的平方和。例:执行xx.sh 3 4时,输出结果为25.
参考代码
#!/bin/bash
#work two number pow(2) sum
totll=0
echo "The two number is $1,$2"
echo "The two number Sum of squares is "
totll=`expr $1 \* $1 + $2 \* $2`
echo ${totll}
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w2.sh 2 4
The two number is 2,4
The two number Sum of squares is
20
3.编写一个脚本,使用函数来判断从键盘输入的数是否为整数,若不是则重新输入。
参考代码
#!/bin/bash
#if number is int then out,if not input agin
flag=true
while $flag
do
echo "Please input a number"
read x
expr $x + 0 &>/dev/null
[ $? -eq 0 ] && flag=false || echo "This is a float number!input again"
done
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w3.sh
Please input a number
3.3
This is a float number!input again
Please input a number
2
tanaocheng@tanaocheng-TUF:~/linuxwork$
4.编写一个脚本,实现一个简单的加减乘除运算的计算器。要求:在两个操作数之间进行,并通过命令行菜单向用户提示输入变量的运算符。
参考代码,这里没有考虑浮点数的运算
#!/bin/bash
#A simple calculator
echo "Please input two int number"
flag1=true
while $flag1
do
read x y
expr $x + 0 &>/dev/null
[ $? -eq 0 ] && flag1=false
expr $y + = &>/dev/null
[ $? -eq 0 ] && flag1=false
if ( ($flag1 != true ) )
then
echo "Error in put,input again!!"
fi
done
echo "Please select Calcilation method"
echo "1-add 2-subtract"
echo "3-multuply 4-divide"
read z
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w4.sh
Please input two int number
4 2
Please select Calcilation method
1-add 2-subtract
3-multuply 4-divide
1
Out: 6
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w4.sh
Please input two int number
6 4
Please select Calcilation method
1-add 2-subtract
3-multuply 4-divide
2
Out: 2
5.编写脚本实现猜价格游戏,0-100...
参考代码
#!/bin/bash
#Guess the price,until guess right
number=$[$RANDOM%100]
num=1
while true
do
read -p "Input your guess number: " x
if [ $x -eq ${number} ]; then
echo "You are right,test num is $num"
break
elif [ $x -gt ${number} ]; then
echo "Sorry Bigger,Guess small again"
let num=num+1
continue
else
echo "Srry Small,Guess bigger again"
let num=num+1
continue
fi
read -p "Continue?[y/n]" z
if ( ($z=n) )then
break
else
continue
fi
done
echo "Game over"
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w5.sh
iInput your guess number: 50
Srry Small,Guess bigger again
iInput your guess number: 75
Srry Small,Guess bigger again
iInput your guess number: 87
Sorry Bigger,Guess small again
iInput your guess number: 81
Sorry Bigger,Guess small again
iInput your guess number: 78
Sorry Bigger,Guess small again
iInput your guess number: 76
You are right,test num is 6
Game over
6.编写脚本,提示用户输入一个小于100的整数,并计算从1到该数之间的所有整数的和。
参考代码
#!/bin/bash
# sum of small input all number
read -p "Please input a number: " x
sum=0
for((i=0;i<=x;i=i+1))
do
let sum+=$i
done
echo "out: ${sum}"
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w6.sh
Please input a number: 10
out: 55
7.判断一个数是不是完数。打印出1-1000之间的完数。...
参考代码
#!/bin/bash
#JUdge whether perfect number(1-1000)
##efficiency n*(1+2+3...+n)/2
sum=0
t=0
for((i=1;i<=1000;i=i+1))
do
for((j=1;j<=i;j=j+1))
do
let t=i%j
if [ $t -eq 0 ]
then
let sum=sum+j
fi
done
if [ $sum -eq `expr $i \* 2` ]
then
echo "The number $i is a perfect number"
fi
sum=0
done
测试
tanaocheng@tanaocheng-TUF:~/linuxwork$ ./w7.sh
The number 6 is a perfect number
The number 28 is a perfect number
The number 496 is a perfect number
8.绘制一个由“*”组成的菱行,菱行上对角线的“*”个数由用户输入..
|