Shell编程之案例积累

需求1:shell脚本判断分区使用率
#!/bin/bash
#统计根分区使用率
rate=$(df -h | grep "/dev/sda3" | awk '{print $5}' | cut -d "%" - f1)
#把根分区使用率作为变量赋予变量rate
if [ $rate -ge 80 ]
  then
    echo "Warning! /dev/sda3 is full!!"
fi
需求2:备份mysql数据库
#!/bin/bash
#同步系统时间
ntpdate asia.pool.ntp.org &>/dev/null
#把当前系统时间按照“年月日”格式赋予变量date
date=$(date +%y%m%d)
#统计mysql数据库的大小,并把大小赋予size变量
#此处mysql目录甜自己电脑(服务器)上的
size=$(du -sh /var/lib/mysql)

if [ -d /tmp/dbbak ]
  #如果备份目录存在
  then
    #把当前日期输出到一个临时目录,覆盖
    echo "Data : $date!" > /tmp/dbbak/dbinfo.txt
    #把刚备份的
    echo "Data size : $size" >> /tmp/dbbak/dbinfo.txt
    cd /tmp/bdbak
    #打包/var/lib/mysql和dbinfo.txt
    tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql dbinfo.txt &>/dev/null
    #删除临时文件
    rm -rf /tmp/dbbak/dbinfo.txt
  #如果备份目录不存在
  else
    mkdir /tmp/dbbak
    echo "Date : $date!" > /tmp/dbbak/dbinfo.txt
    echo "Data size : $size" >> /tmp/dbbak/dbinfo.txt
    cd /tmp/bdbak
    tar -zcf mysql-lib-$date.tar.gz /var/lib/mysql dbinfo.txt &>/dev/null
    rm -rf /tmp/dbbak/dbinfo.txt
需求3:判断apache是否启动
#!/bin/bash
#常见判断apacha进程是否启动:
#ps aux | grep httpd
#弊端:有些情况下能判断apache启动了,但是她不能解决apache启动但电脑死机的情况,即apache无法响应后端请求。
#netstat -tlun
#弊端:不能确定apache是不是正常连接,只能确定是不是启动了;且不一定是apache占用的80端口,其他的网页服务,rs等也有可能占用
#nmap是扫描指定服务器上开启的tcp端口
#使用nmap命令扫描服务器并截取apache服务的状态,赋予变量port
#下面的语句意思:截取tcp端口状态,再从输出的状态中截取apache状态
port=$(nmap - sT 192.168.1.156 | grep tcp | grep http |awk '{print $2}')

#注意必须有空格
if ["$port" == "open"]
  then
    echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
  else
    /etc/rc.d/init.d/http start &>/dev/null
    echo "$(date) restart http !!" >> /tmp/autostart-err.log
fi
需求4:判断用户输入的是什么文件
#!/bin/bash
#接收键盘的输入,并赋予变量file
read -p "Please input a filename:" file
#判断file变量是否为空
if [ -z "$file" ]
  then
    echo "Error,please input a filename"
    #如果不写exis就会继续往下运行
    exit 1
  #判断file的值是否存在
  elif [ ! -e "$file" ]
    then
      echo "$file is not a file!"
      exit 2
  #判断file是否为普通文件
  elif [ -f "$file" ]
    then
      echo "$file is a regulare file!"
  #判断file是否为目录文件
  elif [ -d "$file" ]
    then
      echo "$file is a directory!"
  else
      echo "$file is an other file!"
fi
需求5:判断用户输入
#!/bin/bash
read -p "Please choose yes/no:" -t 30 cho
case $cho in
  "yes")
    echo "Your choice is yes!"
    ;;
  "no")
    echo "Your choice is no!"
    ;;
  *)
    echo "Your input is error!"
    ;;
esac
需求6:打印时间
#!/bin/bash
for time in morning noon afternoon evening
  do
    echo "This time is $time!"
  done
需求7:批量解压缩脚本
#!/bin/bash
#假设所有的脚本包都在根下lamp文件中
cd /lamp
#把所有.tar.gz结尾的文件名写入ls.log文件
ls *.tar.gz > ls.log
#只要是空格或者换行符隔开的,就认为是一个内容
for i in $(cat ls.log)
  do
    tar -zxf $i &>/dev/null
  done
