Shell入门学习(二)

引号

反引号:实现命令替换

[root@rhel8 ~]# echo `pwd`
/root          

 单引号

在单引号中的所有字符(包括元字符),保留原有字符含义,其中不能包含单引号,因此,单引号不支持元字符,变量替换和命令替换

[root@rhel8 ~]# echo *
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates test01.sh Videos
[root@rhel8 ~]# echo '*'
*
[root@rhel8 ~]# text='* means all files'
[root@rhel8 ~]# echo $text
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates test01.sh Videos means all files
[root@rhel8 ~]# echo '$text'
$text

双引号:在一对双引号中的字符(包括元字符),除了$、反引号和反斜线外,其他均保留原字符的含义

特点:
        关闭通配符扩展
        支持变量替换
        支持命令替换

练习: 

[root@rhel8 ~]# dlist='whoami'
[root@rhel8 ~]# echo "* $dlist end"
* whoami end
[root@rhel8 ~]# echo "`$dlist`"
root
[root@rhel8 ~]# echo "\$dlist"         //\符号是将后面的特殊符号按一般符号处理
$dlist

[root@rhel8 ~]# x=*
[root@rhel8 ~]# echo $x
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates test01.sh Videos
[root@rhel8 ~]# echo "$x"            //双引号内的字符按找原本的意思执行
*

[root@rhel8 ~]# test="Linux kernel development"
[root@rhel8 ~]# echo $test
Linux kernel development
[root@rhel8 ~]# echo "$test"
Linux kernel development

字符串拼接

[root@rhel8 shell]# myname="haha"
[root@rhel8 shell]# greeting="hello,"$myname""
[root@rhel8 shell]# greeting_1="hello,$myname"  //如果仅使用一个双引号,会执行相应的特殊字符(包括:$、反引号和反斜线)
[root@rhel8 shell]# echo $greeting $greeting_1
hello,haha hello,haha
[root@rhel8 shell]# greeting_2='hello,'$myname''
[root@rhel8 shell]# greeting_3='hello,$myname'  //如果仅使用一个单引号,特殊字符会当做一般字符
[root@rhel8 shell]# echo $greeting_2 $greeting_3
hello,haha hello,$myname

获取字符串长度

[root@rhel8 shell]# str="abcd"
[root@rhel8 shell]# echo ${#str}
4

提取字符串

[root@rhel8 shell]# str="abcdefghigklmn"
[root@rhel8 shell]# echo ${str:1:4}
bcde

查找字符串位置

[root@rhel8 shell]# str="hello world"
[root@rhel8 shell]# echo `expr index "$str" ed`    //只会查找第一个字符的位置
2
[root@rhel8 shell]# echo `expr index "$str" d`
11

条件表达式

基本命令和变量赋值语句属于顺序语句,条件和分支属于选择语句,for,while和until语句属于循环语句

条件表达式是用于判断条件是否满足的逻辑表达式,当条件为真返回0,否则返回1

语法:

语法1:test 条件表达式
语法2:[ 条件表达式 ]

文件状态操作符

-d filename     若文件为目录文件,则返回真
-f filename     若文件为普通文件,则返回真
-r filename     若文件可读,则返回真
-s filename     若文件长度大于0,则返回真
-u filename     若文件的SUID位被设置,则返回真
-w filename     若文件可写,则返回真
-x filename     若文件可执行,则返回真

字符串操作符

字符串操作符用于判断字符串的性质以及字符串之间的关系         

string                若字符串非空,则返回真
-n string             若字符串长度大于0,则返回真
-z string             若字符串长度为0,则返回真
string1 = string2     若两个字符串相等,则返回真
string1 != string2    若两个字符串不相等,则返回真

[root@rhel8 ~]# user=tom
[root@rhel8 ~]# test "$user"=tom
[root@rhel8 ~]# echo $?                                 
//这里简单说一下$?,当返回0时,说明上一条命令执行成功,当返回为非0时  0                                                                          执行失败。也可以人为修改为固定的值。
[root@rhel8 ~]# month="January "
[root@rhel8 ~]# test $month = January
[root@rhel8 ~]# echo $?

0
[root@rhel8 ~]# str1="testing string"
[root@rhel8 ~]# test "$str1" = "testing string"
[root@rhel8 ~]# echo $?

