shell day2

发布于:2023-01-13 ⋅ 阅读:(430) ⋅ 点赞:(0)

shell的运算符

1、算术运算符:+,-,*,/,%,**

2、赋值运算符:=,+=,-=,/=,*=,%=

3、比较运算符:>,>=,<,<=,!=,==

4、逻辑运算符:and   or   not

5、位运算符:&,|,~,^,<<,>>

6、成员运算符:xxx in xxx

7、身份运算符:xxx is xxx

关于位运算

如下的三个概念,全部都是因为负数二进制运算出错,为了解决这个问题而诞生
        原码:原始的二进制码
        反码:符号位不变,其余各位取反
        补码:反码加1 (计算机中将负数以补码形式存储)


		在一个字节中
		10  0000 1010
		11  0000 1011
				
				与运算符(一种为假则为假)
				10 & 11 = 10
					
					1010
					1011   &
					----------
					1010
				
				或运算符 (一种为真则为真)
				10 | 11 = 11
				
					1010
					1011  |
					----------
					1011
		
				异或运算符 (相反为真)
				规律:
					1、两个相同的数做异或结果为0
					2、任何数和0异或,结果不会发生变化
				10 ^ 11 = 1
				
					1010
					1011  ^
					----------
					0001
				
				按位取反 (把真变假 把假变真)
				~10  
				
				~0000 1010
				-----------
				 1111 0101
				 
				 
				左移运算
				2 << 2 = 8
				
				0000 0010 <<
				-----------
				0001 0000
				
				右移动运算
				  >>
				  
				  
				符号位: 0为正
				         1为负
						 
					0111 1111   +127
					1111 1111   -127
					1000 0000   -128

一些使用运算符的例子

 1 #!/bin/bash
  2 #########################
  3 #File name:test.sh
  4 #Version:v1.0
  5 #Email:admin@test.com
  6 #Created time:2022-08-18 06:56:11
  7 #Description:
  8 #########################
  9 a=$1
 10 b=$2
 11 echo a+b=$(($a+$b))
 12 echo a-b=$(($a-$b))
 13 echo a*b=$(($a*$b))
 14 echo a/b=$(($a/$b))
 15 echo a%b=$(($a%$b))
 16 echo "--------------"
 17 let r=$a+$b
 18 echo a+b=$r
 19 x=`expr $a + $b`
 20 echo a+b=$x


[root@r8 shell_ex]# bash test.sh 8 4
a+b=12
a-b=4
a*b=32
a/b=2
a%b=0
--------------
a+b=12
a+b=12

${}中的使用

 

示例:截取字符串

[root@r8 shell_ex]# str1="hello world"
#返回变量长度
[root@r8 shell_ex]# echo ${#str1}
11

#变量截取
#指定起始位置,一直到结束
[root@r8 shell_ex]# echo ${str1:1}
ello world

#指定长度,不指定起始位置默认从开头开始
[root@r8 shell_ex]# echo ${str1::3}
hel

#指定起始位置和长度
[root@r8 shell_ex]# echo ${str1:1:3}
ell

#从右边第几个字符开始,及字符的个数
[root@r8 shell_ex]# echo ${str1:0-1:1}
d

#输出右边的几个字符
[root@r8 shell_ex]# echo ${str1:0-5}
world
[root@r8 shell_ex]# echo ${str1: -5}
world

#提取完整字符串
[root@r8 shell_ex]# echo ${str1:-5}
hello world

示例:删除字符串


#获取后缀名tar.gz
[root@r8 shell_ex]# filename=testfile.tar.gz
[root@r8 shell_ex]# file=${filename#*.}
[root@r8 shell_ex]# echo $file
tar.gz
#获取后缀名.gz
[root@r8 shell_ex]# filename=testfile.tar.gz
[root@r8 shell_ex]# file=${filename##*.}
[root@r8 shell_ex]# echo $file
gz
#截取testfile.tar
[root@r8 shell_ex]# filename=testfile.tar.gz
[root@r8 shell_ex]# file=${filename%.*}
[root@r8 shell_ex]# echo $file
testfile.tar
#截取testfile
[root@r8 shell_ex]# filename=testfile.tar.gz
[root@r8 shell_ex]# file=${filename%%.*}
[root@r8 shell_ex]# echo $file
testfile

