Shell脚本编写与应用

简介:

脚本一:检查对象是否存在

    判断目录是否存在,如果有就再判断是否有指定文件,不存在就创建这个文件,并把当前系统时间写入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
if  [ -e $HOME ]
then
         echo  "ok. $HOME exist."
   if  [ -e $HOME /testing  ]
then
         echo  "Appending date to existing file"
         date  >> $HOME /testing
else
   echo  "Creating new file"
         date  >> $HOME /tesing
   fi
else
   echo  "Sorry.you do not have a HOME directory"
fi
1
2
3
[root@www ~] # sh t2.sh 
ok.  /root  exist.
Creating new  file


脚本二:判断是否是文件

    判断指定目录文件是否存在,如果存在,继续判断是否是一个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
#check if a file
if  [ -e $HOME ]; then
         echo  "The object exist. is it a file?"
   if  [ -f $HOME ]; then
         echo  "Yes. it is a file."
   else
         echo  "No. it is not a file!"
         if  [ -f $HOME/.bash_history ]; then
                 echo  "But this is a file!"
         fi
fi
else
         echo  "Sorry.the object does bot exist."
fi
1
2
3
4
[root@www ~] # sh t3.sh 
The object exist. is it a  file ?
No. it is not a  file !
But this is a  file !

脚本三:检查文件是否可读

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
#check if you can read a file
pwfile= /etc/shadow
if  [ -f $pwfile ]; then
   if  [ -r $ file  ]; then
         tail  -n1 $pwfile
   else
         echo  "Sorry. I am unable to read the $pwfile file"
   fi
else
         echo  "Sorry. the file $file does not exist"
fi
1
2
[root@www ~] # sh t4.sh 
admin:$6$sSKhOTAb$OaDGmJufvzbsZj4zFkdVFtvVYrVECdaGRvOB7Zrt9RSgmuiNxhlAIV5mtEkFSuX9gRxZ5LwxkXo06Ro0SN2Tm.:16818:0:99999:7:::


脚本四:检查空文件-s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
#testing if a file is empty
file =file55
touch  $ file
if  [ -s $ file  ]; then
         echo  "The $file file exist and has data in it"
else
         echo  "The $file exist and is empty."
fi
date  > $ file
if  [ -s $ file  ]; then
         echo  "The $file file has data in it"
else
         echo  "The $file is still empty."
fi
1
2
3
4
5
6
[root@www ~] # sh t5.sh 
The file55 exist and is empty.
The file55  file  has data  in  it
[root@www ~] # sh t5.sh 
The file55  file  exist and has data  in  it
The file55  file  has data  in  it



脚本五:检查是否可写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
#checking if a file is writeable
logfile=$HOME /t6file
touch  $logfile
chmod  u-w $logfile
now=` date  +%Y-%m-%d-%H:%M`
if  [ -w $logfile ]; then
         echo  "The program ran at:$now"  > $logfile
         echo  "The first attempt succeeded"
else
         echo  "The first attempt failed"
fi
chmod  u+w $logfile
if  [ -w $logfile ]; then
         echo  "The program ran at:$now"  > $logfile
         echo  "The second attempt succeeded"
else
         echo  "The second attempt failed"
fi
1
2
3
[root@www ~] # sh t6.sh 
The first attempt succeeded
The second attempt succeeded


脚本六:检查是否可执行

1
2
3
4
5
6
7
8
#!/bin/bash
#checking file execution
if  [ -x tesing ]; then
         echo  "You can run the script:"
         . /tesing
else
         echo  "Sorry. you are unable to execute the script"
fi
1
2
3
4
5
[root@www ~] # sh t7.sh 
Sorry. you are unable to execute the script
[root@www ~] # chmod a+x tesing 
[root@www ~] # sh t7.sh 
You can run the script:


脚本七:查看系统当前存在的普通用户数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
sum =` awk  -F ':'  '$3 >= 500'  /etc/passwd  wc  -l`
uname =` awk  -F:  '$3 >= 500 {print $1," Uid=",$3}'  /etc/passwd `
if  [ $ sum  - eq  0 ]; then
         echo  "The system exist $sum common users."
else
         echo  "The system exist $sum common users:"
         echo  "$uname"
fi
 
 
[root@master1 ~] # sh user1.sh 
The system exist 2  users :
HM  Uid= 500
UU  Uid= 501


脚本八:按照日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为2013-09-23.log, 并且把磁盘的使用情况写到到这个文件中。

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
d=` date  "+%F" `
dir = /home/disk_usage
f= /home/disk_usage/ $d.log
[ -d $ dir  ] ||  mkdir  /home/disk_usage
 
if  [ $? - eq  0 ]; then
         echo  "-------------------------------------"  >> $f
         df  -Th >> $f
fi


脚本九:用脚本生成shell脚本文件头

