欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2023-11-22 10:24:58
例子一:绘制特殊图形 复制代码 代码如下: #!/bin/bash   max_no=0   echo -n "enter number b...

例子一:绘制特殊图形

复制代码 代码如下:

#!/bin/bash
 
max_no=0
 
echo -n "enter number between (5 to 9) : "
 read max_no
 
if ! [ $max_no -ge 5 -a $max_no -le 9 ] ; then
 echo "wtf... i ask to enter number between 5 and 9, try again"
 exit 1
 fi
 
clear
 
for (( i=1; i=i; s-- ))
 do
 echo -n " "
 done
 for (( j=1; j=1; i-- ))
 do
 for (( s=i; s<=max_no; s++ ))
 do
 echo -n " "
 done
 for (( j=1; j<=i; j++ ))
 do
 echo -n " ."
 done
 echo ""
 done
 
echo -e "\n\n\t\t\t whenever you need help, tecmint.com is always there"

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

输出结果:

复制代码 代码如下:

[root@tecmint ~]# chmod 755 special_pattern.sh
[root@tecmint ~]# ./special_pattern.sh
enter number between (5 to 9) : 6
       .
      . .
     . . .
    . . . .
   . . . . .
  . . . . . .
  . . . . . .
   . . . . .
    . . . .
     . . .
      . .
       .
 
        whenever you need help, tecmint.com is always there

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

例子二:五颜六色的脚本

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

复制代码 代码如下:

#!/bin/bash
 
clear
echo -e "\033[1m hello world"
 # bold effect
echo -e "\033[5m blink"
       # blink effect
echo -e "\033[0m hello world"
 # back to noraml
 
echo -e "\033[31m hello world"
 # red color
echo -e "\033[32m hello world"
 # green color
echo -e "\033[33m hello world"
 # see remaing on screen
echo -e "\033[34m hello world"
echo -e "\033[35m hello world"
echo -e "\033[36m hello world"
 
echo -e -n "\033[0m"
  # back to noraml
echo -e "\033[41m hello world"
echo -e "\033[42m hello world"
echo -e "\033[43m hello world"
echo -e "\033[44m hello world"
echo -e "\033[45m hello world"
echo -e "\033[46m hello world"
 
echo -e "\033[0m hello world"

输出结果:

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

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

例子三:加密文件/目录

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

复制代码 代码如下:
[root@midstage ~]# yum install pinentry-gui

在ubuntu/debian下安装“pinentry-gui”的命令是:
复制代码 代码如下:
[root@midstage ~]# apt-get install pinentry-gui

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

#!/bin/bash
echo "welcome, i am ready to encrypt a file/folder for you"
echo "currently i have a limitation, place me to the same folder,
where a file to be encrypted is present"
echo "enter the exact file name with extension"
read file;
gpg -c $file
echo "i have encrypted the file sucessfully..."
echo "now i will be removing the original file"
rm -rf $file

输出结果:
复制代码 代码如下:

[root@tecmint ~]# chmod 755 encrypt.sh
[root@tecmint ~]# ./encrypt.sh
 
welcome, i am ready to encrypt a file/folder for you
currently i have a limitation, place me to the same folder,
where a file to be encrypted is present
enter the exact file name with extension
 
package.xml
 
                   enter passphrase
 
                   passphrase _________________________________
 
 
                   please re-enter this passphrase
 
                   passphrase _________________________________
 
 
i have encrypted the file successfully...
now i will be removing the original file

代码说明:

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

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

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

复制代码 代码如下:

#!/bin/bash
date;
echo "uptime:"
uptime
echo "currently connected:"
w
echo "--------------------"
echo "last logins:"
last -a |head -3
echo "--------------------"
echo "disk and memory usage:"
df -h | xargs | awk '{print "free/total disk: " $11 " / " $9}'
free -m | xargs | awk '{print "free/total memory: " $17 " / " $8 " mb"}'
echo "--------------------"
start_log=`head -1 /var/log/messages |cut -c 1-12`
oom=`grep -ci kill /var/log/messages`
echo -n "oom errors since $start_log :" $oom
echo ""
echo "--------------------"
echo "utilization and most expensive processes:"
top -b |head -3
echo
top -b |head -10 |tail -4
echo "--------------------"
echo "open tcp ports:"
nmap -p- -t4 127.0.0.1
echo "--------------------"
echo "current connections:"
ss -s
echo "--------------------"
echo "processes:"
ps auxf --width=200
echo "--------------------"
echo "vmstat:"
vmstat 1 5

输出结果:
复制代码 代码如下:

[root@tecmint ~]# chmod 755 server-health.sh
[root@tecmint ~]# ./server-health.sh
 
tue jul 16 22:01:06 ist 2013
uptime:
22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18
currently connected:
22:01:06 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18
user tty from login@ idle jcpu pcpu what
tecmint pts/0 116.72.134.162 21:48 0.00s 0.03s 0.03s sshd: tecmint [priv]
--------------------
last logins:
tecmint pts/0 tue jul 16 21:48 still logged in 116.72.134.162
tecmint pts/0 tue jul 16 21:24 - 21:43 (00:19) 116.72.134.162
--------------------
disk and memory usage:
free/total disk: 292g / 457g
free/total memory: 3510 / 3838 mb
--------------------
oom errors since jul 14 03:37 : 0
--------------------
utilization and most expensive processes:
top - 22:01:07 up 174 days, 4:42, 1 user, load average: 0.36, 0.25, 0.18
tasks: 149 total, 1 running, 148 sleeping, 0 stopped, 0 zombie
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
 
pid user pr ni virt res shr s %cpu %mem time+ command
1 root 20 0 3788 1128 932 s 0.0 0.0 0:32.94 init
2 root 20 0 0 0 0 s 0.0 0.0 0:00.00 kthreadd
3 root rt 0 0 0 0 s 0.0 0.0 0:14.07 migration/0

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

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

复制代码 代码如下:
max=95
email=server@127.0.0.1
part=sda1
 
use=`df -h |grep $part | awk '{ print $5 }' | cut -d'%' -f1`
if [ $use -gt $max ]; then
echo "percent used: $use" | mail -s "running out of disk space" $email
fi

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