1、 shell脚本是什么

它是一种脚本语言,并非编程语言。

可以使用一些逻辑判断、循环等语法。

可以自定义子函数,是系统命令的集合。

shell脚本可以实现自动化运维,大大增加我们的工作效率。

2、shell脚本结构以及执行方法

开头行指定bash路径: #! /bin/bash

以#开头的行作为解释说明

#注释自己的脚本内容,方便自己查阅;utf8字符集,支持中文;

脚本的名字以.sh结尾,用于区分这是一个shell脚本

执行脚本方式有两种:

chmod a+x 1.sh    添加x执行权限;

./1.sh 可以直接执行,或者写绝对路径/root/shell/1.sh

如果没有执行权限可以 bash 1.sh    或 sh 1.sh

bash -x 1.sh 可以查看脚本执行过程

实验练习:

1234 [root@localhost shell]# cat 1.sh

#!/bin/bash

#这是我的第一个脚本

echo "hello world"

[root@localhost shell]# ls -l

-rw-r--r-- 1 root root 60 6月  16 19:28 1.sh

[root@localhost shell]# chmod a+x 1.sh

[root@localhost shell]# ls -l

-rwxr-xr-x 1 root root 60 6月  16 19:28 1.sh

[root@localhost shell]# ./1.sh

hello world

[root@localhost shell]# /root/shell/1.sh

hello world

[root@localhost shell]# /bin/sh 1.sh

hello world

[root@localhost shell]# bash -x 1.sh

+ echo 'hello world'

hello world

执行bash和sh是一样的,sh是bash的软连接文件;

[root@localhost ~]# ls -l /bin/bash

-rwxr-xr-x. 1 root root 871700 10月 16 2014 /bin/bash

[root@localhost ~]# ls -l /bin/sh

lrwxrwxrwx. 1 root root 4 3月  4 00:59 /bin/sh -> bash

3、学会date命令的用法

date  +%Y-%m-%d    date +%y-%m-%d 年月日

date  +%Y-%m-%d = date +%F 年月日

date  +%H:%M:%S = date +%T 时间

date +%s  时间戳

date -d @1434248742    根据时间戳算出当前时间

date -d "+1day" 一天后    date -d "-1day" 一天前

date -d  "-1month" 一月前

date -d  “-1min” 一分钟前

date +%w    date +%W 星期

实验练习:

[root@localhost shell]# date +%y

15

[root@localhost shell]# date +%Y

2015

[root@localhost shell]# date +%m

06

[root@localhost shell]# date +%d

16

[root@localhost shell]# date +%H

14

[root@localhost shell]# date +%M

01

[root@localhost shell]# date +%S

54

从1970年 01月01日0点0分开始算起到现在多少秒;

[root@localhost shell]# date +%s

1434434874

[root@localhost shell]# date -d @1434434874

2015年 06月 16日 星期二 14:07:54 CST

CST是中国时间 +8小时

[root@yonglinux shell]# date -d @0

1970年 01月 01日 星期四 08:00:00 CST

[root@localhost shell]# date +%F

2015-06-16

[root@localhost shell]# date +%T

14:04:17

[root@localhost shell]# date +%Y-%m-%d

2015-06-16

[root@localhost shell]# date +"%Y-%m-%d %H:%M:%S"

2015-06-16 14:05:13

[root@localhost shell]# date +"%F %T"

2015-06-16 14:05:38

周二

12 [root@localhost shell]# date +%w

2

今年的第多少周,24周

[root@localhost shell]# date +%W

24

全年有多少周,52周;

[root@localhost shell]# echo "365/7" | bc

52

[root@localhost shell]# date -d "-1 day"

2015年 06月 15日 星期一 14:16:31 CST

[root@localhost shell]# date -d "-1 day" +"%F %T"

2015-06-15 14:19:13

[root@localhost shell]# date -d "+1 day" +"%F %T"

2015-06-17 14:19:22

[root@localhost shell]# date -d "+1 month" +"%F %T"

2015-07-16 14:19:31

[root@localhost shell]# date -d "+1 year" +"%F %T"

2016-06-16 14:19:39

[root@localhost shell]# date -d "+1 week" +"%F %T"

2015-06-23 14:19:45

[root@localhost shell]# date -d "-10 hour" +"%F %T"

2015-06-16 04:19:59

[root@localhost shell]# date -d "-10 min" +"%F %T"

2015-06-16 14:10:15

