示例:shell变量运算及条件测试

"这篇博客详细介绍了Shell脚本中的各种运算符,包括算术、赋值、比较、逻辑和位运算符。此外,还展示了数学计算的不同方法,如使用括号、expr、let、bc等。文章深入讲解了字符串操作,特别是`${}
摘要由CSDN通过智能技术生成

1.shell的运算符有哪些

算术运算符:+,-,*,/,%,*
赋值运算符:=,+=,-=,/=,*=,%=
比较运算符:>,>=,<,<=, !=,=
逻辑运算符: &&,|,!
位运算符:&, l , ~, ^,>>,<<
自增自减:++i,i++,--i,i--


2.数学计算的命令有哪些,并举例使用

1、(())
[root@servera bash]# echo $(( 3+4 ))
7

2、expr
示例
[root@servera bash]# expr 3 '+' 4
7

3、let
[root@servera bash]# let a=3+4;echo $a
7

4、bc
方式一:[root@servera bash]# echo '3+4'| bc
7
方式二:[root@servera bash]# bc
3+4
7
^C
(interrupt) Exiting bc.
[root@servera bash]# 

5、$[]
[root@servera bash]# echo $[ 3+4 ]
7

6、awk
[root@servera bash]# echo '' | awk '{ print 4 + 3 }'
7

7、declare
[root@servera bash]# declare -i a=4+3 | echo $a
7


3.${}中的使用,并举例包含(#, ##,%,%%,:, /, //),并举例使用

先定义变量
[root@servera bash]# data=`cat /etc/passwd | grep syc`
[root@servera bash]# echo $data
syc:x:1001:1001::/home/syc:/bin/bash
示例
1、${}   #返回变量的内容
[root@servera bash]# echo ${data}
syc:x:1001:1001::/home/syc:/bin/bash

2、${#}  #返回变量的字符个数
[root@servera bash]# echo ${#data}
36

3、${data:offset:length}  # 截取字符串
[root@servera bash]# echo ${data:5:10}
:1001:1001
Tips:顺着取得时候,下标从0开始

4、${#}
[root@servera bash]# echo ${data#*:}   # 从前往后,最短匹配,删除第一个冒号之前的字符
x:1001:1001::/home/syc:/bin/bash

5、${##}
[root@servera bash]# echo ${data##*:}  # 从后往前,最长匹配,删除第一个冒号之前的所有字符
/bin/bash

6、${:}
[root@servera bash]# echo ${data:5}  # 从下标为5的位置截取至结尾
:1001:1001::/home/syc:/bin/bash 
[root@servera bash]# echo ${data:5:10}  # 从下标5的位置开始往后截取10个
:1001:1001
[root@servera bash]# echo ${data::-4}  # 从后往前删除4个字符,保留其余
syc:x:1001:1001::/home/syc:/bin/
[root@servera bash]# echo ${data: -1}  # 从后往前删除1个字符,保留其余
h

7、${%}
[root@servera bash]# echo ${data%:*}  # 从后往前,最短匹配,删除第一个冒号之后的
syc:x:1001:1001::/home/syc

8、${%%}
[root@servera bash]# echo ${data%%:*} # 从前往后,最长匹配,删除第一个冒号之后的所有字符
syc

9、${/}
[root@servera bash]# echo ${data/h/H}  # 只替换第一个匹配到的
syc:x:1001:1001::/Home/syc:/bin/bash

10、${//}
[root@servera bash]# echo ${data//h/H}  # 替换所有的h为大写H
syc:x:1001:1001::/Home/syc:/bin/basH


4.条件测试的语法 (()), [[]],[],test,功能以及语法格式

1、((<算术表达式>))
功能:整数运算,针对于整数的比较运算符
[root@servera bash]# echo $(( 1 > 3 ))
0

2、[[ <测试表达式> ]]
格式:
功能:条件测试,可以使用逻辑运算符&,||
[root@servera bash]# [[ -f year.sh && 1 < 0 ]];echo $?
1

3、[]
格式:[ <测试表达式> ]
功能:条件测试,逻辑运算符&&,||不能使用,用-a和-o分别代替&&和||
[root@servera bash]# [ -f year.sh ];echo $?
0

4、test
功能:条件测试
格式: test <测试表达式>
[root@servera bash]# test -f year.sh ; echo $?
0


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

#!/bin/bash
read -p "Please input int_number,num_one and num_two:"  num_one num_two
  
if [ -z $num_one -o -z $num_two ];then
  echo "Please input two number"
  exit 1
fi

expr $num_one + 10 &> /dev/null    # +10判断它是否是数字
return_num_one=$?
expr $num_two + 10 &> /dev/null
return_num_two=$?

if [ "$return_num_one" -ne 0 -a "$return_num_two" -ne 0 ];then    
  echo "Please input two number"  
  exit 2
fi

if [ $num_one -lt $num_two ];then
  echo "$num_one < $num_two"
  exit 0
elif [ $num_one -gt $num_two ];then
  echo "$num_one > $num_two"
  exit 0
elif [ $num_one -eq $num_two ];then
  echo "$num_one == $num_two"
  exit 0
fi

测试
[root@servera bash]# bash compare_size_test.sh 
Please input int_number,num_one and num_two:
Please input two number
[root@servera bash]# bash compare_size_test.sh 
Please input int_number,num_one and num_two:6 6
6 == 6
[root@servera bash]# bash compare_size_test.sh 
Please input int_number,num_one and num_two:9 3
9 > 3
[root@servera bash]# bash compare_size_test.sh 
Please input int_number,num_one and num_two:3 9
3 < 9


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

程序的文件名;共有几个参数;若参数的个数小于2个则告知用户参数数量太少;全部的参数内容;第一个参数;第二个参数。

#!/bin/bash
echo "================================"
echo "the script filename is $0"
echo "================================"
if [ $# -lt 2 ];then 
  echo "parameter number is too less"
  exit 2
fi  

echo "The total number of parameters is $# "
echo "your whole parameter is '$@'" 

echo "================================"
echo "your whole parameter is '$@'" 
echo "the 1st parameter is $1" 
echo "the 2nd parameter is $2"

测试
[root@servera bash]# bash parameter_test.sh 2 3
================================
the script filename is parameter_test.sh
================================
The total number of parameters is 2 
your whole parameter is '2 3'
================================
your whole parameter is '2 3'
the 1st parameter is 2
the 2nd parameter is 3
不带参数
[root@servera bash]# bash parameter_test.sh 
================================
the script filename is parameter_test.sh
================================
parameter number is too less

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值