几个Shell脚本的例子

【例子:001】判断输入为数字,字符或其他

#!/bin/bash
read -p "Enter a number or string here:" input 

case $input in
[0-9]) echo -e "Good job, Your input is a numberic! \n" ;;
[a-zA-Z]) echo -e "Good job, Your input is a character! \n" ;;
*) echo -e "Your input is wrong, input again! \n" ;;
esac 

【例子:002】求平均数

#!/bin/bash 

# Calculate the average of a series of numbers. 

SCORE="0"
AVERAGE="0"
SUM="0"
NUM="0" 

while true; do 

echo -n "Enter your score [0-100%] (‘q‘ for quit): "; read SCORE; 

if (("$SCORE" < "0")) || (("$SCORE" > "100")); then
echo "Be serious. Common, try again: "
elif [ "$SCORE" == "q" ]; then
echo "Average rating: $AVERAGE%."
break
else
SUM=$[$SUM + $SCORE]
NUM=$[$NUM + 1]
AVERAGE=$[$SUM / $NUM]
fi 

done 

echo "Exiting." 

【例子:003】自减输出

[scriptname: doit.sh]
while (( $# > 0 ))
do
echo $*
shift
done 

/> ./doit.sh a b c d e
a b c d e
b c d e
c d e
d e
e 

【例子:004】在文件中添加前缀

# 人名列表
# cat namelist
Jame
Bob
Tom
Jerry
Sherry
Alice
John

# 脚本程序
# cat namelist.sh
#!/bin/bash
for name in $(cat namelist)
do
echo "name= " $name
done
echo "The name is out of namelist file"

# 输出结果
# ./namelist.sh
name= Jame
name= Bob
name= Tom
name= Jerry
name= Sherry
name= Alice
name= John
【例子:005】批量测试文件是否存在
[[email protected] ~]# cat testfile.sh
#!/bin/bash

for file in test*.sh
do
if [ -f $file ];then
echo "$file existed."
fi
done

[[email protected] ~]# ./testfile.sh
test.sh existed.
test1.sh existed.
test2.sh existed.
test3.sh existed.
test4.sh existed.
test5.sh existed.
test78.sh existed.
test_dev_null.sh existed.
testfile.sh existed.
【例子:005】用指定大小文件填充硬盘
[[email protected] ~]# df -ih /tmp
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg00-lvol5
1000K 3.8K 997K 1% /tmp
[[email protected] ~]# cat cover_disk.sh
#!/bin/env bash
counter=0
max=3800
remainder=0
while true
do
((counter=counter+1))
if [ ${#counter} -gt $max ];then
break
fi
((remainder=counter%1000))
if [ $remainder -eq 0 ];then
echo -e "counter=$counter\tdate=" $(date)
fi
mkdir -p /tmp/temp
cat < testfile > "/tmp/temp/myfile.$counter"
if [ $? -ne 0 ];then
echo "Failed to write file to Disk."
exit 1
fi
done
echo "Done!"
[[email protected] ~]# ./cover_disk.sh
counter=1000 date= Wed Sep 10 09:20:39 HKT 2014
counter=2000 date= Wed Sep 10 09:20:48 HKT 2014
counter=3000 date= Wed Sep 10 09:20:56 HKT 2014
cat: write error: No space left on device
Failed to write file to Disk.
dd if=/dev/zero of=testfile bs=1M count=1
【例子:006】通过遍历的方法读取配置文件
[[email protected] ~]# cat hosts.allow
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
127.0.0.6
127.0.0.7
127.0.0.8
127.0.0.9
[[email protected] ~]# cat readlines.sh
#!/bin/env bash
i=0
while read LINE;do
hosts_allow[$i]=$LINE
((i++))
done < hosts.allow
for ((i=1;i<=${#hosts_allow[@]};i++)); do
echo ${hosts_allow[$i]}
done
echo "Done"
[[email protected] ~]# ./readlines.sh
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
127.0.0.6
127.0.0.7
127.0.0.8
127.0.0.9
Done
【例子:007】简单正则表达式应用
[[email protected] ~]# cat regex.sh
#!/bin/env sh
#Filename: regex.sh
regex="[A-Za-z0-9]{6}"
if [[ $1 =~ $regex ]]
then
num=$1
echo $num
else
echo "Invalid entry"
exit 1
fi
[[email protected] ~]# ./regex.sh 123abc
123abc

#!/bin/env bash
#Filename: validint.sh
validint(){
ret=`echo $1 | awk ‘{start = match($1,/^-?[0-9]+$/);if (start == 0) print "1";else print "0"}‘`
return $ret
}

validint $1

if [ $? -ne 0 ]; then
echo "Wrong Entry"
exit 1
else
echo "OK! Input number is:" $1
fi
【例子:008】简单的按日期备份文件
#!/bin/bash

NOW=$(date +"%m-%d-%Y") # 当前日期
FILE="backup.$NOW.tar.gz" # 备份文件
echo "Backing up data to /tmp/backup.$NOW.tar.gz file, please wait..." #打印信息
tar xcvf /tmp/backup.$NOW.tar.gz /home/ /etc/ /var # 同时备份多个文件到指定的tar压缩文件中
echo "Done..."

【例子:009】交互式环境select的使用
#!/bin/bash

echo "What is your favorite OS?"

select OS in "Windows" "Linux/Unix" "Mac OS" "Other"
do
break
done

echo "You have selected $OS"

[email protected]:~/training# ./select.sh
What is your favorite OS?
1) Windows
2) Linux/Unix
3) Mac OS
4) Other
#? 1
You have selected Windows
【例子:010】批量修改文件名的脚本
#!/bin/bash
# we have less than 3 arguments. Print the help text:
if [ $# -lt 3 ]; then
cat <<-EOF
ren -- renames a number of files using sed regular expressions
USAGE: ren.sh ‘regexp‘ ‘replacement‘ files
EXAMPLE: rename all *.HTM files in *.html:
ren ‘HTM$‘ ‘html‘ *.HTM
EOF
exit 0
fi
OLD="$1"
NEW="$2"
# The shift command removes one argument from the list of
# command line arguments.
shift
shift
# $* contains now all the files:
for file in $*
do
if [ -f "$file" ]; then
newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
if [ -f "$newfile" ]; then
echo "ERROR: $newfile exists already"
else
echo "renaming $file to $newfile "
mv "$file" "$newfile"
fi
fi
done
[email protected]:~/training# ./ren.sh "HTML$" "html" file*.HTML
renaming file10.HTML to file10.html
renaming file1.HTML to file1.html
renaming file2.HTML to file2.html
renaming file3.HTML to file3.html
renaming file4.HTML to file4.html
renaming file5.HTML to file5.html
renaming file6.HTML to file6.html
renaming file7.HTML to file7.html
renaming file8.HTML to file8.html
renaming file9.HTML to file9.html
【例子:011】break语句在脚本中的应用示例
#!/bin/bash

for VAR1 in 1 2 3
do
for VAR2 in 0 5
do
if [ $VAR1 -eq 2 -a $VAR2 -eq 0 ]
then
break 2 # 退出第二重循环,亦即退出整个循环
else
echo "第一个变量:$VAR1 第二个变量:$VAR2"
fi
done
done
[email protected]:~/training# ./test.sh
第一个变量:1 第二个变量:0
第一个变量:1 第二个变量:5

【例子:012】/dev/tty在读取人工输入中的特殊作用
#!/bin/bash
# 用来验证两次输入的密码是否一致

printf "Enter your passwd: " # 提示输入
stty -echo # 关闭自动打印输入字符的功能
read pwd1 < /dev/tty # 读取密码
printf "\nEnter again: " # 再次提示输入
read pwd2 < /dev/tty # 再读取一次以确认
stty echo # 打开自动打印输入字符的功能

if [[ "$pwd1" == "$pwd2" ]]; then # 对两次输入的密码进行判断
echo -e "\nPASSWORD: the same"
else
echo -e "\nPASSWORD: not same"
fi
[email protected]:~/training# ./test.sh
Enter your passwd:
Enter again:
PASSWORD: the same

【例子:013】/dev/null在脚本中的简单示例
#!/bin/bash

if grep /bin/bash $0 > /dev/null 2>&1 # 只关心命令的退出状态而不管其输出
then # 对退出状态进行判断
echo -e "/bin/bash in $0\n"
else
echo -e "/bin/bash not in $0\n"
fi
脚本输出:
[email protected]:~/training# ./test.sh
/bin/bash in ./test.sh

【例子:014】构建自己的bin目录存放执行脚本,然后随便执行的简单示例
$ cd # <span style="font-family:FangSong_GB2312;">进入家目录</span>
$ mkdir bin <span style="font-family:FangSong_GB2312;"># 创建$HOME目录下自己的bin目录</span>
$ mv test.sh bin # 将我们自己的脚本放到创建的bin目录下
<span style="font-family:FangSong_GB2312;">$ </span>PATH=$PATH:$HOME/bin # 将个人的bin目录放到PATH<span style="font-family:FangSong_GB2312;">中

$ test.sh # 现在就可以直接执行自己的脚本了</span>

【例子:015】将长句子中单词长度为5及以上的单词打印出来
#!/bin/bash
# filename: test.sh

sentence="When you‘re attracted to someone it just means that your subconscious is attracted to their subconscious, subconsciously.
So what we think of as fate, is just two neuroses knowing they‘re a perfect match."

for word in ${sentence}
do
new=`echo $word | tr -cd ‘[a-zA-Z]‘` # 去除句子中的 ,或者‘
len=${#new} # 求长度
if [ "$len" -ge 5 ] # 再判断
then
echo $new
fi
done
[email protected]:~# ./test.sh
youre
attracted
someone
means
subconscious
attracted
their
subconscious
subconsciously
think
neuroses
knowing
theyre
perfect
match
【例子:016】根据输入的数据(年4位,月2位),来判断上个月天数
#!/bin/bash

get_last_day()
{
year=`expr substr $1 1 4`
month=`expr substr $1 5 2`
curr_month=`echo $month | tr -d ‘0‘` # 去掉里面的0,方便后面计算
echo "curr_month=$curr_month"
last_month=`expr $curr_month - 1`
case $last_month in
01|03|05|07|08|10|12|0)
echo "上个月天数-->" 31 ;;
02)
if [ `expr $year % 400` = 0 ] ; then
echo "上个月天数-->" 29
elif [ `expr $year % 4` = 0 ] && [ `expr $year % 100` != 0 ] ; then
echo "上个月天数-->" 29
else
echo "上个月天数-->" 28
fi ;;
*)
echo "上个月天数-->" 30
esac
}

if [ $# -ne 1 ]; then
echo "Usage: $0 201608"
else
get_last_day $1
fi

[email protected]:~/training# ./test.sh 201601
上个月天数--> 31
【例子:017】统计文件中每个单词出现的频率
#!/bin/sh
# 从标准输入读取文件流,再输出出现频率的前n,默认:25个单词的列表
# 附上出现频率的计数,按照这个计数由大到小排列
# 输出到标准输出
# 语法: wf [n]

tr -cs A-Za-z\‘ ‘\n‘ |
tr A-Z a-z |
sort |
uniq -c |
sort -k1,1nr -k2 |
sed ${1:-25}q
[email protected]:~/training# wf 10 < /etc/hosts | pr -c4 -t -w80
6 ip 1 1 archive 1 capable
3 ff 1 allnodes 1 are 1 cn
2 localhost 1 allrouters
【例子:018】使用while和break等待用户登录
#!/bin/bash
# 等待特定用户登录,每30秒确认一次
# filename: wait_for_user_login.sh

read -p "Ener username:-> " user
while true
do
if who | grep "$user" > /dev/null
then
echo "The $user now logged in."
break
else
sleep 30
fi
done
[email protected]:~/shell# ./wait_for_user_login.sh
Ener username:-> guest
The guest now logged in.
【例子:019】结合while,case,break,shift做简单的选项处理
#!/bin/bash

# 将标志变量设置为空值
file= verbose= quiet= long=

while [ $# -gt 0 ] # 执行循环直到没有参数为止
do
case $1 in # 检查第一个参数
-f) file=$2
shift ;; # 移位-f,使得结尾shift得到$2的值
-v) verbose=true
quiet= ;;
-q) quiet=true
verbose= ;;
-l) long=true ;;
--) shift
break ;;
-*) echo "$0: $1: unrecongnized option >&2" ;;
*) break ;;
esac
done
~
【例子:020】read读取多个变量处理,及文本遍历的两种常用方式
#!/bin/bash

