美文网首页
3、shell 脚本规范和判断表达式

3、shell 脚本规范和判断表达式

作者: 沙砾丶ye | 来源:发表于2019-12-18 19:28 被阅读0次

一、什么是 shell script

shell script 是利用 shell 的功能所写的一个“程序 (program)”,这个程序是使用纯文本文件,将一些 shell 的语法与指令(含外部指令)写在里面, 搭配正则表达式、管道命令与数据流重导向等功能,以达到我们所想要的处理目的。

简单的说,也就是可以使用一个普通的文本,写上多条 shell 命令,一起执行这些命令。
但是,在这个文件中可以添加一些逻辑判断什么的。

二、shell script 规范

  • script 的功能;
    script 的版本信息;
    script 的作者与联络方式;
    script 的版权宣告方式;
    script 的 History (历史纪录);
    script 内较特殊的指令,使用“绝对路径”的方式来下达;
    script 运行时需要的环境变量预先宣告与设置。

三、简单脚本练习

vim show-name.sh

#!/bin/bash
# Program:
#    User inputs 2 integer numbers; program will cross these two numbers.
# History:
# 2015/07/16    VBird    First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "You SHOULD input 2 numbers, I will multiplying them! \n"
read -p "first number:  " firstnu
read -p "second number: " secnu
total=$((   ${firstnu}   *    ${secnu}  ))
echo -e "\nThe result of ${firstnu} x ${secnu} is ==> ${total}"

四、执行脚本方式 (source script, sh script, ./script)

source script

父进程中执行脚本中代码

image

sh script

子进程中执行脚本中的代码,相当于打开了一个子 shell ,一个全新的环境。

image

五、script 的默认变量

位置变量

位置变量是根据命令出现在命令行上的位置来确定的变量,在shell调用过程中,会按参数所在的位置进行调用。

命令(shell脚本名) 参数1 参数2 参数3

$0    $1     $2     $3  ...$9

例如:

wc /etc/passwd /etc/group

例如:
编辑一个任意文件(这里是 position-arg.sh),写入如下内容

#!/bin/bash
echo "this scriptname is $0"
echo "this 1st argument is $1"
echo "this 2nd argument is $2" 
echo "this 3rd argument is $3"
echo "参数分别为$*"
echo "共有$#个arguments"

此脚本地执行时,使用sh执行即可

sh    position-arg.sh  a b c  d  e

Shell_判断表达式

test

image.png
示例:
$ touch a.txt
$ test -e a.txt;echo $?
0                             # 测试成功,命令返回值为 0

$ test -e s.txt;echo $?
1                             # 测试失败,命令返回值为 非 0

$ test -f a.txt;echo $?
0

$ test -d a.txt;echo $?
1
image.png

示例:

$ test -r a.txt; echo $?
0

$ test -x a.txt; echo $?
1

$ test -w a.txt; echo $?
0

$ test -u a.txt; echo $?     # 判断 a.txt 文件是否具有 SUID 属性
1

$ cat a.txt                        # 查看 a.txt ,此文件内容为空

$ test -s a.txt; echo $?    # 判断 a.txt 文件中有内容
1                                      # 命令返回值为 1 ,说明文件中没有内容

$ echo "123" > a.txt

$ test -s a.txt; echo $?
0
image.png
示例:
$ touch b.txt

$ ls -l a.txt
-rw-r--r--  1 shark  staff  4 12 17 22:59 a.txt

$ ls -l b.txt
-rw-r--r--  1 shark  staff  0 12 17 23:05 b.txt

$ test a.txt -nt b.txt; echo $?  # 判断 a.txt 是否比 b.txt 新
1                                            # 返回 1, 表示判断表达式不成立

$ test b.txt -nt a.txt; echo $?
0
image.png
$ test a.txt -ef a-hard.txt; echo $?
0
image.png
示例:
$ test 10 -eq 20; echo $?
1

$ n1=10

$ n2=20

$ test $n1 -eq $n2; echo $?
1

$ test $n1 -lt $n2; echo $?
0

$ test $n1 -ne  $n2; echo $?
0
image.png

注意:
这里的 string 可以是实际的字符串,也可以是一个变量
这里说的字符串是否为 0 的意思是 字符串的长度是否为 0

示例:

$ test   -z  ''; echo $?      # 空字符串
0

$ test  -z  ' '; echo $?      # 含有一个空格的字符串
1

$ test  !  -z ' '; echo $?   # 判断含有一个空格的字符串,其长度为非 0 的字符串, 空格也算是字符串。
0

$ test -z ${name}; echo $?   # 变量未定义,shell 中认为其长度为 0
0

$ name=shark

$ test -z ${name}; echo $?
1

$ age=''                               # 定义变量,并且赋值为空字符串

$ test  -z  ${age}; echo $?    # shell 中,被赋值为空字符串的变量长度也为 0
0

注意:
再次强调一下, 在 shell 中,以下两种情况,变量的长度均视为 0
1.变量未定义
2变量定义了,但赋值为空字符串,比如 a='' , b=""

[root@kube-master script]# name=shark
[root@kube-master script]# age=shark
[root@kube-master script]# test $name == $age ;echo $?
0
[root@kube-master script]# test $name != $age ;echo $?
1
image.png
示例 image.png

判断符号 []

[ -z "${HOME}" ] ; echo $?

必须要注意中括号的两端需要有空白字符来分隔喔! 假设我空白键使用“□”符号来表示,那么,在这些地方你都需要有空白键:

image.png

在中括号 [] 内的每个元素之间都需要用空格来分隔;
在中括号内的变量,最好都以双引号括号起来;

错误示范

# 定义变量
name="shark ops"

# 开始测试值是否相等
[ ${name} == "xiguatian" ]

会报如下错误信息:

bash: [: too many arguments

之前的错误写法 [ ${name} == "xiguatian" ] 的,会变成这样 [ shark ops == "xiguatian" ]

正确写法应该写成这样 [ "${name}" == "xiguatian" ] 的, 会变成这样 [ "shark ops" == "xiguatian" ]

相关文章

网友评论

      本文标题:3、shell 脚本规范和判断表达式

      本文链接:https://www.haomeiwen.com/subject/dtounctx.html