[root@localhost shell]# date -d "-10 sec" +"%F %T"

2015-06-16 14:20:14

4、shell脚本中的变量

当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替。

使用条件语句时,常常使用变量    if [ $a -gt 1 ]; then ... ; fi

引用某个命令的结果时,用变量替代  n=`wc -l 1.txt`

写和用户交互的脚本时,变量也是必不可少的  read -p "Input a number: " n; echo $n

如果没写这个n,可以直接使用$REPLY

内置变量 $0, $1, $2,$#    $0表示脚本本身,$1 第一个参数,$2 第二个参数,$#表示参数的个数;

数学运算a=1;b=2; c=$(($a+$b))  或者 c=$[$a+$b]

实验练习:

引用某个命令的结果,使用变量代替

[root@localhost shell]# file=`which yum`

[root@localhost shell]# echo $file

/usr/bin/yum

[root@localhost shell]# rpm -qf $file

yum-3.2.29-60.el6.CentOS.noarch

变量只在当前shell下生效,子shell不会生效;

要想子shell也生效,使用export file 声明变量;

用户交互的变量:

[root@localhost shell]# cat 2.sh

#!/bin/bash

#与用户交互的变量

read -p "请输入一个数字:" num

echo $num

[root@localhost shell]# sh 2.sh

请输入一个数字:333

333

参数的变量:

[root@localhost shell]# cat 3.sh

#!/bin/bash

#关于参数的变量

echo "\$1=$1"

echo "\$2=$2"

echo "\$3=$3"

echo "\$#=$#"

echo "\$0=$0"

[root@localhost shell]# sh 3.sh ABC linux world

$1=ABC

$2=linux

$3=world

$#=3

$0=3.sh

数值变量:

12345678910 [root@localhost shell]# a=1;b=2

[root@localhost shell]# c=$a+$b

[root@localhost shell]# echo $c

1+2

[root@localhost shell]# c=$[$a+$b]

[root@localhost shell]# echo $c

3

[root@localhost shell]# c=$(($a+$b))

[root@localhost shell]# echo $c

3

5、shell中的逻辑判断

格式1:if 条件 ; then 语句; fi

格式2:if 条件; then 语句; else 语句; fi

格式3:if …; then … ;elif …; then …; else …; fi

逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等;注意到处都是空格。

可以使用 &&并且 || 或者 结合多个条件

大于>gt    (greater than)

小于< lt    (less than)

大于等于 >= ge

小于等于 <=  le

等于    ==eq    (equal)

不等于  != ne

实验练习:

[root@localhost shell]# cat if.sh

#!/bin/bash

#if判断语句,条件为真,打印true;

if :

then

echo true

fi

[root@localhost shell]# sh if.sh

true

if判断语句2;

[root@localhost shell]# cat if2.sh

#!/bin/bash

#if判断语句,条件为真,返回true;

if [ 1 == 1 ]

then

echo "true"

fi

[root@localhost shell]# sh -x if2.sh

+ '[' 1 == 1 ']'

+ echo true

true

[root@localhost shell]# sh if2.sh

true

if判断语句3;

[root@localhost shell]# cat if3.sh

#!/bin/bash

#if判断语句,条件为真返回true,条件为假,返回false;

if [ "1" == "2" ]

then

echo "true"

else

echo "false"

fi

[root@localhost shell]# sh if3.sh

false

变量,进行比较

[root@localhost shell]# cat if4.sh

#!/bin/bash

#if判断语句,变量进行比较;

a=1

if [ "$a" == "2" ]

then

echo "true"

else

echo "false"

fi

[root@localhost shell]# sh -x if4.sh

+ a=1

+ '[' 1 == 2 ']'

+ echo false

false

多个判断要加elif

[root@localhost shell]# cat if5.sh

#!/bin/bash

#if判断语句,多个判断使用elif;

a=1

if [ "$a" == "2" ]

then

echo "true"

elif [ "$a" -lt 10 ]

then

echo "no false"

else

echo "false"

fi

[root@localhost shell]# sh if5.sh

no false

[ $a -lt 3 ] 也可以这样代替 (($a<3));使用方括号请一定注意空格;

1234 [root@localhost shell]# a=1;if(($a<3)); then echo OK;fi

OK

[root@localhost shell]# a=1;if [ $a -lt 3 ]; then echo OK;fi

OK

&& 并且  前面的执行成功后才执行后面的;