while IFS=: read user pwd pid gid fullname homedir shell # IFS作为列之间的分隔符号,read读取多个变量
do
printf "The user=%s homedir=%s\n" "$user" "$homedir" # 对文本中的行进行处理
done < /etc/passwd # 读取文件

# 第二种方式
#!/bin/bash

cat /etc/passwd |
while IFS=: read user pwd pid gid fullname homedir shell
do
printf "The user=%s homedir=%s\n" "$user" "$homedir"
done
【例子:021】复制目录树的两个简单脚本
#!/bin/bash
# 方式一
find /root/shell -type d -print | # 寻找所有目录
sed ‘s;/root/shell/;/tmp/shell/;‘ | # 更改名称,使用;作为定界符
sed ‘s/^/mkdir -p /‘ | # 插入mkdir -p 命令
sh -x # 以Shell的跟踪模式执行

# 方式二
find /root/shell -type d -print | # 寻找所有目录
sed ‘s;/root/shell/;/tmp/shell/;‘ | # 更改名称,使用;作为定界符
while read newdir # 读取新的目录名
do
mkdir -p $newdir
done
~
【例子:022】发邮件给系统前10名磁盘用户,要求清理磁盘空间
#!/bin/bash

cd /home # 移动到目录的顶端
du -s * | # 产生原始磁盘用量
sort -nr | # 以数字排序,最高的在第一位
sed 10q | # 在前10行之后就停止
while read amount name # 将读取的数据分别作为amount, name变量
do
mail -s "disk usage warning" $name << EOF
Gretings. You are one of the top 10 consumers of disk space
on the system. Your home directory users $amount disk blocks.

