- 脚本内容
$ cat game.sh
#!/usr/bin#需求:用shell脚本写一个石头、剪刀、步的游戏
#定义函数ofen,存放经常使用到的输出语句
function often(){
echo "你出的拳是:$person"
echo "电脑出的拳是:$computer"
}
#用数字1、2、3分别模拟石头、剪刀、步
echo "你可以输入1,2,3代表你出的拳,1表示石头,2表示剪刀,3表示步"
#1.提示用户出拳,并接出拳
read -p '请出拳[1(石头) | 2(剪刀) | 3(步)]:' person#2.判断person是否为合法出拳,不是退出游戏
if [ $person -ne 1 -a $person -ne 2 -a $person -ne 3 ];then
echo '请输入合法的猜拳!'
exit
fi#3.电脑出拳
computer=$[ $RANDOM % 3 + 1 ]#4.比较person 和 computer的值,判断输赢
if [ $person -eq $computer ];then
often
echo "此局是平局"
elif [ $person -eq 1 -a $computer -eq 2 ] || [ $person -eq 2 -a $person -eq 3 ] || [ $person -eq 3 -a $computer -eq 1 ];then
often
echo "此局你赢了"
else
often
echo "此局你输了"
fi
- linux主机运行脚本
bash game.sh