A-A+

5个Shell脚本编程入门练习例子

2018年08月10日 好文阅读 暂无评论 阅读 0 views 次

这篇文章主要介绍了5个Shell脚本编程入门例子,涵盖了各种操作,又有一些游戏的性质,作为入门练习例子是不很不错的,需要的朋友可以参考下。

例子一:绘制特殊图形,代码如下:

  1. #!/bin/bash  
  2.    
  3. MAX_NO=0  
  4.    
  5. echo -n "Enter Number between (5 to 9) : "  
  6.  read MAX_NO  
  7.    
  8. if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ] ; then  
  9.  echo "WTF... I ask to enter number between 5 and 9, Try Again"  
  10.  exit 1  
  11.  fi  
  12.    
  13. clear  
  14.    
  15. for (( i=1; i=i; s-- ))  
  16.  do  
  17.  echo -n " "  
  18.  done  
  19.  for (( j=1; j=1; i-- ))  
  20.  do  
  21.  for (( s=i; s<=MAX_NO; s++ ))  
  22.  do  
  23.  echo -n " "  
  24.  done  
  25.  for (( j=1; j<=i; j++ ))  
  26.  do  
  27.  echo -n " ."  
  28.  done  
  29.  echo ""  
  30.  done  
  31.    
  32. echo -e "\n\n\t\t\t Whenever you need help, Tecmint.com is always there"  

你应该不会被上述例子中的“关键字”困扰了,很多都是你熟悉的,或者从它们的名字可以猜出它们的意思,如“max”设定某个变量的最大值,“for”是一个循环。

输出结果,代码如下:

  1. [root@tecmint ~]# chmod 755 Special_Pattern.sh  
  2. [root@tecmint ~]# ./Special_Pattern.sh  
  3. Enter Number between (5 to 9) : 6  
  4.        .  
  5.       . .  
  6.      . . .  
  7.     . . . .  
  8.    . . . . .  
  9.   . . . . . .  
  10.   . . . . . .  
  11.    . . . . .  
  12.     . . . .  
  13.      . . .  
  14.       . .  
  15.        .  
  16.    
  17.         Whenever you need help, Tecmint.com is always there  

如果你有其它语言的编程基础,那么学习上面的脚本对你来说应该很容易。即使你是计算机方面的新手,这个学习过程也不会太难。

例子二:五颜六色的脚本

Linux终端也是支持五颜六色的,请看下面的脚本:

  1. #!/bin/bash  
  2.    
  3. clear  
  4. echo -e "\033[1m Hello World"  
  5.  # bold effect  
  6. echo -e "\033[5m Blink"  
  7.        # blink effect  
  8. echo -e "\033[0m Hello World"  
  9.  # back to noraml  
  10.    
  11. echo -e "\033[31m Hello World"  
  12.  # Red color  
  13. echo -e "\033[32m Hello World"  
  14.  # Green color  
  15. echo -e "\033[33m Hello World"  
  16.  # See remaing on screen  
  17. echo -e "\033[34m Hello World"  
  18. echo -e "\033[35m Hello World"  
  19. echo -e "\033[36m Hello World"  
  20.    
  21. echo -e -n "\033[0m"  
  22.   # back to noraml  
  23. echo -e "\033[41m Hello World"  
  24. echo -e "\033[42m Hello World"  
  25. echo -e "\033[43m Hello World"  
  26. echo -e "\033[44m Hello World"  
  27. echo -e "\033[45m Hello World"  
  28. echo -e "\033[46m Hello World"  
  29. echo -e "\033[0m Hello World"  

输出结果:

生存法

你可以对上面的列子举一反三,把它用到你自己的脚本中去。

例子三:加密文件/目录

下面的例子演示了如何加密一个份文件或者文件夹。目前的这个版本的脚本有一些局限,例如你必须把它和你要加密的文件/目录放到同一个文件夹下面。另外,你可能需要安装“pinentry-gui”。在Fedora下安装“pinentry-gui”的命令是:

[root@midstage ~]# yum install pinentry-gui

在Ubuntu/Debian下安装“pinentry-gui”的命令是:

[root@midstage ~]# apt-get install pinentry-gui

创建一个脚本“Encrypt.sh”,将下面的代码复制进去。你也可以从这里下载这个脚本。代码如下:

  1. #!/bin/bash  
  2. echo "Welcome, I am ready to encrypt a file/folder for you"  
  3. echo "currently I have a limitation, Place me to the same folder, 
  4. where a file to be encrypted is present"  
  5. echo "Enter the Exact File Name with extension"  
  6. read file;  
  7. gpg -c $file  
  8. echo "I have encrypted the file sucessfully..."  
  9. echo "Now I will be removing the original file"  
  10. rm -rf $file  