Please clean up unneeded files, as soon as possible.

Thanks,
Your friendly neighborhood system administrator.
EOF
done
【例子:023】将密码文件转换为Shell邮寄列表
#!/bin/bash

# passwd-to-mailing-list
#
# 产生使用特定shell的所有用户邮寄列表
#
# 语法: passwd-to-mailing-list < /etc/passwd

# 删除临时性文件
rm -rf /tmp/*.mailing-list

# 从标准输入中读取
while IFS=: read user passwd uid gid name home Shell
do
Shell=${Shell:-/bin/sh} # 如为空shell,指/bin/sh
file="/tmp/$(echo $Shell | sed -e ‘s;^/;;‘ -e ‘s;/;-;g‘).mailing-list"
echo $user, >> $file
done
[email protected]:~# vim passwd-to-mailing-list
[email protected]:~# passwd-to-mailing-list < /etc/passwd
[email protected]:~# cat /tmp/bin-bash.mailing-list
root,
test,
user,
[email protected]:~# cat /tmp/bin-sh.mailing-list
libuuid,
jerry,
【例子:024】变更目录时更新PS1
#!/bin/bash

cd()
{
command cd "[email protected]" # 实际改变目录
x=$(pwd) # 取得当前目录的名称,传递给变量
PS1="${x##*/}\$ " # 截断前面的组成部分,指定给PS1
}
root$ # 最后输出,类似于这种,看不到目录的完整路径
【例子:025】根据XML文件中的license时间来判断是否过期
<?xml version="1.0" encoding="gb2312"?>
<license>
<pos>中国,福建,福州市,鼓楼区</pos>
<installid>123123</installid>
<device>hdsas_base_3.0.0.2_16Q2_RC2</device>
<id>_RC257971fe611f0</id>
<hwid>f04c3d1eb4bf6113</hwid>
<issuetime>2016-08-02 16:46:39</issuetime>
<expired>30 days</expired>
</license>

