GVKun编程网logo

Shell脚本:if语句中的RegEx(shell脚本 if -e)

29

此处将为大家介绍关于Shell脚本:if语句中的RegEx的详细内容,并且为您解答有关shell脚本if-e的相关问题,此外,我们还将为您介绍关于20.1shell脚本介绍20.2shell脚本结构和

此处将为大家介绍关于Shell脚本:if语句中的RegEx的详细内容,并且为您解答有关shell脚本 if -e的相关问题,此外,我们还将为您介绍关于20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量、20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.4 Shell脚本中的变量、69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量、bash – shell脚本:if语句的有用信息。

本文目录一览:

Shell脚本:if语句中的RegEx(shell脚本 if -e)

Shell脚本:if语句中的RegEx(shell脚本 if -e)

我似乎无法弄清楚如何在if语句中正确编写正则表达式.我希望它打印出所有带有“结束日期”的行.

NUMBERS包含一个包含以下内容的文本文件:

End Date    ABC ABC ABC ABC ABC ABC
05/15/13    2   7   1   1   4   5  
04/16/13    4   3   0   1   3   6  
03/17/13    6   9   3   8   5   9  
02/18/13    8   2   7   1   0   1  
01/19/13    1   9   2   2   5   2  
12/20/12    7   2   7   1   0   1

以下是我的代码片段,我遇到了以下问题:

if [ -f $NUMBERS ]
then
        while read line
        do
                if [ $line = ^End ]
                then
                        echo "$line"
                else
                        echo "broKEN!"
                        break                   
                fi
        done < $NUMBERS
else
        echo "===== $NUMBERS Failed to read  ====="
fi

输出是:

broken!

解决方法

如果你正在使用bash,请尝试=〜:

...
if [[ $line =~ ^End ]]

请注意,以下内容不起作用:1

if [[ "$line" =~ "^End" ]]

20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量

20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量

第二十章 shell编程

20.1 shell介绍

  • shell是一种脚本语言
  • 可以使用逻辑判断、循环等语法
  • 可自定义函数
  • shell是系统命令的集合
  • shell脚本可以实现自动化运维,能大大增加我们的运维效率

 

20.2 shell脚本结构和执行

结构

  • 开头需要“#!/bin/bash”
  • 脚本内容中以#开头的行作为解释说明
  • 编写脚本时备注:作者、时间、功能等信息,方便之后查看
  • 脚本的名字用“.sh”结尾,用于区分这是一个shell脚本

例子:

[root@cham002 shell]# vim 01.sh 

#!/bin/bash
#written by cham
#2017-09-08
#echo w ls  

echo "123"
w
ls


[root@cham002 shell]# sh 01.sh
123
 22:50:49 up 5 min,  1 user,  load average: 0.02, 0.16, 0.11
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.230.1    22:46    1.00s  0.10s  0.00s w
01.sh

执行方法

  1.  给脚本添加执行权限“chmod a+x 01.sh”,然后直接执行该脚本“./01.sh”
  2.  sh 01.sh     (/bin/bash = .bin/sh = bash)
  3. 绝对路径执行   /root/shell/01.sh     ./ 只是相对路径
  4. [root@cham002 shell]# chmod a+x 01.sh 
    [root@cham002 shell]# ./01.sh 
    123
     22:54:32 up 8 min,  1 user,  load average: 0.07, 0.11, 0.10
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.230.1    22:46    0.00s  0.12s  0.00s /bin/bash ./01.sh
    01.sh
    
    [root@cham002 shell]# ls -l /bin/bash
    -rwxr-xr-x. 1 root root 960392 8月   3 2016 /bin/bash
    [root@cham002 shell]# ls -l /bin/sh
    lrwxrwxrwx. 1 root root 4 10月 19 06:56 /bin/sh -> bash
    
    [root@cham002 shell]# bash 01.sh
    123
     22:57:39 up 11 min,  1 user,  load average: 0.00, 0.06, 0.08
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.230.1    22:46    3.00s  0.13s  0.00s bash 01.sh
    01.sh
    [root@cham002 shell]# sh 01.sh
    123
     22:57:48 up 12 min,  1 user,  load average: 0.00, 0.06, 0.08
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.230.1    22:46    4.00s  0.13s  0.00s w
    01.sh
    

     

