文章目录

  • 1.猜数字
  • 2.石头剪刀布
  • 3.排大小
  • 4.时钟
  • 5.开机启动问候
  • 6.打印国际象棋
  • 7. 打印乘法表
  • 8. 彩色动态管道

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 的随机数,你猜: " caiif [ $cai -eq $num ]thenecho "恭喜,猜对了"exitelif [ $cai -gt $num ]thenecho "Oops,猜大了"elseecho "Oops,猜小了"fi
done

2.石头剪刀布

#!/bin/bashgame=(石头 剪刀 布)
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 ]thenecho "你赢"else echo "计算机赢"
fi;;
2)if [ $num -eq 0 ]thenecho "计算机赢"elif [ $num -eq 1 ] thenecho "平局"else echo "你赢"
fi;;
3)if [ $num -eq 0 ]then  echo "你赢"elif [ $num -eq 1 ]then echo "计算机赢"else echo "平局"
fi;;
*)echo "必须输入1-3 的数字"
esac 

3.排大小

#!/bin/bash# 依次提示用户输入 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 ];thentmp=$num1num1=$num2num2=$tmp
fi
# 如果 num1 大于 num3,就把 num1 和 num3 对调,确保 num1 变量中存的是最小值
if [ $num1 -gt $num3 ];thentmp=$num1num1=$num3num3=$tmp
fi
# 如果 num2 大于 num3,就把 num2 和 num3 对调,确保 num2 变量中存的是最小值
if [ $num2 -gt $num3 ];thentmp=$num2num2=$num3num3=$tmp
fi
echo "排序后数据(从小到大)为:$num1,$num2,$num3"

4.时钟

#!/bin/bash# 使用死循环实时显示时间 while :
doecho "当前时间是:$(date +"%Y‐%m‐%d %H:%M:%S")"sleep 1
done

5.开机启动问候

tips:将该脚本放到 /etc/profile.d/ 路径下用于开机问候