获得<issuetime>2016-08-02 16:46:39</issuetime>时间加上<expired>30 days</expired>
期限,得到时间减去系统当前时间,小于7天,显示license即将在几天后过期。
代码如下:
#!/bin/bash

CURR_TIME=$(date +‘%Y%m%d‘)
FILE_TIME=$(grep ‘issuetime‘ hdlicense.xml | tr -d ‘[\-a-z<>/]‘ | awk ‘{print $1}‘)
REAL_TIME=$(date -d "$FILE_TIME +30 days" +%Y%m%d)

d1=$(date "+%s" -d "$REAL_TIME")
d2=$(date "+%s" -d "$CURR_TIME")

EXPI_TIME=$(((d1-d2)/86400))

if [ "$EXPI_TIME" -lt "7" ]; then
echo "你的license将在 $EXPI_TIME 天后过期!"
fi
【例子:026】根据参数来判断是否要新创建目录
#!/bin/bash

DIR=$1

if [ X"$DIR" = X"" ]; then
echo "Usage: `basename $0` directory to create" >&2
exit 1
fi

if [ -d $DIR ];then
echo "The directory you create is exist."
exit 0
else
echo "The $DIR does not exist, will create now."
echo -n "Create it now? [y/n]"
read ANS
if [ X"$ANS" = X"y" -o X"$ANS" = X"Y" ];then
mkdir $DIR > /dev/null 2>&1
if [ $? !=0 ]; then
echo "Error creating the direcory $DIR" >&2
exit 1
else
echo "Create $DIR OK"
exit 0
fi
fi
fi
【例子:027】创建新目录,并将当前目录下的所有.txt文件拷贝到新目录中
#!/bin/bash