sh参数

  • -x:sh -x test.sh 查看显示脚本执行过程
  • -n:sh -n test.sh 查看脚本是否存在语法错误
[root@cham002 shell]# sh -x 01.sh
+ echo 123
123
+ w
 23:09:28 up 23 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.230.1    22:46    0.00s  0.42s  0.00s sh -x 01.sh
+ ls
01.sh

[root@cham002 shell]# sh -n 01.sh
#没有任何输出代表没有错

 

20.3 date命令用法

date命令用于显示或设置系统时间与日期。
语法: date [option] 参数

Options:
-d <string>:显示字符串指定的日期与时间(字符串前后必须加上双引号)
-s<string>:根据字符串来设置时间与日期(字符串前后必须加双引号)

参数:
<+时间日期格式>:指定日期和时间显示的格式

[root@cham002 shell]# date
2018年 02月 06日 星期二 23:15:10 CST

常用日期格式 

date +%Y(%y):以四位(两位)数字格式显示年份
[root@cham002 shell]# date +%Y
2018
[root@cham002 shell]# date +%y
18

date "+%Y-%m-%d %H:%M:%S %w"
以上参数分别表示:年、月、日、时、分、秒、星期
[root@cham002 shell]# date "+%Y-%m%d %h:%M:%S %w"
2018-0206 2月:25:50 2


[root@cham002 shell]# date +%m 月
02
[root@cham002 shell]# date +%d 日
06
[root@cham002 shell]# date +%H 时
23
[root@cham002 shell]# date +%M 分
18
[root@cham002 shell]# date +%S 秒
53
[root@cham002 shell]# date +%w星期几
2
[root@cham002 shell]# date +%D 2月6号18年
02/06/18
[root@cham002 shell]# date +%Y%m%d
20180206
[root@cham002 shell]# date +%F
2018-02-06

[root@cham002 shell]# date +%s时间戳
1517930389

[root@cham002 shell]# date +%T
23:20:51
[root@cham002 shell]# date +%H%M%S
232140
[root@cham002 shell]# date +%H:%M:%S
23:21:54
[root@cham002 shell]# date +%W
06

cal=calendar(日历),“cal -y”可以查看一年的日历。
[root@cham002 shell]# cal
      二月 2018     
日 一 二 三 四 五 六
             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

打印指定日期&时间

有时候需要使用N天(小时、分钟、秒(比如昨天的、))前的日期或时间。

格式: date -d "-1 day" +%F

2月6号的昨天
[root@cham002 shell]# date -d "-1 day"
2018年 02月 05日 星期一 23:35:58 CST
[root@cham002 shell]# date -d "-1 day" +%F
2018-02-05

2月份的上个月
[root@cham002 shell]# date -d "-1 month" +%F
2018-01-06

2月6号昨天的时间
[root@cham002 shell]# date -d "-1 hour" +%T
22:41:16
[root@cham002 shell]# date -d "-1 hour" +%T—%F
22:42:12—2018-02-06


说明: 指定某时间或日期的时候,后面要跟对应的时间格式参数(以上方法同样使用于 周、时、分、秒 )。

换算时间戳

[root@cham002 shell]# date +%s
1517931910
[root@cham002 shell]# date -d @1517931910
2018年 02月 06日 星期二 23:45:10 CST
[root@cham002 shell]# date +%s -d "2018-02-06 22:42:12"
1517928132

 

20.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))或者$[$a+$b]

[root@cham002 shell]# vim test.sh

#!/bin/bash
d=`date +%F`
echo "today is $d"

[root@cham002 shell]# sh test.sh
today is 2018-02-07

说明: 该脚本中将变量d定义为了当前日
注意: 在shell脚本中将命令结果定义为变量时要使用反引号,调用变量的方法:“$变量名” 。

 