|| 或者  前面的执行不成功执行后面的;

12345 [root@localhost shell]# a=5

[root@localhost shell]# if [ $a -lt 10 ]&&[ $a -gt 2 ];then echo OK;fi

OK

[root@localhost shell]# if [ $a -lt 10 ]||[ $a -gt 2 ];then echo OK;fi

OK

奇数偶数判断,输入的数字除以2,余数为0为偶数,非0为奇数;

[root@yonglinux shell]# cat 4.sh

#!/bin/bash

read -p "enter a number:" n

n1=$[$n%2]

if [ $n1 -eq 0 ]

then

echo "你输入的数字是偶数"

else

echo "你输入的数字是奇数"

fi

[root@yonglinux shell]# sh 4.sh

enter a number:23

你输入的数字是奇数

[root@yonglinux shell]# sh 4.sh

enter a number:90

你输入的数字是偶数

判断输入的是否是数字,不是数字的直接退出;数字的话判断奇数或偶数;

`echo $n|grep -c '[^0-9]'`    匹配输入的字符为非数字的行数,如果为1说明不是数字。

[root@localhost shell]# cat 5.sh

#!/bin/bash

#判断输入的是否是数字,不是数字的直接退出;数字的话判断是奇数或偶数;

read -p "请输入一个数字:" n

n2=`echo $n|grep -c '[^0-9]'`

if [ $n2 -eq 1 ]

then

echo "你输入的不是纯数字,请重新输入"

exit 1

fi

n1=$[$n%2]

if [ $n1 -eq 0 ]

then

echo "你输入的数字是偶数"

else

echo "你输入的数字是奇数"

fi

[root@localhost shell]# sh 5.sh

请输入一个数字:abc

你输入的不是纯数字,请重新输入

[root@localhost shell]# sh 5.sh

请输入一个数字:323

你输入的数字是奇数

6、if 判断文件、目录属性

[ -f file ]判断是否是普通文件,且存在

[ -d file ] 判断是否是目录,且存在

[ -e file ] 判断文件或目录是否存在

[ -r file ] 判断文件是否可读

[ -w file ] 判断文件是否可写

[ -x file ] 判断文件是否可执行

实验练习:

1.sh存在的话执行后面的

[root@localhost shell]# [ -f 1.sh ] && echo "1.sh exist"

1.sh exist

21.sh不存在执行后面的。

12 [root@localhost shell]# [ -f 21.sh ] || echo "1.sh not exist"

1.sh not exist

判断文件是否存在

[root@localhost shell]# cat test.sh

#!/bin/bash

#判断1.sh是否存在;

if [ -e 1.sh ]

then

echo "1.sh exist"

else

echo "1.sh not exist"

fi

[root@localhost shell]# sh test.sh

1.sh exist

exec的用法,结合date变量实验

exec命令用于调用并执行指令的命令。exec命令通常用在shell脚本程序中,可以调用其他的命令。如果在当前终端中使用命令,则当指定的命令执行完毕后会立即退出终端。

[root@localhost shell]# cat date.sh

#!/bin/bash

#exec的用法,结合date变量实验;

d=`date +%F`

exec >/tmp/$d.log 2>&1

echo "Begin at `date`"

ls /tmp/abc

cd /ddd

echo "End at `date`"

[root@localhost shell]# sh date.sh

[root@localhost shell]# cat /tmp/2015-06-16.log

Begin at 2015年 06月 16日 星期二 16:49:54 CST

ls: 无法访问/tmp/abc: 没有那个文件或目录

date.sh: line 7: cd: /ddd: 没有那个文件或目录

End at 2015年 06月 16日 星期二 16:49:54 CST

exec >/tmp/$d.log 2>&1  表示执行下面的行,输出正确或错误的信息都写到/tmp/目录下 日期.log里面;