DIR=testdir
THERE=`pwd`

mkdir $DIR > /dev/null 2>&1

if [ -d $DIR ]; then
cd $DIR
if [ $? = 0 ]; then
HERE=`pwd`
cp $THERE/*.txt $HERE
else
echo "Cannot cd to $DIR"
exit 1
fi
else
echo "Cannot create directory $THERE"
exit 1
fi
【例子:028】菜单显示小脚本
#!/bin/bash

USER=`whoami`
HOST=`hostname -s`
DATE=`date ‘+%d/%m/%Y‘`

help(){
cat <<EOF
-----------------------------------------------------
User: $USER Host: $HOST Date: $DATE
-----------------------------------------------------
1. List files in current directory
2. Use the vi editor
3. See who is on the system
H. Help screen
Q. Exit Menu
-----------------------------------------------------
Your Choice [1, 2, 3, 4, H, Q] >
EOF
}

while :
do
help
echo -n "Enter your choice: "
read ANS
case $ANS in
1) ls -lart ;;
2) vi ;;
3) who ;;
H) help ;;
Q) exit 0 ;;
esac
done
【例子:029】判断输入是否为纯字母示例
#!/bin/bash
error_info()
{
echo "[email protected] error, please check your input."
exit 1
}

check_name()
{
NAME=`echo $1 | tr -d ‘[a-zA-Z]‘`
if [ X"$NAME" = X"" ];then
return 0
else
return 1
fi
}

while :
do
echo -n "Please Input your first name:"
read F_NAME
if check_name $F_NAME; then
echo "Your First Name met the condition."
break
else
echo "Wrong input, please enter again."
fi
done

while :
do
echo -n "Please Input your last name:"
read L_NAME
if check_name $L_NAME; then
echo "Your Last Name met the condition."
break
else
error_info
fi
done
~

时间: 2024-11-02 10:31:44

几个Shell脚本的例子的相关文章

shell脚本编程-例子_批量添加用户脚本

用户管理是Linux系统维护的工作之一,其中设计用户添加.删除等简单操作. 需求:一次添加很多用户.在一个文本文件中. 格式:以行为单位,每行是一条用户信息.用户名和密码之间使用特定的分隔符分开,可是是空格,逗号,Tab键等.这里用空格区分 eg: [[email protected] ~]$ cat addusers.txt username001 password001 username002 password002 username003 password003 username004 p

shell脚本编程-例子_自动登陆ftp备份

ftp很常见的是用于存取文件的应用,它也用于日常备份.这种周期性的工作无疑需要通过自动化脚本来完成.本次系统为CentOS7 本次实验需要你做如下操作: 1.修改ftp服务器的配置文件/etc/vsftpd/vsftpd.conf,将anon_upload_enable=YES,前面的注释符去掉,即允许匿名用户上传文件: 2.在下一行中增加anon_other_write_enable=YES,即允许匿名用户覆盖同名文件,并重启vsftpd服务配置生效. [[email protected] c

几个不错的Shell脚本

几个Shell脚本的例子,觉得还不错. [例子:001]判断输入为数字,字符或其他 #!/bin/bash read -p "Enter a number or string here:" input case $input in [0-9]) echo -e "Good job, Your input is a numberic! \n" ;; [a-zA-Z]) echo -e "Good job, Your input is a character!

Linux Shell 高级编程技巧4----几个常用的shell脚本例子

4.几个常用的shell脚本例子    4.0.在写脚本(同样适用在编程的时候),最好写好完善的注释    4.1.kill_processes.sh(一个杀死进程的脚本) #!/bin/bash current_PID=$$ ps -aux | grep "/usr/sbin/httpd" | grep -v "grep" | awk '{print $2}' > /tmp/${current_PID}.txt for pid in `cat /tmp/${

Linux Shell脚本例子

Shell脚本是我们运维人员管理的最基础知识,下面就是我在学习过程中的一些小例子(比起大牛来说).写这篇博客的目的,是为了记录自己学习脚本的历程,也是为了能和读者一起探讨学习. # Example1: 自动创建脚本的模板 脚本名:creat_scripts.sh  # 功能描述:creat_scripts.sh SCRIPTS_NAME 如果创建的脚本名文件不存在,则创建成脚本文件; # 如果对应的文件存在,且为脚本文件,则打开文件到最后一行: # 如果对应的文件存在,但不是脚本文件,则提示退出

一些简单的shell脚本例子

         对于刚开始学shell脚本的人来说,建立编程思维很重要,需要能够把自己需要做的事情,用编程的方式表达出来,下面是我学习和搜集的一些例子,对于刚刚开始接触的人,或许有一定的帮助.      求任意数的和或乘积,先定义函数,任意数的和.乘积,然后通过case结构再定义变量,调用函数. hei(){ x=1 while [ $# -gt 0 ] do x=`expr $x \* $1` shift done echo $x } he(){ x=0 while [ $# -gt 0 ]

java调用shell脚本,并获得结果集的例子

/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Runtime rt = Runtime.getRuntime(); rt.exec(shell); } catch (Exception e) { e.printStackTrace(); } } /** * 运行shell * * @param shStr * 需要执行的shell * @return

shell脚本中执行python脚本并接收其返回值的例子

1.在shell脚本执行python脚本时,需要通过python脚本的返回值来判断后面程序要执行的命令 例:有两个py程序  hello.py 复制代码代码如下: def main():    print "Hello" if __name__=='__main__':    main()world.py def main():    print "Hello" if __name__=='__main__':    main() shell 脚本 test.sh

shell脚本交互:expect学习笔记及实例详解

最近项目需求,需要写一些shell脚本交互,管道不够用时,expect可以很好的实现脚本之间交互,搜索资料,发现网上好多文章都是转载的,觉得这篇文章还不错,所以简单修改之后拿过来和大家分享一下~ 1. expect是spawn: 后面加上需要执行的shell命令,比如说spawn sudo touch testfile 1.3 expect: 只有spawn执行的命令结果才会被expect捕捉到,因为spawn会启动一个进程,只有这个进程的相关信息才会被捕捉到,主要包括:标准输入的提示信息,Li