内置变量

$0,$1,$2,$3……

  • $0:表示脚本本身
  • $1:第一个参数
  • $2:第二个参数
  • $#:表示参数的个数

 

[root@cham002 shell]# vim sum.sh

#!/bin/bash
a=6
b=6
sum=$[$a+$b]
echo "$a+$b=$sum"

[root@cham002 shell]# sh sum.sh
6+6=12

注: 数学运算要用[ ]括起来,且前面要加符号$。

 

和用户交互模式

[root@cham002 shell]# vim you.sh

#!/bin/bash
read -p "please input a number:" x
read -p "please input a number:" y
you=$[$x+$y]
echo "$x+$y=$you"

[root@cham002 shell]# sh you.sh
please input a number:8  
please input a number:8
8+8=16
[root@cham002 shell]# sh you.sh
please input a number:7+14
please input a number:7+14
7+14+7+14=42

注: read命令用于和用户交互,它把用户输入的字符串作为变量值,可以使用-t选项指定读取值时等待的时间(超出时间后自动退出脚本)。

shell脚本预设变量

有时候使用类似/etc/init.d/iptables restart的命令,前面的/etc/init.d/iptables文件其实就是一个shell脚本,后面的字符串restart就是预设变量。

[root@cham002 shell]# vim option.sh
#!/bin/bash
you=$[$1+$2]
echo "you=$1+$2=$you"
echo "Result of $0"

[root@cham002 shell]# sh option.sh 36

[root@cham002 shell]# sh option.sh 3 6
you=3+6=
Result of option.sh

[root@cham002 shell]# sh option.sh 7 8
you=7+8=
Result of option.sh

说明: 脚本中的$1和$2即为shell的预设变量,分别为脚本的第一个参数和第二个参数,shell脚本预设变量是没有限制的,注意$0位脚本本身的名字

 

20.5 Shell脚本中的逻辑判断

逻辑表达式

在[ ]中括号中:

  • -lt:=little than 小于  <
  • -le:=little && equal 小于等于  <=
  • -eq:=equal 等于  ==
  • -ne:=no equal 不等于  !=
  • -gt:=greater than 大于  >
  • -ge:=greater && equal 大于等于  >=

可以用用符号,但要在(( ))小括号中:类似: if (($a>1)); then echo ok;fi 

ok

<,<=,==,!=,>,>=

注意: 使用双括号括起来

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

[root@cham002 shell]# vim if1.sh

#!/bin/bash
a=5
 if [ $a -gt 3 ]  #注意[]内的空格
 then
      echo ok
 fi

[root@cham002 shell]# sh if1.sh
ok

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

[root@cham002 shell]# vim if2.sh 

#!/bin/bash
a=1
if [ $a -gt 3 ] 
then
      echo ok
else
      echo nook
 fi

[root@cham002 shell]# sh -x if2.sh 
+ a=1
+ ''['' 1 -gt 3 '']''
+ echo nook
nook

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

[root@cham002 shell]# vim if3.sh

#!/bin/bash
a=5
if [ $a -lt 3 ]
then
      echo "a<3"
elif [ $a -gt 6 ]
then
      echo "a>6"
else
      echo nook
 fi
~       

[root@cham002 shell]# sh -x if3.sh
+ a=5
+ ''['' 5 -lt 3 '']''
+ ''['' 5 -gt 6 '']''
+ echo nook
nook

关系

各个条件之间的关系可以使用逻辑连接符:

条件A&&条件B:并且
条件A||条件B:或者

• if [ $a -gt 5 ] && [ $a -lt 10 ]; then

• if [ $b -gt 5 ] || [ $b -lt 3 ]; then

 

20.6 文件目录属性判断

shell脚本中if经常用于判断文档的属性,比如判断是普通文件还是目录文件,判断文件是否有读、写、执行权限等。if常用的选项有以下几个:

  • -e:判断文件或目录是否存在
  • -d:判断是不是目录文件以及是否存在
  • -f:判断是不是普通文件以及是否存在
  • -r:判断是否有读权限
  • -w:判断是否有写权限
  • -x:判断是否有执行权限