输出结果,代码如下:

  1. [root@tecmint ~]# chmod 755 Encrypt.sh  
  2. [root@tecmint ~]# ./Encrypt.sh  
  3.    
  4. Welcome, I am ready to encrypt a file/folder for you  
  5. currently I have a limitation, Place me to the same folder,  
  6. where a file to be encrypted is present  
  7. Enter the Exact File Name with extension  
  8.    
  9. package.xml  
  10.    
  11.                    Enter passphrase  
  12.    
  13.                    Passphrase _________________________________  
  14.    
  15.    
  16.                    Please re-enter this passphrase  
  17.    
  18.                    Passphrase _________________________________  
  19.    
  20.    
  21. I have encrypted the file successfully...  
  22. Now I will be removing the original file  

代码说明:

gpg -c: 这个命令使用aka来加密文件。 在你需要的时候,你需要对加密的文件进行解密。这里我们不给出具体的代码了,你可以自己尝试着写出来。提示:使用命令 gpg -d filename.gpg > filename 可以解密一份文件。

例子四:查看服务器利用率

查看服务器的利用率是管理员的一份重要的日常工作。聪明的管理员是知道如何是这份任务自动化的。下面的这份脚本会抓取服务器的很多信息,快快试试吧!代码如下:

  1. #!/bin/bash  
  2. date;  
  3. echo "uptime:"  
  4. uptime  
  5. echo "Currently connected:"  
  6. w  
  7. echo "--------------------"  
  8. echo "Last logins:"  
  9. last -a |head -3  
  10. echo "--------------------"  
  11. echo "Disk and memory usage:"  
  12. df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'  
  13. free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}'  
  14. echo "--------------------"  
  15. start_log=`head -1 /var/log/messages |cut -c 1-12`  
  16. oom=`grep -ci kill /var/log/messages`  
  17. echo -n "OOM errors since $start_log :" $oom  
  18. echo ""  
  19. echo "--------------------"  
  20. echo "Utilization and most expensive processes:"  
  21. top -b |head -3  
  22. echo  
  23. top -b |head -10 |tail -4  
  24. echo "--------------------"  
  25. echo "Open TCP ports:"  
  26. nmap -p- -T4 127.0.0.1  
  27. echo "--------------------"  
  28. echo "Current connections:"  
  29. ss -s  
  30. echo "--------------------"  
  31. echo "processes:"  
  32. ps auxf --width=200  
  33. echo "--------------------"  
  34. echo "vmstat:"  
  35. vmstat 1 5  

输出结果,代码如下:

  1. [root@tecmint ~]# chmod 755 Server-Health.sh  
  2. [root@tecmint ~]# ./Server-Health.sh  
  3.    
  4. Tue Jul 16 22:01:06 IST 2013  
  5. uptime:  
  6. 22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18  
  7. Currently connected:  
  8. 22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18  
  9. USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT  
  10. tecmint pts/0 116.72.134.162 21:48 0.00s 0.03s 0.03s sshd: tecmint [priv]  
  11. --------------------  
  12. Last logins:  
  13. tecmint pts/0 Tue Jul 16 21:48 still logged in 116.72.134.162  
  14. tecmint pts/0 Tue Jul 16 21:24 - 21:43 (00:19) 116.72.134.162  
  15. --------------------  
  16. Disk and memory usage:  
  17. Free/total disk: 292G / 457G  
  18. Free/total memory: 3510 / 3838 MB  
  19. --------------------  
  20. OOM errors since Jul 14 03:37 : 0  
  21. --------------------  
  22. Utilization and most expensive processes:  
  23. top - 22:01:07 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18  
  24. Tasks: 149 total, 1 running, 148 sleeping, 0 stopped, 0 zombie  
  25. Cpu(s): 0.1%us, 0.0%sy, 0.0%ni, 99.3%id, 0.6%wa, 0.0%hi, 0.0%si, 0.0%st  
  26.    
  27. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  
  28. 1 root 20 0 3788 1128 932 S 0.0 0.0 0:32.94 init  
  29. 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd  
  30. 3 root RT 0 0 0 0 S 0.0 0.0 0:14.07 migration/0  

例子五:查看硬盘使用情况及发送提示邮件

下面的这个例子展示了当硬盘的使用空间超出了预期设定的值时,如果通过脚本来发送提示邮件。代码如下:

  1. MAX=95  
  2.  EMAIL=server@127.0.0.1  
  3.  PART=sda1  
  4.     
  5.  USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`  
  6.  if [ $USE -gt $MAX ]; then  
  7.  echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL  
  8.  fi  

说明:将上述脚本中的“USER”替换成你的用户名。你可以通过命令“mail”来查看你的邮件。

标签:

给我留言