0
[root@rhel8 ~]# test $str1 = "testing string"   //这里等号的左边没有加双引号,输出为两个字符串,右边加双引号为一    -bash: test: too many arguments                           个字符串,所以不相等
[root@rhel8 ~]# name=""
[root@rhel8 ~]# test "$name"=tom
[root@rhel8 ~]# echo $?                       
             //上一条命令执行成功,但是执行的结果为假,所以这里依然显示的为0
0

[root@rhel8 ~]# blanks="    "
[root@rhel8 ~]# test $blanks
[root@rhel8 ~]# echo $?

1
[root@rhel8 ~]# test "$blanks"
[root@rhel8 ~]# echo $?

0

数字操作符

数字操作符操作的对象是数值,用于比较两个数值的大小关系

n1 -eq n2        判断是否相等,相等返回真
n1 -ne n2        判断是否不相等,不等返回真
n1 -lt n2        判断n1小于n2,若小于返回真
n1 -gt n2        判断n1大于n2,若大于返回真
n1 -le n2        判断n1不大于n2,若不大于返回真
n1 -ge n2        判断n1不小于n2,若不小于返回真

[root@rhel8 ~]# x1="005"
[root@rhel8 ~]# test "$x1" -eq 5
[root@rhel8 ~]# echo $?
0
[root@rhel8 ~]# test "$x1" = 5
[root@rhel8 ~]# echo $?
1

注意:操作对象的类型由操作符决定,当操作符为 -eq 时,将$x1作为数字处理,当操作符为 = 时,则作为字符串处理       

逻辑操作符

若操作对象是逻辑表达式,则用逻辑操作符

e1 -a e2    同时为真返回真   all
e1 -o e2    有一个为真返回真 or
! e1        表达式e1不为真时返回真 not

[root@rhel8 ~]# count=10
[root@rhel8 ~]# test "$count" -ge 0 -a "$count" -lt 10
[root@rhel8 ~]# echo $?

1
[root@rhel8 ~]# count=8
[root@rhel8 ~]# test "$count" -ge 0 -a "$count" -lt 10
[root@rhel8 ~]# echo $?

0

命令分隔符

cmd1;cmd2      以独立的进程依次执行cmd1和cmd2
(cmd1;cmd2)    在同一进程中依次执行cmd1和cmd2
cmd1&cmd2      cmd1和cmd2同时运行,分属于不同的进程组
cmd1&&cmd2     当cmd1为真时,执行cmd2
cmd1||cmd2     当cmd1为真时不执行cmd2;当cmd1不为真时,执行cmd2
cmd1 | cmd2    cmd1的输出作为cmd2的输入
cmd1&          cmd1以后台方式运行

[root@localhost test]# number=10
[root@localhost test]# [ "$number" -gt 1 ] && [ "$number" -lt 10 ]
[root@localhost test]# echo $?

1
[root@localhost test]# touch test02.sh
[root@localhost test]# file=test02.sh

[root@localhost test]# test -s $file && cat $file      //-s表示文件存在并且不为空
[root@localhost test]# echo $?                               //此时文件为空,可以看到上一条命令执行失败
1     

[root@localhost test]# vim test02.sh                      //编辑该文件,随便写入东西
[root@localhost test]# test -s $file && cat $file
sdaawdqqs
[root@localhost test]# echo $?                              //执行成功
0     

[root@rhel8 ~]# [ "$yn" = "Y" ] || [ "$yn" = "y" ]          //此时yn还没有赋值
[root@rhel8 ~]# echo $?
1
[root@rhel8 ~]# yn=Y
[root@rhel8 ~]# [ "$yn" = "Y" ] || [ "$yn" = "y" ]
[root@rhel8 ~]# echo $?
0
[root@rhel8 ~]# yn=y
[root@rhel8 ~]# [ "$yn" = "Y" ] || [ "$yn" = "y" ]
[root@rhel8 ~]# echo $?
0

[root@localhost ~]# test -x sh1.sh||echo "sh1.sh does not exist or is not runnable"    //-x表示文件是否存在并授予执sh1.sh does not exist or is not runnable                                                                                      行(或搜索)权限
[root@localhost ~]# touch sh1.sh
[root@localhost ~]# test -x sh1.sh||echo "sh1.sh does not exist or is not runnable"

sh1.sh does not exist or is not runnable
[root@localhost ~]# chmod a+x sh1.sh
[root@localhost ~]# test -x sh1.sh||echo "sh1.sh does not exist or is not runnable" 
[root@localhost ~]# echo $?
0

猜你喜欢

转载自blog.csdn.net/weixin_43997530/article/details/106685690