格式 

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

[root@cham002 shell]# vim file1.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -f $f ]
then
    echo $f exist
else
    touch $f
fi


[root@cham002 shell]# sh -x file1.sh
+ f=/tmp/chamlinux
+ ''['' -f /tmp/chamlinux '']''  逻辑判断文件不存在
+ touch /tmp/chamlinux       不存在就会创建

再次执行
[root@cham002 shell]# sh -x file1.sh
+ f=/tmp/chamlinux
+ ''['' -f /tmp/chamlinux '']''  逻辑判断文件存在
+ echo /tmp/chamlinux exist
/tmp/chamlinux exist

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

[root@cham002 shell]# cp file1.sh file2.sh
[root@cham002 shell]# vi !$
vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -d $f ]
then
    echo $d exist
else
    touch $f
fi

[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ ''['' -d /tmp/chamlinux '']''
+ touch /tmp/chamlinux

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -e $f ]
then
    echo $d exist
else
    touch $f
fi
       

[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ ''['' -e /tmp/chamlinux '']''
+ echo exist
exist

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -r $f ]
then
    echo $f readable
fi

[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ ''['' -r /tmp/chamlinux '']''
+ echo /tmp/chamlinux readable
/tmp/chamlinux readable

[root@cham002 shell]# sh  file2.sh 
/tmp/chamlinux readable

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -w $f ]
then
    echo $f writeable
fi

[root@cham002 shell]# sh  file2.sh 
/tmp/chamlinux writeable

[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ ''['' -w /tmp/chamlinux '']''
+ echo /tmp/chamlinux writeable
/tmp/chamlinux writeable

#对root用户来讲是可读可写的,如果是其他用户就可能是只读。
[root@cham002 shell]# ls -l /tmp/chamlinux
-rw-r--r-- 1 root root 0 2月   7 01:08 /tmp/chamlinux

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -x $f ]
then
    echo $f exeable
fi
       

