今天分享给大家的是Linux脚本编程的具体例子,就算你是新手(小白),也照样可以很熟练的掌握这些代码。

列举Linux Shell脚本基础编程的案例

例子一:绘制特殊图形

#!/bin/bash

MAX_NO=0

echo-n"EnterNumberbetween(5to9):"

readMAX_NO

if![$MAX_NO-ge5-a$MAX_NO-le9];then

echo"WTF...Iasktoenternumberbetween5and9,TryAgain"

exit1

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"nntttWheneveryouneedhelp,Tecmint.comisalwaysthere"

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

输出结果:

[root@tecmint~]#chmod755Special_Pattern.sh

[root@tecmint~]#./Special_Pattern.sh

EnterNumberbetween(5to9):6

.

..

...

....

.....

......

......

.....

....

...

..

.

例子二:加密文件/目录

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

[root@midstage~]#yuminstallpinentry-gui

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

[root@midstage~]#apt-getinstallpinentry-gui

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

#!/bin/bash

echo"Welcome,Iamreadytoencryptafile/folderforyou"

echo"currentlyIhavealimitation,Placemetothesamefolder,

whereafiletobeencryptedispresent"

echo"EntertheExactFileNamewithextension"

readfile;

gpg-c$file

echo"Ihaveencryptedthefilesucessfully..."

echo"NowIwillberemovingtheoriginalfile"

rm-rf$file

输出结果:

[root@tecmint~]#chmod755Encrypt.sh

[root@tecmint~]#./Encrypt.sh

Welcome,Iamreadytoencryptafile/folderforyou

currentlyIhavealimitation,Placemetothesamefolder,

whereafiletobeencryptedispresent

EntertheExactFileNamewithextension

package.xml

Enterpassphrase

Passphrase_________________________________

Pleasere-enterthispassphrase

Passphrase_________________________________

Ihaveencryptedthefilesuccessfully...

NowIwillberemovingtheoriginalfile

代码说明:

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

列举Linux Shell脚本基础编程的案例

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

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

#!/bin/bash

date;

echo"uptime:"

uptime

echo"Currentlyconnected:"

w

echo"--------------------"

echo"Lastlogins:"

last-a|head-3

echo"--------------------"

echo"Diskandmemoryusage:"

df-h|xargs|awk'{print"Free/totaldisk:"$11"/"$9}'

free-m|xargs|awk'{print"Free/totalmemory:"$17"/"$8"MB"}'

echo"--------------------"

start_log=`head-1/var/log/messages|cut-c1-12`

oom=`grep-cikill/var/log/messages`

echo-n"OOMerrorssince$start_log:"$oom

echo""

echo"--------------------"

echo"Utilizationandmostexpensiveprocesses:"

top-b|head-3

echo

top-b|head-10|tail-4

echo"--------------------"

echo"OpenTCPports:"

nmap-p--T4127.0.0.1

echo"--------------------"

echo"Currentconnections:"

ss-s

echo"--------------------"

echo"processes:"

psauxf--width=200

echo"--------------------"

echo"vmstat:"

vmstat15

输出结果:

[root@tecmint~]#chmod755Server-Health.sh

[root@tecmint~]#./Server-Health.sh

TueJul1622:01:06IST2013

uptime:

22:01:06up174days,4:42,1user,loadaverage:0.36,0.25,0.18

Currentlyconnected:

22:01:06up174days,4:42,1user,loadaverage:0.36,0.25,0.18

USERTTYFROMLOGIN@IDLEJCPUPCPUWHAT

tecmintpts/0116.72.134.16221:480.00s0.03s0.03ssshd:tecmint[priv]

--------------------

Lastlogins:

tecmintpts/0TueJul1621:48stillloggedin116.72.134.162

tecmintpts/0TueJul1621:24-21:43(00:19)116.72.134.162

--------------------

Diskandmemoryusage:

Free/totaldisk:292G/457G

Free/totalmemory:3510/3838MB

--------------------

OOMerrorssinceJul1403:37:0

--------------------

Utilizationandmostexpensiveprocesses:

top-22:01:07up174days,4:42,1user,loadaverage:0.36,0.25,0.18

Tasks:149total,1running,148sleeping,0stopped,0zombie

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

PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND

1root20037881128932S0.00.00:32.94init

2root200000S0.00.00:00.00kthreadd

3rootRT0000S0.00.00:14.07migration/0

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

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

MAX=95

[email protected]

PART=sda1

USE=`df-h|grep$PART|awk'{print$5}'|cut-d'%'-f1`

if[$USE-gt$MAX];then

echo"Percentused:$USE"|mail-s"Runningoutofdiskspace"$EMAIL

fi

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

以上就是通过四个案例具体对Linux脚本编程做出的详解,欢迎大家分享学习。