linux下shell脚本论文,Linux下Shell脚本编程相关推荐

  1. linux 检查权限,检查目录下 文件的权限-linux shell脚本,

    检查目录下 文件的权限-linux shell脚本, #!/bin/bash #History: #2019/07/23    Fsq #This Program will check Permiss ...

  2. Linux下shell脚本之双色球摇号脚本

    Linux下shell脚本之双色球摇号脚本 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 二.脚本内容 三.运行脚本 一.脚本要求 1.编写脚本Lottery.sh,模拟摇号过程 2.6位数 ...

  3. linux新建备份数据库的脚本文件,Linux下shell脚本:自动每日备份网站文件和数据库上传FTP空间...

    前言 服务器.vps,难免会遇到各种问题,丢失数据,则必然痛心疾首啊!!! 数据无价,so建议养成定期备份的习惯!而且,要多备份几份,本地.网盘.FTP空间等,都多保存几份! 这里,就为大家带来一个l ...

  4. 如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样)

    如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样) python标准库中的subprocess可以解决这个问题. from subprocess ...

  5. linux用户开机.bashrc,验证linux shell在启动时会自动执行用户主目录下的.bashrc脚本...

    在本文中, 我们来一起验证linux shell在启动时会自动执行用户主目录下的.bashrc脚本(注意是.bashrc而不是bashrc). 如果你的主目录下没有.bashrc, 那也不要担心,自己 ...

  6. linux下开机自动启动,定时运行shell脚本

    Shell 脚本与window/dos 下的批处理相似,也就是用各类命令预先放入一个文件中,方便一次性执行的的一个程序文件,主要是方便管理员进行设置或者管理用. 一.shell脚本 在Linux下,我 ...

  7. Linux shell脚本,Linux下的西红柿时间管理法 I

    Linux shell脚本,Linux下的西红柿时间管理法 I 全部代码在[https://github.com/younggift/tomato.git]. 1. 西红柿时间管理法是什么 西红柿是一 ...

  8. linux脚本查看系统内存,二个linux下查看内存使用情况的shell脚本()

    摘要 腾兴网为您分享:二个linux下查看内存使用情况的shell脚本(),政务易,悦作业,优化大师,王者荣耀等软件知识,以及单向历app,优路教育app,kimoji,开关电源设计软件,皮皮高清影视 ...

  9. Linux 金字塔 的shell命令,linux下保留文件系统下剩余指定数目文件的shell脚本

    原文出处: http://www.jbxue.com/article/13808.html (原创文章,转载请注明出处) 本节内容: 保留文件系统下剩余指定数目的文件 例子: #!/bin/bash ...

最新文章

  1. (入门)上传GitHub怎么忽略node_modules
  2. .net Forms身份验证不能用在应用的分布式部署中吗?
  3. 又一款度盘不限速神器!无需登录即可下载
  4. ajax绑定事件页面重复提交,ajax防止用户重复提交点击事件
  5. mysql 5.7 mirror_Centos7 Docker离线部署Mysql5.7
  6. 中山大学附属第一医院精准医学研究院 消化系统肿瘤研究于君课题组招聘启事...
  7. linux模块创建proc,[Linux 运维]/proc/modules 以及内核模块工具
  8. swap分区自动建立配置
  9. 失焦事件触发_JavaScript event 事件详解
  10. Linux视频教程在线观看Boobooke
  11. 浪潮服务器开启远程管理,最新浪潮服务器远程管理(27页)-原创力文档
  12. Html中如何自定义Video显示的长宽比
  13. 去除WordPress网站链接中“category”的四种方法
  14. APP软件测试点着重详解
  15. ocr文字识别技术有什么意义
  16. excel离散度图表怎么算_excel离散数据表格-Excel 离散程度分析图表如何做
  17. win10 oracle怎样卸载,怎么完全卸载Oracle?(Win10) 以Oracle 11g为例
  18. TeamView: 为了进一步增强安全性,在发起连接之前,我们希望您先验证您的账户
  19. js创建一个电脑对象,该对象要有颜色、重量、品牌、型号,可以看电影、听音乐、打游戏和敲代码。
  20. HCE知识盘点:大事记、NFC卡模拟、与SE兼容

热门文章

  1. TensorFlow2实现协同过滤算法中的矩阵分解(首家基于TS2版本)
  2. php获取随机图片,PHP 随机显示某张图片
  3. PAT_B_1050_C++(20分)
  4. python自动化办公知识点整理汇总_Python自动化办公知识点整理汇总
  5. qt如何提前jpg文件里的缩略图_如何去捕获和编辑H5制作软件Hype中的海报图像
  6. [Spark]PySpark入门学习教程---RDD介绍(2)
  7. 感知算法论文(九):Towards Accurate One-Stage Object Detection with AP-Loss
  8. nlp文本相似度_用几行代码在Python中搜索相似文本:一个NLP项目
  9. C语言高级编程:二维数组、指针的指针、数组指针、指针数组
  10. sublime txt常用插件推荐