#!/bin/bash
# 根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动 # 00‐12 点为早晨,12‐18 点为下午,18‐24 点为晚上
# 使用 date 命令获取时间后,if 判断时间的区间,确定问候语内容
json=`curl -s http://www.weather.com.cn/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 ];thenmsg="Good Morning $USER"
elif [ $tm -gt 12 -a $tm -le 18 ];thenmsg="Good Afternoon $USER"
elsemsg="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}
dofor j in {1..8}dosum=$[i+j]if [  $[sum%2] -eq 0 ];thenecho -ne "\033[46m  \033[0m"elseecho -ne "\033[47m  \033[0m"fidoneecho
done

7. 打印乘法表

#!/bin/bashfor i in `seq 9`do for j in `seq $i`doecho -n "$i*$j=$[i*j] "doneechodone

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 Linux Forums
#
# I, Yu-Jie Lin, made a few changes and additions:
#
#   -p, -t, -R, and -C
#
#   Screenshot: http://flic.kr/p/dRnLVj
#   Screencast: http://youtu.be/5XnGSFg_gTk
#
# And push the commits to Gist:
#
#   https://gist.github.com/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
#
#   Pushed the changes to https://gist.github.com/4725048
#   hole1: https://gist.githubusercontent.com/livibetter/4689307/raw/949e43fe2962c2c97c8b1d974ff93dd053d9bd37/pipes.sh
#   hole2: Fun On The Terminal Part 2p=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=("┃┏ ┓┛━┓  ┗┃┛┗ ┏━""│╭ ╮╯─╮  ╰│╯╰ ╭─""│┌ ┐┘─┐  └│┘└ ┌─""║╔ ╗╝═╗  ╚║╝╚ ╔═"
)
v="${sets[0]}"
RNDSTART=0
NOCOLOR=0OPTIND=1
while getopts "p:t:f:s:r:RCh" arg; do
case $arg inp) ((p=(OPTARG>0)?OPTARG:p));;t) ((OPTARG>=0 && OPTARG<${#sets[@]})) && v="${sets[OPTARG]}";;f) ((f=(OPTARG>19 && OPTARG<101)?OPTARG:f));;s) ((s=(OPTARG>4 && OPTARG<16 )?OPTARG:s));;r) ((r=(OPTARG>=0)?OPTARG:r));;R) RNDSTART=1;;C) NOCOLOR=1;;h) echo -e "Usage: $(basename $0) [OPTION]..."echo -e "Animated pipes terminal screensaver.\n"echo -e " -p [1-]\tnumber of pipes (D=1)."echo -e " -t [0-$((${#sets[@]} - 1))]\ttype of pipes (D=0)."echo -e " -f [20-100]\tframerate (D=75)."echo -e " -s [5-15]\tprobability of a straight fitting (D=13)."echo -e " -r LIMIT\treset after x characters, 0 if no limit (D=2000)."echo -e " -R \t\trandom starting point."echo -e " -C \t\tno color."echo -e " -h\t\thelp (this screen).\n"exit 0;;esac
donecleanup() {tput rmcuptput cnormexit 0
}
trap cleanup SIGHUP SIGINT SIGTERMfor (( i=1; i<=p; i++ )); doc[i]=$((i%8)) n[i]=0 l[i]=0((x[i]=RNDSTART==1?RANDOM*w/32768:w/2))((y[i]=RNDSTART==1?RANDOM*h/32768:h/2))
donetput smcup
tput reset
tput civis
while ! read -t0.0$((1000/f)) -n1; dofor (( i=1; i<=p; i++ )); do# New position:((${l[i]}%2)) && ((x[i]+=-${l[i]}+2,1)) || ((y[i]+=${l[i]}-1))# Loop on edges (change color on loop):((${x[i]}>w||${x[i]}<0||${y[i]}>h||${y[i]}<0)) && ((c[i]=RANDOM%8))((x[i]=(x[i]+w)%w))((y[i]=(y[i]+h)%h))# New random direction:((n[i]=RANDOM%s-1))((n[i]=(${n[i]}>1||${n[i]}==0)?${l[i]}:${l[i]}+${n[i]}))((n[i]=(${n[i]}<0)?3:${n[i]}%4))# Print:tput cup ${y[i]} ${x[i]}[[ $NOCOLOR == 0 ]] && echo -ne "\033[1;3${c[i]}m"echo -n "${v:l[i]*4+n[i]:1}"l[i]=${n[i]}done((r>0 && t*p>=r)) && tput reset && tput civis && t=0 || ((t++))
done
cleanup


参考:https://blog.csdn.net/weixin_42405670/article/details/89818462


几个简单有趣的shell脚本相关推荐

  1. 编写一个弹出式菜单的shell程序_分享一个有趣的shell脚本--实现抓阄程序

    概述 今天主要分享一个有趣的shell脚本,用来实现抓阄,平时就不用剪刀石头布了. 需求 使用shell编写一个抓阄的程序: 1.执行脚本后,输入英文名字全拼,产生随机数01-99之间的数字,数字越大 ...

  2. linux脚本怎么发送到桌面,如何在Linux上使用Zenity创建简单的图形Shell脚本

    Zenity使用单个命令为shell脚本添加了图形界面. Shell脚本是自动化重复任务的好方法,但是它们通常只限于终端 - Zenity将它们从终端中导出到桌面上. 我们已经介绍了过去的shell脚 ...

  3. 分享一个有趣的shell脚本--单词及字母去重排序案例

    概述 今天主要分享一个用shell脚本来实现单词及字母去重排序案例,下面一起来看下吧~ 需求 1.按单词出现频率降序排序! 2.按字母出现频率降序排序! 相关文本: the squid project ...

  4. 简单的倒计时shell脚本

    效果如下: 代码如下: #! /bin/bash #####################倒计时################ #作者:liop #完成时间:2019.12.17 #三位数以内秒数 ...

  5. 嵌入式 Linux 入门(五、Shell 脚本编程上:认识 Shell 脚本)

    大家好,是矜辰所致,嵌入式 Linux入 门第五课,本课开始简单学习一下 Shell 脚本编程. 目录 前言 一.Shell 脚本基础说明 1.1 什么是 Shell 脚本 1.2 Shell 脚本的 ...

  6. 用shell脚本监控系统

    简单的用shell脚本写一个"监控"程序作为思路,大致为:实时检测系统的内存使用率,如果大于阈值那么报警(如果有条件可以使用短信接口或者实在不行可以使用邮件通知),并记录到日志文件 ...

  7. Linux shell脚本基础学习详细介绍(完整版)2

    详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...

  8. linux循环处理脚本命令,后端开发必须掌握的Linux命令[Shell脚本篇]

    Shell脚本相关命令 介绍 一句话说明shell脚本是个啥 shell脚本就是将多个shell指令汇集到一起去完成一个复杂的功能 类似windows下的批处理文件 一般以sh为文件后缀 语法 程序结 ...

  9. linux shell 一行 for,BASH shell脚本回显到同一行输出

    我有一个简单的BASH shell脚本,它检查curl命令的HTTP响应代码. 逻辑很好,但我坚持"简单地"打印出"输出". 我正在使用GNU bash,版本3 ...

最新文章

  1. mesh threejs 属性_Threejs构建mesh
  2. Sql Server编程
  3. perl regular expresstion
  4. python lambda函数详细解析(据说面试90%的人经常遇到)
  5. 如何将图片序列化_如何将图片文字转化为Word文档?
  6. 7c盘满了怎么扩容_iPhone 备份文件太大,C 盘不够放怎么办?
  7. win7电脑误删鼠标键盘驱动_快速恢复win7系统因驱动冲突导致鼠标键盘用不了的详细步骤...
  8. FC网络光纤通信 c语言实现,[2018年最新整理]光纤通信实验指导书(含原理).doc
  9. sumif单列求和_Sumif、Sumifs单列多条件求和
  10. python北京房价预测_《安家》热播,我用Python对北京房价进行了分析,结果……...
  11. 发音到底是 /s/ 还是 /z/ ?
  12. 新生学大学计算机心得,大学生信息技术心得体会怎么写
  13. win10安装提示“我们无法创建新的分区”
  14. Python Pathlib 详解
  15. idea中src/main/resources目录下的applicationContext.xml文件访问src/main/webap目录下的配置文件
  16. win10运行在哪里打开 win10怎么打开运行窗口快捷键
  17. RHCE 第十一天 mail postfix dovecot SSL
  18. 【方案开发】电子血压计方案开发设计
  19. python基础-运算符
  20. Java七大设计原则详解与运用

热门文章

  1. 华为在线软件训练与测试
  2. 我在滴滴数据分析岗实习了8个月
  3. Spring IoC组装打印机
  4. python-docx_python-docx中设置中文字体
  5. Mysql数据库面试典籍30+ | 大别山码将
  6. 使用装饰者设计模式增强自定义连接池
  7. 单链表实现增删改查(进化版)
  8. 你女朋友都能看懂的“三握四挥”
  9. C语言刷题系列——9.在数组中查找指定元素
  10. php 仿美团切换城市,微信小程序仿美团城市选择的实现