[root@cham002 shell]# sh  file2.sh   #因为不可执行所以没有输出,没有定义else
[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ ''['' -x /tmp/chamlinux '']''

 另外一种用法,判断这个文件或者目录存不存在,如果存在就删除,“&&”表示当前面的命令执行成功的时候才会执行后面的命令 。“||”表示文件不存在才会执行后面的操作

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
[ -f /tmp/chamlinux ] && rm -f $f


如果文件不存在
#!/bin/bash
f="/tmp/chamlinux"
if [ ! -f $f ]
then 
    touch $f
fi

 

20.7 if 特殊用法

•if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样

[root@cham002 shell]# vim if4.sh

#!/bin/bash
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
     echo error
     exit
elif [ $n -gt 100 ]
then
     echo fdsfsdfdf
fi


[root@cham002 shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ ''['' -z '''' '']''
+ echo error
error
+ exit
[root@cham002 shell]# 


[root@cham002 shell]# vim if4.sh

#!/bin/bash
if [ ! -f /tmp/lalal ]     
then
    echo "/tmp/lalal not exist."   即,如果变量n为空则显示error并退出该脚本。  
    exit
fi
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
     echo error
     exit
elif [ $n -gt 100 ]
then
     echo fdsfsdfdf
fi


[root@cham002 shell]# sh -x if4.sh
+ ''['' ''!'' -f /tmp/lalal '']''
+ echo ''/tmp/lalal not exist.''
/tmp/lalal not exist.
+ exit
[root@cham002 shell]# sh  if4.sh
/tmp/lalal not exist.

即,当该文件不存在的时候就会退出执行,不会提示存在语法错误。(该脚本存在逻辑错误,只做效果演示用)

注意: 在该表达式中引用变量时要用双引号引起来。

•if [ -n "$a" ] 表示当变量a的值不为空

[root@cham002 shell]# ls
01.sh  file1.sh  file2.sh  if1.sh  if2.sh  if3.sh  if4.sh  option.sh  sum.sh  test.sh  you.sh
[root@cham002 shell]# if [ -n 01.sh ]; then echo ok; fi
ok

[root@cham002 shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[root@cham002 shell]# if [ -n "$b" ]; then echo $b; else echo "b is mull";fi
b is mull

if grep -q ''123'' 1.txt; then  表示如果1.txt中含有''123''的行时会怎么样 

[root@cham002 shell]# if grep -w ''user1'' /etc/passwd; then echo "user1 exist"; fi
user1:x:1000:1000::/home/user1:/bin/bash
user1 exist
[root@cham002 shell]# if grep -wq ''user1'' /etc/passwd; then echo "user1 exist"; fi
user1 exist

判断某参数存在:
[root@cham002 shell]# vim if123.sh
#!/bin/bash
if
	grep -wq ''user1'' /etc/passwd
then
	echo "user1 exist."
fi

[root@cham002 shell]# sh if123.sh
user1 exist.
[root@cham002 shell]# sh -x if123.sh
+ grep -wq user1 /etc/passwd
+ echo ''user1 exist.''
user1 exist.

判断某参数不存在:
[root@cham002 shell]# vim ifno123.sh
#!/bin/bash
if 
        ! grep -wq ''user10'' /etc/passwd
then
        echo "no user1"
fi

[root@cham002 shell]# sh ifno123.sh
no user1


#!/bin/bash
if 
        ! grep -wq ''user10'' /etc/passwd
then
        useradd user10
        echo "yes"
fi

[root@cham002 shell]# sh ifno123.sh
useradd:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
正在创建信箱文件: 文件已存在
yes

[root@cham002 shell]# sh -x ifno123.sh
+ grep -wq user10 /etc/passwd
+ useradd user10
useradd:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
正在创建信箱文件: 文件已存在
+ echo yes
yes

说明: grep中-w选项=Word,表示过滤一个单词;-q,表示不打印过滤的结果。判断某参数不存在时使用!表示取反。

 

20.8-20.9 case判断

格式:

case 变量名 in
    value1)
       commond1
       ;;
    value2)
       commod2
       ;;
    value3)
       commod3
       ;;
esac

在case中,可以在条件中使用“|”,表示或的意思,如:

2|3)
    commond
    ;;

太晚了,记得case还没做!!!!!!!!!!!!!!!!!!!!!

20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.4 Shell脚本中的变量

20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.4 Shell脚本中的变量

20.1 Shell脚本介绍

1. shell是一种脚本语言 aming_linux blog.lishiming.net

2. 可以使用逻辑判断、循环等语法

3. 可以自定义函数

4. shell是系统命令的集合

5. shell脚本可以实现自动化运维,能大大增加我们的运维效率

20.2Shell脚本结构和执行

开头(首行)需要加#!/bin/bash

wKioL1m_ckqhE3r9AAAIvtY8MV4555.png

2. #开头行作解释说明

wKiom1m_cpKSIIw8AAAImgijdsU902.png

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

wKioL1m_cnORagMSAAAKpg-_z8k994.png

4. 执行.sh脚本方法有两种

1) 先给脚本添加x权限[root@hao-01 ~]# chmod +x 1.sh

脚本的绝对路径回车

/root/

wKiom1m_crjQp73IAAAR6B9l73w499.png

2) bash(bash=sh)执行脚本:

bash1.sh

wKiom1m_ctKhnUK4AAANPCm9Z84272.png

5.查看执行过程sh -x 1.sh

wKiom1m_c5uhsOr1AAATS5lJuOo439.png

6.检测是否有语法错误

sh -n1.sh

20.3date命令用法

1. 查看当前时间 date

wKioL1m_cv_hbEcJAAAKEU7XspY974.png

2.

年:

date +%Y

%y

月:

%m

分钟:

%M

日期:

[root@hao-01 ~]# %d

月日年:

%D

年月日

%Y%m%d

年-月-日:

%F

小时:

%H

秒:

%s

时间戳(距离1970年1月1日零点零分到现在有多少秒):

%s

时:分钟:秒:

%H:%M%s

%T

周(星期几):

%w

今年第几周(第几个星期):

%W

显示日历:

cal

前一天(昨天):

date -d "-1 day" +%F

上个月:

-1 month前一年(去年):

-1 years前一小时:

-1 hour前一分钟:

-1 min" +%F


时间戳(当前时间到1970年1月1日零点零分有多少秒):

%s

通过时间戳,翻译成日期时间date -d @1505206911

,转换成date +%s -d "2017-09-12 17:01:51"

wKioL1m_cxuhdcXAAAAwLkDpJ1U561.png

20.4Shell脚本中的变量

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

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

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

4. 写和用户交互的脚本时,变量也是必不可少的read -p "Input a number: " n; echo $n 如果没写这个n,可以直接使用$REPLY

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

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

69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量

69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量

1、shell脚本介绍

shell是一种脚本语言和传统的开发语言相比,会比较简单:

shell有自己语法,可以支持逻辑判断、循环等语法:

可以自定义函数,目的是减少重复的代码:

shell是系统命令的集合:

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

2、shell脚本结构和执行

开头需要加#!bin/bash

#号开头注释,解释说明:

脚本需要以.sh结尾,用以区分是shell脚本:

执行的方法有两种:

chmod  +x    1.sh    ;     sh   1.sh

bash   1.sh

查看脚本的执行过程:       sh    -x    1.sh

查看脚本的是否语法错误:   sh     -n    1.sh

3、shell脚本操作

创建一个shell目录,用于写shell脚本:     mkdir      shell     ;    cd   shell

#!bin/bash  第一行必须要这样写,固定的格式,表示在当前机器上用什么命令去执行这个脚本:

[root@localhost_02 shell]# cat 1.sh
#!/bin/bash
echo "123"
w
ls

执行脚本:      sh    1.sh

[root@localhost_02 shell]# sh 1.sh
123
 14:47:01 up  1:28,  1 user,  load average: 0.03, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.149.135  13:20    5.00s  0.04s  0.00s w
1.sh  :q!

注释:在当前机器上,不加"#!/bin/bash"也是可以执行的,得到的结果也相同,说明机器可以识别到每一条命令,但是换一台机器就不一定能可以执行。

通常指定"#!/bin/bash"后,接下来要运行的这些命令是通过哪一个解释器来操作的,通常都是/bin/bash这个解释器来执行的,它的作用都在于此:

3:通过给文件一个执行权限:  chmod   a+x  1.sh

执行./1.sh ,说明它的这行命令已经解析,被bin/bash认识了:

[root@localhost_02 shell]# chmod a+x 1.sh
[root@localhost_02 shell]# ./1.sh
123
 14:50:13 up  1:31,  1 user,  load average: 0.01, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.149.135  13:20    5.00s  0.05s  0.00s /bin/bash ./1.sh
1.sh  :q!

其实/bin/bash和bin/sh是同一个文件:     连接文件:

[root@localhost_02 shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960472 8月   3 2017 /bin/bash
[root@localhost_02 shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 5月  31 22:04 /bin/sh -> bash

若没有/bin/sh这个文件:则可以使用/bin/bash这个命令来执行:

[root@localhost_02 shell]# /bin/bash 1.sh
123
 14:53:19 up  1:34,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.149.135  13:20    7.00s  0.09s  0.03s w
1.sh  :q!

注释:若以"#"开头的则表示解释说明,在脚本中第二行开始开头的:

脚本一般都是以sh结尾的,用于区分是shell脚本,如若没有,则需要查看这个文件才能知道是shell脚本:

shell有两种执行方式:

1)  sh    1.sh     运行shell脚本:

2)  chmod   a+x    1.sh  先给脚本添加一个执行权限,

然后 ./1.sh 来执行.

      这里的  ./   是一个相对路径的目录,表示当前目录:

      也可以使用绝对路径来执行:       /root/shell/1.sh         #其实就是找到这个文件再执行

[root@localhost_02 shell]# /root/shell/1.sh 
123
 15:04:11 up  1:45,  1 user,  load average: 0.01, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.149.135  13:20    3.00s  0.07s  0.01s w
1.sh  :q!

查看脚本的执行过程:    注释:每一个加号,则表示一个操作步骤:

 sh     -x    1.sh        或者          /bin/bash    -x   1.sh     

[root@localhost_02 shell]# sh -x 1.sh
+ echo 123
123
+ w
 15:06:37 up  1:47,  1 user,  load average: 0.02, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.149.135  13:20    5.00s  0.06s  0.00s sh -x 1.sh
+ ls
1.sh  :q!

查看脚本是否有错误:    注释:若没有错误,则不会有任何的输出:

sh     -n    1.sh

[root@localhost_02 shell]# sh -n 1.sh

2、data 命令语法:   一般用法前面需要"+"号:

%y-%m-%d             ====        年  月  日

%H:%M:%S             ====        小时   分钟    秒

小s表示时间戳:      |       大S表示秒

date   +%Y-%m-%d                                 表示年月日(大Y表示四位的年):

date   +%y-%m-%d                                 表示年月日(小y表示两位的年):

date    +%s                                               表示时间戳:(距离1970年过去了多少秒):

date      -d      @1537515069                 (数字是时间戳显示出来的数字):   显示出来的也是当前日期:

date   -d   "+1 day"                                表示一天前

date   -d   "-1  day"                                表示一天后

date   -d    "+1   month"                       表示一月前

date    -d    "-1   month"                      表示一月后

date    -d    "+1  min"                          表示一分钟前

date    -d    "-1   min"                          表示一分后

date        +%w                                 表示周几

date       +%W                                表示一年的第几周

date语法实践

date  命令,会显示当前的系统的日期和时间:

[root@localhost_02 shell]# date
2018年 09月 21日 星期五 15:39:22 CST

date命令在shell中的作用非常大,一般用于对文件的后缀名增加一个时间:

1:表示年 月 日 date   +%Y-%m-%d                   date   +%y-%m-%d     

[root@localhost_02 shell]# LANG=en_US.UTF-8            #设定字符集:
[root@localhost_02 shell]# date +%Y-%m-%d              #表示年月日,四位的年:            
2018-09-21
[root@localhost_02 shell]# date +%y-%m-%d              #表示年月日,两位的年:
18-09-21
[root@localhost_02 shell]# date +%Y                    #表示四位的年:
2018
[root@localhost_02 shell]# date +%y                    #表示两位的年:
1
[root@localhost_02 shell]# date +%m                    # m 表示月份:
09
[root@localhost_02 shell]# date +%d                    # d 表示日期:
21
[root@localhost_02 shell]# date +%D                    # D 表示一种特殊格式的年月份:
09/21/18
[root@localhost_02 shell]# date +%F                    # 表示年月日的简写:
2018-09-21
[root@localhost_02 shell]# date +%Y%m%d                #表示年月日,不加杆也可以的:
20180921

注释:其实x表示年月日的写法:   data   +%F   ===   date   +%Y-%m-%d   ===   date   +%D(特殊格式)

注释:在表示年月日的时候:默认是用" - "来区分的(如 date  +%F), 不加也是可以的,如上例子:

2:时间单位(小时 分钟 秒   周末)           #默认使用冒号分割:

[root@localhost_02 shell]# date +%T             # T 表示简写的小时  分钟 秒:
15:53:08
[root@localhost_02 shell]# date +%H:%M:%S       #  表示小时 分钟  秒:
15:54:30
[root@localhost_02 shell]# date +%H             # H 表示小时:
15
[root@localhost_02 shell]# date +%M             # M 表示分钟:
55
[root@localhost_02 shell]# date +%S             # S 表示秒:
06
[root@localhost_02 shell]# date +%s             #表示时间戳(距1970年过去了多少秒):
1537516549
[root@localhost_02 shell]# date +%w             # w 表示周几:
5
[root@localhost_02 shell]# date +%W             # W 表示今年的第几周:
38

注释:表示时间的方式:   date   +%T  ===  date   +%H:%M:%S

注释:表示日期的方式:   date   +%F  ===  date   +%Y-%m-%h

3、date还用来标记之前的日期:会用来标记日记(前一天的日期):

year   month   day     hour    min    second    week  (后面的s可加可以不加):

用加减标记(" - "减号标记之前的日期)    (" + "加号标记之后的日期)

day   -1  "-1 year"           #显示上一年的日期:

day   -1  "-1 month"      #显示上一个月的日期:

day   +1  "+1 day"        #显示明天的日期:

day   +1    "+1  hour"     #显示下一个小时的日期:

day   +1    “+1 min”      #显示下一个分钟的日期:

day   +1     "+1 sec"         #显示下一秒的日期: 

[root@localhost_02 shell]# date -d "-1 year"          #显示上一年的日期:
2017年 09月 22日 星期五 07:49:09 CST
[root@localhost_02 shell]# date -d "-1 month"         #显示上一个月的日期:
2018年 08月 22日 星期三 07:49:16 CST
[root@localhost_02 shell]# date -d "-1 day"           #显示昨天的日期:
2018年 09月 21日 星期五 07:49:20 CST
[root@localhost_02 shell]# date -d "+1 hour"          #显示一小时后的日期:
2018年 09月 22日 星期六 08:49:28 CST
[root@localhost_02 shell]# date -d "+1 min"           #显示一分钟后的日期:
2018年 09月 22日 星期六 07:50:32 CST
[root@localhost_02 shell]# date -d "+1 sec"           #显示一秒后的日期:
2018年 09月 22日 星期六 07:49:37 CST
[root@localhost_02 shell]# date -d "+1 week"          #显示一周后的日期:
2018年 09月 29日 星期六 07:49:40 CST

  4、时间戳:      date   +%s                          date   -d    @时间戳

date   +%s   表示显示出时间戳

[root@localhost_02 shell]# date +%s
1537574033
[root@localhost_02 shell]# date -d @1537574033            @后面跟的就是时间戳:
2018年 09月 22日 星期六 07:53:53 CST

注释:若想在linux系统中,把当前日期换算成时间戳,可以使用:  date   +%s   -d"2018-09-22 07:56:53"

[root@localhost_02 shell]# date +%F-%T
2018-09-22-07:56:53
[root@localhost_02 shell]# date +%s -d"2018-09-22 07:56:53"
1537574213

5、在linux下还可以编辑日期:  cal

[root@localhost_01 network-scripts]# cal
      九月 2018     
日 一 二 三 四 五 六
                   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

3、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)) 或者$[$a+$b] 

 

 

bash – shell脚本:if语句

bash – shell脚本:if语句

我在这里按照教程: http://bash.cyberciti.biz/guide/If..else..fi#Number_Testing_Script

我的脚本看起来像:

lines=`wc -l $var/customize/script.PHP`
if test $lines -le 10
then
    echo "script has less than 10 lines"
else
    echo "script has more than 10 lines"
fi

但我的输出看起来像:

./boot.sh: line 33: test: too many arguments
script has more than 10 lines

为什么说我有太多的论点?我没有看到我的脚本与教程中的脚本有何不同.

wc -l file命令会打印两个单词.试试这个:
lines=`wc -l file | awk '{print $1}'`

要调试bash脚本(boot.sh),您可以:

$bash -x ./boot.sh

它将打印每一行执行.

我们今天的关于Shell脚本:if语句中的RegExshell脚本 if -e的分享已经告一段落,感谢您的关注,如果您想了解更多关于20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量、20.1 Shell脚本介绍;20.2 Shell脚本结构和执行;20.3 date命令用法;20.4 Shell脚本中的变量、69:shell脚本介绍 | shell脚本结构 | 执行data命令用法 | shell脚本中变量、bash – shell脚本:if语句的相关信息,请在本站查询。

本文标签: