网站搜索

供 Linux 新手学习 Shell 编程的 5 个 Shell 脚本 - 第二部分


要学习某件事,你需要去做,而不用担心不成功。我相信实用性,因此将陪伴您进入脚本语言的实用世界。

本文是我们第一篇文章《了解 Linux Shell 和基本 Shell 脚本编写 – 第一部分》的扩展,在这篇文章中我们让您体验了脚本编写,并且我们不会让您失望。

脚本 1:绘制特殊图案

#!/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<=MAX_NO; i++ )) do     for (( s=MAX_NO; s>=i; s-- ))
    do
       echo -n " "
    done
    for (( j=1; j<=i;  j++ ))     do      echo -n " ."      done     echo "" done ###### Second stage ###################### for (( i=MAX_NO; i>=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, linux-console.net 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, linux-console.net is always there

如果你对任何编程语言有一点了解,学习上面的脚本并不困难,即使你是计算、编程和 Linux 的新手,也不会太困难。

下载Special_Pattern.sh

脚本 2:创建彩色脚本

谁说Linux是无色且无聊的,将下面的代码保存到任何[] sh,使其可执行并运行它,不要'不要忘记告诉我这是怎么回事,想想你能实现什么,在某个地方实现它。

#!/bin/bash
clear 
echo -e "33[1m Hello World"
bold effect
echo -e "33[5m Blink"
blink effect
echo -e "33[0m Hello World"
back to normal
echo -e "33[31m Hello World"
Red color
echo -e "33[32m Hello World"
Green color
echo -e "33[33m Hello World"
See remaining on screen
echo -e "33[34m Hello World"
echo -e "33[35m Hello World"
echo -e "33[36m Hello World"
echo -e -n "33[0m"
back to normal
echo -e "33[41m Hello World"
echo -e "33[42m Hello World"
echo -e "33[43m Hello World"
echo -e "33[44m Hello World"
echo -e "33[45m Hello World"
echo -e "33[46m Hello World"
echo -e "33[0m Hello World"

注意:现在不用担心颜色代码,那些对你重要的东西会逐渐出现在你的舌尖上。

警告:您的终端可能没有闪烁功能。

样本输出
[root@tecmint ~]# chmod 755 Colorfull.sh
[root@tecmint ~]# ./Colorfull.sh

Hello World
Blink
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

下载多彩l.sh

脚本 3:加密文件/目录

该脚本将加密文件(还记得吗?目录/驱动程序/…。在Linux中,所有内容都被视为文件>)。上述脚本当前的限制是它不支持使用 TAB 自动完成名称。另外,需要将需要加密的脚本和文件放在同一个文件夹中。如果需要,您可能需要使用 yumapt 软件包安装“pinentry-gui”。

[root@midstage ~]# yum install 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 thh 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 successfully..."
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 *******_________________________________ │
                                                   │                                                     │
                                                   │       <OK>                             <Cancel>     │
                                                   └─────────────────────────────────────────────────────┘

Please re-enter this passphrase

                                                   ┌─────────────────────────────────────────────────────┐
                                                   │ Please re-enter this passphrase                     │
                                                   │                                                     │
                                                   │ Passphrase ********________________________________ │
                                                   │                                                     │
                                                   │       <OK>                             <Cancel>     │
                                                   └─────────────────────────────────────────────────────┘

I have encrypted the file successfully...
Now I will be removing the original file
</pre>

gpg -c :这将使用密码(又名密码)加密您的文件。在这个学习的过程中你绝对不会想到实际的学习过程会这么简单。那么加密文件后你需要什么?明显地!解密文件。我希望你——学习者、读者自己编写解密脚本,别担心我不会把你留在中间,我只是想让你从这篇文章中有所收获。

注意gpg -d filename.gpg > filename 是您需要在解密脚本中实现的内容。如果成功你可以在评论中发布你的脚本,如果不成功你可以让我帮你写。

下载Encrypt.sh

脚本 4:检查服务器利用率

检查服务器利用率是管理员的重要任务之一,好的管理员是知道如何自动化日常任务的管理员。下面的脚本将提供有关您的服务器的许多此类信息。你自己检查一下。

#!/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

注意:我已经为您提供了在终端本身中提供输出的脚本,如何将输出存储在文件中以供将来参考。使用重定向运算符来实现它。

  1. >‘:重定向运算符会导致文件创建,如果存在,内容将被覆盖。
  2. >>‘:当你使用>>时,你是在添加信息,而不是替换它。
  3. 与“>”相比,“>>”是安全的

下载服务器-Health.sh

脚本 5:检查磁盘空间并发送电子邮件警报

当分区 PART 中的磁盘使用量大于允许的最大值时收到一封电子邮件怎么样,它是 Web 管理员的救星脚本,几乎不需要修改。

MAX=95
[email 
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”命令检查邮件。

下载检查磁盘空间.sh

脚本编写和编程是没有界限的,任何事情都可以根据需要实现。现在就这些了,在我的下一篇文章中,我将为您提供一些不同风格的脚本编写。在那之前保持冷静并调整,享受。