1、方法一

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
if  sed  -n  '/^#!/p'  $1 &> /dev/null
then
cat  >> $1 << EOF
#!/bin/bash                                                                                                      
#author by HM
#Date & Time:`date +"%F %T"` 
EOF
fi
vim +3 $1

2、方法二

1
2
3
4
5
6
7
8
#!/bin/bash
file =$1
dt=` date  + "%F %T" `
touch  $ file
echo  "#!/bin/bash"  >> $ file
echo  "#author by HM"  >> $ file
echo  "#Date & Time: $dt"  >> $ file
vim +3 $ file


脚本十:判断当前的操作系统的类型,使用if elif语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
PLATFORM=` uname  -s`
FREEBSD=
SUNOS=
MAC=
AIX=
MINIX=
LINUX=
 
echo
echo  "Identifying the platfrom on which this installer is running on .."
echo
 
if  "$PLATFORM"  "FreeBSD"  ]; then
         echo  "This is FreeBSD system."
         FREEBSD=1
elif  "$PLATFORM"  "SunOS"  ]; then
         echo  "This is Solaris system."
         SUNOS=1
elif  "PLATFORM"  "Darwin"  ]; then
         echo  "This is Mac OSX system."
         MAC=1
elif  "$PLATFORM"  "AIX"  ]; then
         echo  "This is AIX system."
         AIX=1
elif  "$PLATFORM"  "MINIX"  ]; then
         echo  "This is MINIX system."
         MINIX=1
elif  "$PLATFORM"  "Linux"  ]; then
         echo  "This is Linux system."
         LINUX=1
else
         echo  "Failed to identify this Openrating System,Abort!"
         exit  1
fi
 
echo  "Starting to install the software..."
echo
 
exit  0

执行结果

1
2
3
4
5
6
[root@salve1 ~] # sh sys.sh 
 
Identifying the platfrom on  which  this installer is running on ..
 
This is Linux system.
Starting to  install  the software...


脚本十一:打印九九乘法口诀

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
for  in  {1..9}
do
for  in  ` seq  1 $i`
   do
     m=` echo  "$i"  "*"  "$j"  bc `
         echo  -n  "$j " x " $i " = " $m"  " "
   done
     echo  " "
done
1
2
3
4
5
6
7
8
9
10
# sh 9x9.sh 
1 x 1 = 1   
1 x 2 = 2  2 x 2 = 4   
1 x 3 = 3  2 x 3 = 6  3 x 3 = 9   
1 x 4 = 4  2 x 4 = 8  3 x 4 = 12  4 x 4 = 16   
1 x 5 = 5  2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25   
1 x 6 = 6  2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36   
1 x 7 = 7  2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49   
1 x 8 = 8  2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64   
1 x 9 = 9  2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81


脚本十二:case的使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash
 
#clear清除屏幕
clear
 
echo  "          File Operation List"
echo  "          ---- --------- ----"
 
echo  "Choose one of the following operations:"
echo
 
echo  "[O]pen File"
echo  "[D]elete File"
echo  "[R]ename File"
echo  "[M]ove File"
echo  "[C]opy File"
echo  "[P]aste File"
echo
 
echo  -n  "Please enter your operation:"
read  operation
 
case  "$operation"  in
 
"O" | "o" )
         echo
         echo  "Opening File ..."
         echo  "Successed!"
         ;;
 
"D" | "d" )
         echo
         echo  "Deleleting File..."
         echo  "Successed!"
         ;;
 
"R" | "r" )
         echo
         echo  "Rename File to File2"
         echo  "Successed!"
         ;;
 
"M" | "m" )
         echo
         echo  "Moving File1 to File2..."
         echo  "Successed!"
         ;;
 
"C" | "c" )
         echo
         echo  "Copying File1 to File2..."
         echo  "Successed!"
         ;;
 
"P" | "p" )
         echo
         echo  "Paste File"
         echo  "Successed!"
         ;;
 
*)
         echo
         echo  "Error,Unknown operation."
         echo  "Program exit!"
         exit  1
         ;;
esac
 
echo
 
exit  0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# sh case1.sh 
           File Operation List
           ---- --------- ----
Choose one of the following operations:
 
[O]pen File
[D]elete File
[R]ename File
[M]ove File
[C]opy File
[P]aste File
 
Please enter your operation:o
 
Opening File ...
Successed!



本文转自 HMLinux 51CTO博客,原文链接:http://blog.51cto.com/7424593/1736333
相关文章
|
12天前
|
关系型数据库 MySQL Shell
备份 MySQL 的 shell 脚本(mysqldump版本)
【4月更文挑战第28天】
23 0
|
1天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
|
2天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
2天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
4天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
20 5
|
4天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
4天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
5天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
13 3
|
10天前
|
弹性计算 运维 监控
|
10天前
|
存储 弹性计算 运维
自动化收集员工信息的Shell脚本
【4月更文挑战第30天】
10 0