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

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

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

Shell脚本编写与应用相关推荐

  1. shell 脚本编写 if else then

    shell 脚本编写 if else then if ....; then .... elif ....; then .... else .... fi 大多数情况下,可以使用测试命令来对条件进行测试 ...

  2. Shell awk文本处理,shell脚本编写

    Shell awk文本处理,shell脚本编写 一:内容包含awk.变量.运算符.if多分支 <a>语法糖: awk [options] 'commands' files option - ...

  3. 7. Shell 脚本编写

    7. Shell 脚本编写 1.提示用户输入一个字符串,如果是 hello,打出 yes,并每秒输出 "hello,world",否则就输出 no,实现如下: #!/bin/bas ...

  4. shell脚本编写遇到的问题--循环

    shell脚本编写遇到的问题 0 背景 工作中需要修补数据,大概半年的数据;跑数据是scala脚本+python脚本,如果手动补充数据,需要运行180次- 于是,shell脚本搞起来- 1 规划思路 ...

  5. datetime报错 sql脚本_Linux中Mysql数据库备份shell脚本编写实例

    学了段时间的Linux,也学习了shell脚本编写的基本命令与语法,现做一个综合案例来详细讲解. 要求:1).每天凌晨备份数据库shaoxiao到/data/backup/db中 2).备份开始和备份 ...

  6. Linux系统一键安全加固shell脚本编写思路

    本次分享一下个人在对Linux系统一键安全加固shell脚本编写时的一些思路: Linux系统一键安全加固shell脚本编写思路 1.编写须知 1.1 脚本使用说明 1.2 主要功能说明: 1.3隐藏 ...

  7. linux脚本设计菜单,菜单式shell脚本编写

    用shell脚本编写有以下的功能: Syste Manage 1.show the user //显示登陆系统用户以及动作. 2.test the network //网络测试 3.show the ...

  8. Linux编写脚本nsum求和,详解Linux Shell脚本编写技巧,附实例说明

    原标题:详解Linux Shell脚本编写技巧,附实例说明 Linux Shell是一个很难的知识板块.虽然大家都认真学,基本的语法很都掌握,但有需求时,很难直接上手编程,要么写了很久,要么写不好!对 ...

  9. shell脚本编写思路和实例讲解

    shell脚本编写思路和实例讲解 前言 常听见身边有很多学习shell脚本的朋友抱怨shell脚本不好写,好不容易写出来的脚本一直报错,符号空格又太多,错了一个就无法运行还不好排查错误. 客观讲she ...

  10. bash 与 shell脚本编写指南

    bash 与 shell脚本编写指南 bash 与 shell脚本编写指南 bash基本命令 man与info命令 遍历目录命令 文件与目录列表命令 处理文件相关命令 处理目录命令 查看文件内容 通过 ...

最新文章

  1. 【spring】动态代理
  2. 一台台式计算机的主要配件有哪些,电脑的基本配件有哪些
  3. Linux常用命令和服务器配置
  4. mysql 索引效果是否叠加_MySQL基础实用知识集合(二)
  5. 找出没有出现的数 题解
  6. Android中MVC框架的运用
  7. PaddleOCR服务器端部署C++ cpu或者gpu进行预测
  8. mysql双机热备 读写分离_轻松搭建MySQL主从复制、读写分离双机热备)
  9. 关于RabbitMQ Queue Argument的简介
  10. 开关电源设计书籍推荐
  11. Android studio 教程入门
  12. 福禄克网络VERSIV(威测)电缆认证系统实现ROI更大化
  13. 抓包实现原理与反抓包
  14. 钢筋穿入女子太阳穴 消防及时破拆将其营救
  15. 群友(淡泊、明志)总结java面试题
  16. win10+VS2012+opencv2.4.11的安装和配置
  17. 三种男性最需要的营养素
  18. shell无限死循环
  19. python解决租房问题_高德API+Python解决租房问题
  20. 初学Java,阶段性总结(随笔日记)

热门文章

  1. 人工智能技术,对智慧交通的发展带来巨大影响
  2. 特斯拉AI主管给你的33条深度学习训练建议
  3. 环球博览|中国六代机有望5年内问世
  4. Waymo无人卡车高调重返凤凰城,但货运先机已失
  5. 一个框架解决几乎所有机器学习问题
  6. 欧盟发布《人工智能道德准则》:「可信赖 AI」才是 AI 的指路明灯
  7. 超级干货丨优美的课程笔记,吴恩达点赞的深度学习课程信息图
  8. jieba之sedict(自定义字典)
  9. Python 之 matplotlib (十)Image
  10. “诺奖风向标”2021拉斯克奖公布:授予mRNA疫苗、光遗传学以及戴维·巴尔的摩...