rm -rf /lamp/ls.log
需求8:从1加到100(for)
#!/bin/bash
s=0
for((i=1;i<=100;i=i+1))
  do
    s=$(( $s+$i ))
  done
echo "The sum of 1+2+...+100 is:$s"
需求9:批量添加指定数量的用户
#!/bin/bash
#初始用户名
#每次添加,在用户名前加一个标号
read -p "Please input user name:" -t 30 name
#添加个数
read -p "Please input the number of users:" -t 30 num
#初始密码
read -p "Please input the password of users:" -t 30 pass
#预期结果是stu01、stu02...
if[ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]
  then
    #判断是否输入的是数字,通过正则匹配
    #除了以下的正则,还可以是:^[0-9]*$
    #如果是数字,y就为空
    y=$(echo $num | sed 's/[0-9]//g')
      if [ -z "&y" ]
        then
          for((i=1;i<=$num;i=i+1))
            do
              /usr/sbin/useradd $name$i &>/dev/null
              echo $pass | /usr/bin/passwd --stdin “$name$i” &>/dev/null
            done
      fi
fi
需求10:从1加到100(while)
#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
  do
    s=$(( $s+$i ))
    i=$(( $i+1 ))
  done
echo "The sum of 1+2+...+100 is:$s"
需求10:从1加到100(until)
#!/bin/bash
s=0
i=1
until [ $i -gt 100 ]
  do
    s=$(( $s+$i ))
    i=$(( $i+1 ))
  done
echo "The sum of 1+2+...+100 is:$s"
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).
### 回答1: 游戏Shell编程Linux案例可以用于编写游戏脚本,以便在Linux系统上运行游戏。游戏脚本可以使用Bash Shell编写,并使用命令行界面(CLI)或图形用户界面(GUI)来与用户交互。以下是一个使用Shell编程实现的简单游戏案例。 我们可以通过Shell编程设计一个“石头,剪子,布”的游戏。首先,程序会要求用户输入“石头”,“剪刀”或“布”。接着程序会以随机的方式输出一个选项,并比较用户的选择与程序选项的差异。如果用户和程序选择一致,则输出“平局”,否则判断用户胜负并输出结果。 以下是实现此游戏的伪代码: ``` read choice # 程序提示用户输入选项并读取用户输入 computer_choice = `echo "rock paper scissors" | sed 's/ /\n/g' | shuf -n1` # 以随机方式生成程序选项 if [ $choice == $computer_choice ]; then # 判断用户是否与程序选择一致 echo "tie" elif [ $choice == "rock" ] && [ $computer_choice == "scissors" ]; then # 判断用户和程序选择的胜负 echo "win" elif [ $choice == "scissors" ] && [ $computer_choice == "paper" ]; then echo "win" elif [ $choice == "paper" ] && [ $computer_choice == "rock" ]; then echo "win" else echo "lose" fi ``` 此外,还可以使用Shell编程来实现诸如猜数字、数独等的小游戏。尤其在Linux系统中,Shell编程已被广泛应用于自动化任务和脚本编写,因此使用Shell编程来开发游戏也是很好的选择。 ### 回答2: Shell 编程是一种在 Linux 系统下用来编写脚本的语言,它是一种命令解释器,可以用来完成许多自动化任务。其中,游戏 shell 编程是一个有趣的案例,可以通过 Shell 编程来实现一些小游戏。 比如,我们可以写一个猜数字小游戏。首先,我们需要用 Shell 编程生成一个随机数,然后让玩家输入一个猜测的数字。如果猜对了,游戏就结束了。如果猜错了,程序会提示玩家,让他继续猜,直到猜对为止。 另一个例子是写一个简单的文字冒险游戏。我们可以用 Shell 编程实现一个迷宫,玩家需要输入方向来控制游戏角色前进。同时,在游戏中也可以加入一些障碍和道具,使得游戏更加有趣。 还有一个例子是编写一个猜词小游戏。我们可以用 Shell 编程从一个单词库中随机挑选一个单词,然后让玩家猜这个单词。同时,程序需要给出一些提示,比如单词的长度和其中某些字母的位置。当玩家猜对了单词,游戏结束。 这些小游戏可以通过 Shell 编程实现,不仅可以锻炼编程技能,还可以提供娱乐和乐趣。同时,它也体现了 Shell 编程的灵活性和实用性,可以用来完成各种自动化任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值