Shell学习笔记(二)

目录

1、引号

1.1 反引号

1.2 单引号

1.3 双引号

2、字符串的处理

2.1 字符串拼接

2.2 获取字符串长度

2.3 提取字符串

2.4 查找字符串位置

3、条件表达式

3.1 文件状态操作符

3.2 字符串操作符

3.3 数字操作符

3.4 逻辑操作符

3.5 命令分隔符


 

1、引号

1.1 反引号

可以实现命令替换

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

1.2 单引号

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

[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

1.3 双引号

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

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

[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

2、字符串的处理

2.1 字符串拼接

[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

2.2 获取字符串长度

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

2.3 提取字符串

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

2.4 查找字符串位置

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

3、条件表达式

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

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

语法:

  • 语法1:test 条件表达式
  • 语法2:[ 条件表达式 ](注意条件表达式前后要有空格)

3.1 文件状态操作符

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

[root@localhost test]# [ -w 01.sh ]             //判断01.sh是否有写权限
[root@localhost test]# echo $?
0
[root@localhost test]# [ -e /home/test/01.sh ]  //判断01.sh文件是否存在
[root@localhost test]# echo $?
0

 3.2 字符串操作符

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

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

3.3 数字操作符

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

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作为数字处理,当操作符为 = 时,则作为字符串处理       

3.4 逻辑操作符

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

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

3.5 命令分隔符

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表示文件是否存在并授予执行(或搜索)权限
h1.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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值