linux 编程例子,Linux Shell脚本编程的五个经典例子【新手学习】

例子一:绘制特殊图形

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

输出结果:

772e34b9663ea68b7dd7122258406bd3.png

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

你可以从这里下载这个例子的代码。

例子三:加密文件/目录

下面的例子演示了如何加密一个份文件或者文件夹。目前的这个版本的脚本有一些局限,例如你必须把它和你要加密的文件/目录放到同一个文件夹下面。另外,你可能需要安装“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”来查看你的邮件。

通过这五个例子,我们可以掌握到shell编程中的不少基础。

  • 0
    点赞
  • 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).

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值