条件测试的语法

文件测试表达式

测试文件的读、写、执行等属性,不光是根据文件属性rwx的标识来判断,还要看当前执行测试的用户 是否真的可以按照对应的权限操作文件。

示例:

让用户输入一个文件名,并做如下判断:

(1)如果用户输入的文件为空时显示:you must input a filename,并中断程序;

(2)如果用户输入的文件不存在时,显示the file do not exist,并中断程序;

(3)如果文件存在,判断该文件的文件类型和执行者对该文件所拥有的的权限。

  1 #!/bin/bash
  2 #########################
  3 #File name:test.sh
  4 #Version:v1.0
  5 #Email:admin@test.com
  6 #Created time:2022-08-18 06:56:11
  7 #Description:
  8 #########################
  9 read -p "pleaes input a filename:" filename
 10 [[ -z $filename ]] && echo "you must input a filename" && exit 0
 11 [[ ! -e $filename ]] && echo "the file $filename do not exist" && exit 0
 12 [ -f $filename ] && filetype="regulare file"
 13 test -d $filename && filetype="directory"
 14 test -r $filename && perm="readable"
 15 test -w $filename && perm="$perm writable"
 16 test -x $filename && perm="$perm executable"
 17 echo "-----------------------------------------"
 18 echo "the $filename is a $filetype"
 19 echo "and the permissons are: $perm"
[root@r8 shell_ex]# bash test.sh
pleaes input a filename:test1
-----------------------------------------
the test1 is a regulare file
and the permissons are: readable writable
[root@r8 shell_ex]# bash test.sh
pleaes input a filename:unexit
the file unexit do not exist

常用字符串测试表达式

 

 整数测试表达式

 

通过read读入两个整数,并比较他们的大小

  1 #!/bin/bash
  2 #########################
  3 #File name:test.sh
  4 #Version:v1.0
  5 #Email:admin@test.com
  6 #Created time:2022-08-18 06:56:11
  7 #Description:
  8 #########################
  9 read -p "please input INTEGER:" num1 num2
 10 test -z "$num1" && echo " error " && exit 0
 11 [[ -z "$num1" ]] && echo " error " && exit 0
 12 test "$num1" -eq "$num2" && echo "num1=num2"
 13 [ "$num1" -gt "$num2" ] && echo "num1>num2"
 14 [ "$num1" -lt "$num2" ] && echo "num1<num2"
 15 

测试

[root@r8 shell_ex]# bash test.sh
please input INTEGER:2 3
num1<num2
[root@r8 shell_ex]# bash test.sh
please input INTEGER:3 2
num1>num2
[root@r8 shell_ex]# bash test.sh
please input INTEGER:3 3  
num1=num2
[root@r8 shell_ex]# bash test.sh
please input INTEGER:   
 error 

假设执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据:

程序的文件名;

共有几个参数;

若参数的个数小于2个则告知用户参数数量太少;

全部的参数内容;

第一 个参数;

第二个参数。

[root@r8 shell_ex]# vim one.sh

  1 #!/bin/bash
  2 #########################
  3 #File name:one.sh
  4 #Version:v1.0
  5 #Email:admin@test.com
  6 #Created time:2022-08-18 09:21:09
  7 #Description:
  8 #########################
  9 [ "$#" != 0 ] && {
 10     echo "the script name is $0"
 11     echo "the parameter number is $#"
 12 }
 13 
 14 if [ "$#" -lt 2 ];then
 15     echo "the number of parameter is less than 2."
 16     exit 0
 17 fi
 18 
 19 if [ "$#" -gt 2 ];then
 20     echo "your whole parameter is '$@'"
 21     echo "the 1st parameter is $1"
 22     echo "the 2nd parameter is $2"
 23 fi

测试

[root@r8 shell_ex]# bash one.sh haha xixi hehe
the script name is one.sh
the parameter number is 3
your whole parameter is 'haha xixi hehe'
the 1st parameter is haha
the 2nd parameter is xixi
[root@r8 shell_ex]# bash one.sh haha xixi 
the script name is one.sh
the parameter number is 2

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到