⼏个简单有趣的shell脚本⽂章⽬录
1.猜数字
#!/bin/bash
# 脚本⽣成⼀个 100 以内的随机数,提⽰⽤户猜数字,根据⽤户的输⼊,提⽰⽤户猜对了,
# 猜⼩了或猜⼤了,直⾄⽤户猜对脚本结束。
# RANDOM 为系统⾃带的系统变量,值为 0‐32767的随机数
# 使⽤取余算法将随机数变为 1‐100 的随机数
num=$[RANDOM%100+1]
echo"$num"
# 使⽤ read 提⽰⽤户猜数字
# 使⽤ if 判断⽤户猜数字的⼤⼩关系:‐eq(等于),‐ne(不等于),‐gt(⼤于),‐ge(⼤于等于),
# ‐lt(⼩于),‐le(⼩于等于)
while:
do
read -p "计算机⽣成了⼀个 1‐100 的随机数,你猜: " cai
if[$cai -eq $num]
then
echo"恭喜,猜对了"
exit
elif[$cai -gt $num]
then
echo"Oops,猜⼤了"
else
echo"Oops,猜⼩了"
fi
done
2.⽯头剪⼑布
game=(⽯头剪⼑布)
num=$[RANDOM%3]
computer=${game[$num]}
echo"请根据下列提⽰选择您的出拳⼿势" echo" 1. ⽯头"
echo" 2. 剪⼑"
echo" 3. 布 "
read -p "请选择 1-3 :" person
echo"      $(($person)) VS $((num+1))" echo"你:${game[$person-1]} VS 电脑:$computer" case$person in
1)
if[$num -eq 0 ]
then
echo"平局"
elif[$num -eq 1 ]
then
echo"你赢"
else
echo"计算机赢"
fi;;
2)
if[$num -eq 0 ]
then
echo"计算机赢"
elif[$num -eq 1 ]
then
echo"平局"
else
echo"你赢"
fi;;
3)
if[$num -eq 0 ]
then
echo"你赢"
elif[$num -eq 1 ]
then
echo"计算机赢"
else
echo"平局"
fi;;
*)
echo"必须输⼊1-3 的数字"
esac
3.排⼤⼩
# 依次提⽰⽤户输⼊ 3 个整数,脚本根据数字⼤⼩依次排序输出 3 个数字
read -p " 请输⼊⼀个整数: " num1
read -p " 请输⼊⼀个整数: " num2
read -p " 请输⼊⼀个整数:  " num3
# 不管谁⼤谁⼩,最后都打印 echo "$num1,$num2,$num3"
# num1 中永远存最⼩的值,num2 中永远存中间值,num3 永远存最⼤值
# 如果输⼊的不是这样的顺序,则改变数的存储顺序,如:可以将 num1 和 num2 的值对调tmp=0
# 如果 num1 ⼤于 num2,就把 num1 和和 num2 的值对调,确保 num1 变量中存的是最⼩值if[$num1 -gt $num2];then
tmp=$num1
num1=$num2
num2=$tmp
fi
# 如果 num1 ⼤于 num3,就把 num1 和 num3 对调,确保 num1 变量中存的是最⼩值
if[$num1 -gt $num3];then
tmp=$num1
num1=$num3
num3=$tmp
fi
# 如果 num2 ⼤于 num3,就把 num2 和 num3 对调,确保 num2 变量中存的是最⼩值
if[$num2 -gt $num3];then
tmp=$num2
num2=$num3
num3=$tmp
fi
echo"排序后数据(从⼩到⼤)为:$num1,$num2,$num3"
4.时钟
#!/bin/bash
# 使⽤死循环实时显⽰时间
while:
do
echo"当前时间是:$(date +"%Y‐%m‐%d %H:%M:%S")"
sleep 1
done
5.开机启动问候
tips:将该脚本放到 /etc/profile.d/ 路径下⽤于开机问候
# 根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动
# 00‐12 点为早晨,12‐18 点为下午,18‐24 点为晚上
# 使⽤ date 命令获取时间后,if 判断时间的区间,确定问候语内容
json=`curl -s www.weather/data/sk/101010100.html`
#echo $json
city=`echo $json |sed's/.*city":"//g'|sed's/","cityid.*$//g'`
temp=`echo $json |sed's/.*temp":"//g'|sed's/","WD.*$//g'`
wd=`echo $json |sed's/.*WD":"//g'|sed's/","WS.*$//g'`
ws=`echo $json |sed's/.*WS":"//g'|sed's/","SD.*$//g'`
tm=$(date +%H)
if[$tm -le 12 ];then
msg="Good Morning $USER"
elif[$tm -gt 12 -a $tm -le 18 ];then
msg="Good Afternoon $USER"
else
msg="Good Night $USER"
fi
echo"当前时间是:$(date +"%Y‐%m‐%d %H:%M:%S")"
echo -e "\033[34m$msg\033[0m"
echo'you are now at '$city','$temp'℃,'$ws$wd'.'
6.打印国际象棋
#!/bin/bash
# 打印国际象棋棋盘
# 设置两个变量,i 和 j,⼀个代表⾏,⼀个代表列,国际象棋为 8*8 棋盘
# i=1 是代表准备打印第⼀⾏棋盘,第 1 ⾏棋盘有灰⾊和蓝⾊间隔输出,总共为 8 列
# i=1,j=1 代表第 1 ⾏的第 1 列;i=2,j=3 代表第 2 ⾏的第 3 列
# 棋盘的规律是 i+j 如果是偶数,就打印蓝⾊⾊块,如果是奇数就打印灰⾊⾊块
# 使⽤ echo ‐ne 打印⾊块,并且打印完成⾊块后不⾃动换⾏,在同⼀⾏继续输出其他⾊块for i in{1..8}
do
for j in{1..8}
do
sum=$[i+j]
if[  $[sum%2] -eq 0 ];then
echo -ne "\033[46m  \033[0m"
else
echo -ne "\033[47m  \033[0m"
fi
done
echo
done
7. 打印乘法表
for i in`seq 9`
do
for j in`seq $i`
do
echo -n "$i*$j=$[i*j] "
done
echo
done
8. 彩⾊动态管道
#!/bin/bash
# The author of the original script is unknown to me. The first entry I can
# find was posted at 2010-03-21 09:50:09 on Arch Linux Forums (doesn't mean the
# poster is the author at all):
#
#  Post your handy self made command line utilities (Page 37) / Programming & Scripting / Arch Linu
x Forums
#
# I, Yu-Jie Lin, made a few changes and additions:
#
#  -p, -t, -R, and -C
#
#  Screenshot: flic.kr/p/dRnLVj
#  Screencast: youtu.be/5XnGSFg_gTk
#
# And push the commits to Gist:
#
#  gist.github/4689307
#
# I, Devin Samarin, made a few changes and additions:
#
#  -r can be 0 to mean "no limit".
#  Reset cursor visibility after done.
#  Cleanup for those people who want to quit with ^C
#
shell最简单脚本#  Pushed the changes to gist.github/4725048
#  hole1: gist.githubusercontent/livibetter/4689307/raw/949e43fe2962c2c97c8b1d974ff93dd053d9bd37/pipes.sh #  hole2: Fun On The Terminal Part 2
p=1
f=75 s=13 r=2000 t=0
w=$(tput cols) h=$(tput lines)
# ab -> idx = a*4 + b
# 0: up, 1: right, 2: down, 3: left
# 00 means going up  , then going up  -> ┃
# 12 means going right, then going down -> ┓
sets=(
"┃┏┓┛━┓┗┃┛┗┏━"
"│╭╮╯─╮╰│╯╰╭─"
"│┌┐┘─┐└│┘└┌─"
"║╔╗╝═╗╚║╝╚╔═"
)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。