10_Linux基础-SHELL入门1,简单脚本中使用示例:


10_Linux基础-SHELL入门1


一. 输入输出重定向

回顾1 输入输出重定向

输入

read -p “请输入” a -p 提示 a 赋值给a

read -s 密码隐藏

---------------------------------------------------------------

输出

	echo

	\# echo -e “abc\t abc”	转义字符输出

	abc		abc

	\# echo “abc\t abc”

	abc\t abc

echo -n 不接换行

echo -e 转义字符输出


二. 2个特殊文件

知识点2 2个特殊文件

两个特殊文件

·/dev/null:过滤标准错误信息

·/dev/zero:用来创建指定长度文件

/dev/null:黑洞文件,不保存,不输出的信息,就丢到黑洞文件

/dev/zero:用来生成指定大小的文件,生成一堆0


示例:/dev/zero:用来生成指定大小的文件,生成一堆0

/dev/zero一般用作生成指定大小的文件,做测试用

dd是一个备份命令,也可以产生一个指定大小的文件

if 输入文件 input file

of 输出文件 output file

bs 输出的数据的单位大小

count 输出的数据单位数量

示例:_______________________________________________________

[root@sanchuang-linux dev]# dd if=/dev/zero of=/tmp/test.dd bs=1M count=5
记录了5+0 的读入
记录了5+0 的写出
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.00196718 s, 2.7 GB/s
[root@sanchuang-linux dev]# du -sh /tmp/test.dd 
5.0M	/tmp/test.dd
if 从哪里导进来 , of 导出去这个文件 , bs 数据单位大小  , count数据单位数量

三. here document

知识点3 here document

here document 文档就在这里

<<

生成一个指定内容的文档。

简单脚本中使用

示例:

-----------------------------------------------------------

[root@sanchuang-linux chenpeng]# cat >here_test.txt <<EOF
> nihao
> sanchuang
> huanying
> world............
> x y z
\> EOF
[root@sanchuang-linux chenpeng]# cat here_test.txt
nihao
sanchuang
huanying
world............
x y z


知识点3.2 EOF是文档结束标志 可以自行定义 (end of file)

示例:

------------------------------------------------------

[root@sanchuang-linux chenpeng]# cat >here_test <<XYZ
> nihao
> hello world
> XYZ
[root@sanchuang-linux chenpeng]# cat here_test
nihao
hello world


四. tee命令

知识点4 tee命令

tee命令 输出到屏幕也重定向到文件

示例:

----------------------------------

[root@sanchuang-linux chenpeng]# echo "aa" >test_aa.txt #(注:默认不输出到屏幕)
[root@sanchuang-linux chenpeng]# cat test_aa.txt
aa
[root@sanchuang-linux chenpeng]# echo "bb" |tee test_bb.txt #(注:屏幕+文件)
bb
[root@sanchuang-linux chenpeng]# cat test_bb.txt
bb


五. 清空文件内容

知识点5 清空文件内容

[root@sanchuang-linux chenpeng]# >test_bb.txt
[root@sanchuang-linux chenpeng]# echo > test_bb.txt #(注:有换行)
[root@sanchuang-linux chenpeng]# cat test_bb.txt

[root@sanchuang-linux chenpeng]# echo -n > test_bb.txt
[root@sanchuang-linux chenpeng]# cat test_bb.txt
[root@sanchuang-linux chenpeng]# :>test_bb.txt
[root@sanchuang-linux chenpeng]# cat test_bb.txt


知识点6 echo

echo

在屏幕上显示一段文字或指定内容

输出变量,输出指定内容

-e 选项 转义字符输出

-n 选项 不接换行


六. SHELL入门

shell入门

shell 是一个用C语言写的程序,它是用户使用linux的桥梁

shell 脚本 实现自动化 重复性的操作编写脚本完成,减少人工失误


SHELL的变量

shell的变量

1、局部变量 定义在脚本或命令中

2、环境变量 shell启动的程序能访问到的环境变量 env、 echo $PATH

3、shell变量

示例:环境变量
------------------------------------------
[root@sanchuang-linux chenpeng]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls							#(注:环境变量)
[root@sanchuang-linux chenpeng]# echo $PATH	#(注:环境变量)
/lianxi/sc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/root/bin:/usr/local/nginx5/sbin:/root/bin
示例2:局部变量
-------------------------------------------
a=1
echo $a
echo ${a}

知识点8.2 变量名命名规则

变量名命名规则:

由数字、字母、下划线组合,不能以数字开头

不能使用bash中的关键字

使用一个定义过的变量,需要在前面加上$符号

示例:
--------------------------------------------
[root@sanchuang-linux chenpeng]# echo $PATH		#(注:环境变量)
/lianxi/sc:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/root/bin:/usr/local/nginx5/sbin:/root/bin

SHELL接收参数

知识点9 shell接收参数

shell接收

位置变量 :$1 - $9 ,分别代表参数列表中的 第1 - 9 个参数

可以重复使用(即脚本里可以出现两个$1)


预定义变量,系统预留的一些变量:

$0 当前进程 或者 脚本名称

$! 后台运行的最后一个进程的pid号

$? 上一条命令程序返回值

$* 代表所有参数内容

$$ 表示当前进程pid号

$# 表示参数个数

$@ 代表所有参数(逐个提取)


# perror 1

查看命令的返回值,并且看返回值的具体含义

$? 命令返回值为0 表示正常执行

	不为0 都表示运行错误
示例如下:__________________________________
[root@mysql-binary shell_test]# echo $?
1
[root@mysql-binary shell_test]# perror 1
OS error code   1:  Operation not permitted

#!/bin/bash 脚本前最好加上这一行,默认使用什么解释器去执行

原因:类unix操作系统,unbuntu、debian、centos每个操作系统的默认bash可能不一样


示例1:位置变量$1、$2 __________________________________

[root@sanchuang-linux shell_test]# cat canshu.sh
#!/bin/bash

echo "########这是$1########" #(注:位置变量)
echo "$1" #(注:位置变量)
echo "########这是$2########" #(注:位置变量)
echo "$2" #(注:位置变量)
[root@sanchuang-linux shell_test]# sh canshu.sh "hello" "world" #(注:传了2个参数)
########这是hello######## #(注:参数1)
hello
########这是world######## #(注:参数2)
world


示例2:预定义变量 $0__________________________________________________________

[root@sanchuang-linux shell_test]# echo $0	#(注:$0 当前进程 或者 脚本名称)
-bash
[root@sanchuang-linux shell_test]# sh canshu.sh "hello" "world"
########这是hello########
hello
########这是world########
world
canshu.sh									#(注:$0 当前进程 或者 脚本名称)
[root@sanchuang-linux shell_test]# cat canshu.sh 
#!/bin/bash

echo "########这是$1########"
echo "$1"
echo "########这是$2########"
echo "$2"

echo "$0"

示例3:预定义变量 $* $# $@_____________________

[root@sanchuang-linux shell_test]# vim canshu.sh
#!/bin/bash

echo "########这是$1########"
echo "$1"
echo "########这是$2########"
echo "$2"
echo "$0"

echo "这是所有:$*"			#(注:$* 代表所有参数内容)
echo "参数#:$#"			#(注:$# 表示参数个数)
echo "这是@:$@"			#(注:$@ 代表所有参数(逐个提取))
──────────────────────────────────────────────
[root@sanchuang-linux shell_test]# sh canshu.sh hello world 2020	  #(注:3个参数)
########这是hello########
hello
########这是world########
world
canshu.sh
这是所有:hello world 2020
参数#:3
这是@:hello world 2020

知识点10 python中接收参数 sys模块

python中

sys模块里面的argv属性。python后面 传过来的参数是一个列表,然后获取第一个第二个

[root@sanchuang-linux ~]# vim canshu.py
import sys

print(sys.argv[1],sys.argv[2])		#(注:1接收参数1,2接收参数2)
print(sys.argv[0])					#(注:0是文件名)
----------------------------------------------------------------------
[root@sanchuang-linux ~]# python3 canshu.py "hello" "world"
hello world
canshu.py

数据类型

知识点11 数据类型

shell常用数字、字符串、数组

字符串的定义,可以使用单引号,也可以使用双引号,也可以不用引号

示例:字符串的定义__________________
[root@sanchuang-linux ~]# echo abc
abc
[root@sanchuang-linux ~]# a=b
[root@sanchuang-linux ~]# echo $a
b
[root@sanchuang-linux ~]# a="b"
[root@sanchuang-linux ~]# echo $a
b
[root@sanchuang-linux ~]# a='b'
[root@sanchuang-linux ~]# echo $a
b

示例:数字的定义_________________
[root@sanchuang-linux ~]# a=1
[root@sanchuang-linux ~]# a=2

引号区别

知识点12 引号区别:双引号可以识别变量,单引号不可以识别变量

引号区别:双引号可以识别变量,单引号不可以

[root@sanchuang-linux ~]# head -n1 /etc/passwd			#(注:输出passwd第一条)
root:x:0:0:root:/root:/bin/bash
[root@sanchuang-linux ~]# cat /etc/passwd |head -n1		#(注:不建议用这个 2条命令)
root:x:0:0:root:/root:/bin/bash
#!/bin/bash

# 字符串操作
line=`head -n1 /etc/passwd`		#(注:使用反引号``)(注:把命令输出保存在line里面)
echo $line
---------------------------------------------------------------------------------
[root@sanchuang-linux chenpeng]# bash test2.sh 
root:x:0:0:root:/root:/bin/bash

示例:双引号可以识别变量,单引号不可以识别变量_____________________________

echo "字符串为:$line"
字符串为:root:x:0:0:root:/root:/bin/bash
------------------------------------------
echo '字符串为:$line'
字符串为:$line

字符串操作

知识点13 字符串操作

截取
截取前4个字符:echo ${line:0:4}
截取后9个字符  echo ${line:0-9}
从倒数第九个字符开始截取4个字符 echo ${line:0-9:4}
从左向右截取最后一个:后的字符   echo ${line##*:}
从左向右截取第一个:后的字符     echo ${line#*:}
从右往左截取最后一个:后的字符	echo ${line%%:*}
从右向左截取第一个:后的字符	echo ${line%:*}
字符串长度	echo ${#line}
示例:字符串操作_______________________________
# 字符串操作
[root@sanchuang-linux chenpeng]# vim test2.sh
line=`head -n1 /etc/passwd`
echo $line				#(注:root:x:0:0:root:/root:/bin/bash)
echo "字符串为:$line"		#(注:字符串为:root:x:0:0:root:/root:/bin/bash)

echo '字符串为:$line'		#(注:字符串为:$line)

echo "截取前4个字符:"
echo ${line:0:4}			#(注:root)

echo "截取后9个字符"
echo ${line:0-9}			#(注:/bin/bash)

echo "从倒数第九个字符开始截取4个字符"
echo ${line:0-9:4}			#(注:/bin)

echo "从左向右截取最后一个:后的字符"
echo ${line##*:}			#(注:/bin/bash)

echo "从左向右截取第一个:后的字符"
echo ${line#*:}			#(注:x:0:0:root:/root:/bin/bash)

echo "从右往左截取最后一个:后的字符"
echo ${line%%:*}			#(注:root)

echo "从右向左截取第一个:后的字符"
echo ${line%:*}			#(注:root:x:0:0:root:/root)

echo "字符串长度"
echo ${#line}				#(注:31)
-----------------------------------------------
[root@sanchuang-linux chenpeng]# bash test2.sh 
root:x:0:0:root:/root:/bin/bash
字符串为:root:x:0:0:root:/root:/bin/bash
字符串为:$line
截取前4个字符:
root
截取后9个字符
/bin/bash
从倒数第九个字符开始截取4个字符
/bin
从左向右截取最后一个:后的字符
/bin/bash
从左向右截取第一个:后的字符
x:0:0:root:/root:/bin/bash
从右往左截取最后一个:后的字符
root
从右向左截取第一个:后的字符
root:x:0:0:root:/root
字符串长度
31

练习13 截取百度网址

line="http://www.baidu.com/login"

# 截取出:login
echo ${line:0-5}				#(注:取最后5个字符)
echo ${line##*/}				#(注:从左往右最后一个/后的内容)

# 截取出:www.baidu.com/login
echo ${line##*//}

# 截取出:http://www.baidu.com
echo ${line%/*}

# 截取出:http:
echo ${line%%/*}

数值的运算与比较

知识点14 数值的运算与比较

数值的运算:
第一种: $(( 表达式 ))
第二种: $[ 表达式 ]
第三种: expr 表达式

注意表达式运算符左右空格

示例:↓↓↓↓↓↓↓↓↓↓↓↓↓↓
[root@sanchuang-linux ~]# a=10
[root@sanchuang-linux ~]# b=20
[root@sanchuang-linux ~]# $(($a + $b))
-bash: 30: 未找到命令
[root@sanchuang-linux ~]# echo $(($a + $b))
30
[root@sanchuang-linux ~]# echo $[ $a +$b ]
30
[root@sanchuang-linux ~]# expr $a + $b
30
[root@sanchuang-linux ~]# expr $a+$b
10+20

SHELL结构语句,循环和判断

知识点15 shell结构语句,循环和判断

知识点15.1 for循环

for循环

语法1:↓↓↓↓↓↓↓↓
-----------------------
for 变量 in 值1 值2
do
	循环执行语句
done
=======================================
语法2:↓↓↓↓↓↓↓
---------------------------------------
# for ((i=0;i<3;i++))
for ((初始化变量; 结束循环的条件; 运算))
do 
	循环执行的语句
done

知识点15.2 while循环

While循环

语法1:↓↓↓↓↓
---------------------------------------
while read line
do 
	循环执行语句
done
=======================================
语法2↓↓↓↓↓↓↓↓↓
---------------------------------------
while [条件(非必选)]:
do
	循环执行语句
done
=======================================
注:也支持break,continue

知识点15 判断

知识点15.3 if语句

if语句

语法1:↓↓↓↓↓
-------------------------
if 条件
then
	执行语句
fi
=========================
语法2:↓↓↓↓↓
if 条件
then
	执行语句
else
	执行语句
fi
==========================
语法3:↓↓↓↓↓↓
----------------------
if [ command ];then
符合该条件执行的语句
elif [ command ];then
符合该条件执行的语句
else
符合该条件执行的语句
fi

知识点15.4 case语句

case语句

语法:↓↓↓↓↓________________
case $变量名 in
条件1)
	执行语句一
	;;
条件2)
	执行语句二
	;;
*)
esac

练习16

编写一个shell脚本

接收用户输入的两个数,然后选择要对着两个数进行什么计算,并且输出结果

实现菜单选择

================

  1. add 加法

  2. sub 减法

  3. mul 乘法

  4. exit 退出

================

注:菜单选择用case、服务重启脚本用case

示例:↓↓↓↓↓↓↓↓↓_________________________
[root@sanchuang-linux chenpeng]# vim num_test.sh
#!/bin/bash

read -p "请输入数字一:" num1
read -p "请输入数字二:" num2

echo "================"
echo "1.add 加法"
echo "2.sub 减法"
echo "3.mul 乘法"
echo "4.exit 退出"
echo "================"

read -p "请输入你的选择:" options
case $options in
1)
    echo "两数相加为:$(($num1 + $num2))"
    ;;
2)
    echo "两数相减为:$(($num1 - $num2))"
    ;;
3)
    echo "两数相乘为:$(($num1 * $num2))"
    ;;
4)
    echo "退出!"
    exit
esac
-------------------------------------------------------------------------------------------
整成函数形式
add(){
    echo "两数相加为:$(($num1 + $num2))"
}

case $options in
1)
    add			#(注:需要使用的时候调用)
    ;;
2)…………………………

/etc/init.d 服务的启动脚本

知识点17 /etc/init.d 服务的启动脚本

/etc/init.d/ 放着服务的启动脚本

[root@sanchuang-linux chenpeng]# cd /etc/init.d/              
[root@sanchuang-linux init.d]# ls                             
functions  README           
示例:服务重启脚本用case↓↓↓↓↓__________________
case $mode in
	start)
		启动
		;;
	stop)
		关闭(使用kill命令)
		;;
	restart)
		关闭
		启动
		;;
	reload)
		重新加载配置(使用kill -HUP)
		;;
esac            

kill

知识点18 kill

kill 用来删除正在执行中的程序或者工作
kill 可以将指定的信息发送给程序

# kill -l 可以查看kill信号量	(kill -L(小写))
# kill -0 用来检测进程是否存在,当进程不存在时,kill -0 会报错
# kill -1 pid 重新加载进程(常用)
# kill -HUP pid 和  kill -1 pid是一样的
# kill -1 pid  或者  kill -HUP pid  都表示重新加载这个文件
# kill -9 强制杀死
# kill -15 正常停止一个进程
kill 不接信号量的时候,默认为信号15
除了9号信号,其他信号进程都有权利拒绝执行!

注:重新加载 相当于 加载最新的配置 服务还是正常运行的(连接不会断)

	重启 服务会断
示例:↓↓↓↓↓↓↓↓↓____________
[root@sanchuang-linux ~]# kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
…………………………
63) SIGRTMAX-1	64) SIGRTMAX	

SHELL编程 if判断

知识点19 shell编程 if判断

if 判断

示例:↓↓↓↓↓↓____________________________________________________________
[root@sanchuang-linux ~]# if id wenyao; then echo "ok"; else echo "error"; fi
id: “wenyao”:无此用户
error
--------------------------------------------------------
等同于:↓↓↓↓↓________________________________________________
if id wenyao; 
then 
	echo "ok"; 
else 
	echo "error"; 
fi

[ ]

知识点20 [ ]

[ ]表示条件测试

注意这里的空格很重要。要注意在'['后面和']'前面都必须要有空格。


常用判断:
[ -d FILE ] 如果 FILE 存在且是一个目录则返回为真。
[ -f FILE ] 如果 FILE 存在且是一个普通文件则返回为真。
[ -e **** ] 判断文件/文件夹是否存在

字符串判断:
[ -z STRING ] 如果STRING的长度为零则返回为真,即空是真
[ -n STRING ] 如果STRING的长度非零则返回为真,即非空是真
[ STRING1 ]  如果字符串不为空则返回为真,与-n类似
[ STRING1 == STRING2 ] 如果两个字符串相同则返回为真
[ STRING1 != STRING2 ] 如果字符串不相同则返回为真
[ STRING1 < STRING2 ] 如果 “STRING1”字典排序在“STRING2”前面则返回为真。
[ STRING1 > STRING2 ] 如果 “STRING1”字典排序在“STRING2”后面则返回为真。

数值判断
[ INT1 -eq INT2 ] INT1和INT2两数相等返回为真 ,=
[ INT1 -ne INT2 ] INT1和INT2两数不等返回为真 ,<>
[ INT1 -gt INT2 ] INT1大于INT2返回为真 ,>
[ INT1 -ge INT2 ] INT1大于等于INT2返回为真,>=
[ INT1 -lt INT2 ] INT1小于INT2返回为真 ,<
[ INT1 -le INT2 ] INT1小于等于INT2返回为真,<=

逻辑判断
[ ! EXPR ] 逻辑非,如果 EXPR 是false则返回为真。
[ EXPR1 -a EXPR2 ] 逻辑与,如果 EXPR1 and EXPR2 全真则返回为真。
[ EXPR1 -o EXPR2 ] 逻辑或,如果 EXPR1 或者 EXPR2 为真则返回为真。
[ ] || [ ] 用OR来合并两个条件
[ ] && [ ] 用AND来合并两个条件

示例:↓↓↓↓↓↓↓↓↓↓↓↓↓
[root@sanchuang-linux ~]# a=10
[root@sanchuang-linux ~]# b=20
[root@sanchuang-linux ~]# if [ $a -gt $b ];then echo "a>b";else echo "a<b";fi	#(注:正确)
a<b
[root@sanchuang-linux ~]# if [ $a > $b ];then echo "a>b";else echo "a<b";fi	#(注:出错)
a>b						(注:使用2个中括号不出错)
[root@sanchuang-linux ~]# if [[ $a > $b ]];then echo "a>b";else echo "a<b";fi	#(注:正确)
a<b

[root@sanchuang-linux ~]# if [ $a -gt $b ] && [ $a -ne 20 ];then echo "输出a>b";else echo "输出a<b";fi
输出a<b

练习21

判断当前目录下是否存在文件a,没有的话就创建

有的话输出,输出文件已存在

示例:↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
写法1:
if [ -f a.txt ];
    then echo "文件存在"
else
    touch a.txt
fi
-------------------------------------------------
写法2:推荐(类似python的if三元运算符)
[ -f a.txt ] && echo "文件已存在" || touch a.txt

示例2:

编写一个脚本,实现如下功能

==============

1.增加用户并设置密码

2.删除用户

3.查看用户

4.退出

==============

输入的指定不是1-4,给提示给予提醒,并且如果不输入退出的话,可以循环添加。

按1 增加用户,并且设置密码 useradd passwd

按2 删除用户 userdel -r

按3 查看用户 id

按4 退出 exit


&& ||

知识点22 类似python的if三元运算符

使用&& || 来实现

·cmd1 && cmd2 如果cmd1执行成 功,或者为真,则执行cmd2

·cmd1 || cmd2 如果cmd1执行不成功,或者为假,则执行cmd2

·cmd1 && cmd2 || cmd3 如果cmd1执行成功,就执行cmd2,不成功就执行cmd3

示例:上个练习↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
[ -f a.txt ] && echo "文件已存在" || touch a.txt
[[ -f a.txt ]] && echo "文件已存在" || touch a.txt		#(注:推荐使用两个中括号)

示例:
-------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# a=10
[root@sanchuang-linux ~]# b=20
[root@sanchuang-linux ~]# [ $a -gt $b ] && echo "输出 a>b"
[root@sanchuang-linux ~]# [ $a -gt $b ] || echo "输出 a<b"
输出 a<b
[root@sanchuang-linux ~]# [ $a -gt $b ] && echo "输出 a>b" || echo "输出 a<b"
输出 a<b

[] 、[[]]、 (()) (判断方式)

知识点23 [] 、[[]]、 (()) (判断方式)

[ ] 会做单词拆分
[ ] 很多表示都不是很支持,建议使用[[ ]]判断(2个中括号)
总结:
·建议使用[[ ]]来比较运算,进行判断
·字符串用[[ ]] (推荐)
·数字用比较用(( ))
结论:建议使用[[ ]]来比较运算,进行判断

示例1:if判断时
-------------------------------------------------------------------------------------------
[root@sanchuang-linux chenpeng]# name="wen yao"
[root@sanchuang-linux chenpeng]# [ $name == "wen yao" ] && echo "ok" || echo "error"
-bash: [: 参数太多		#(注:自动做单词拆分)
error
[root@sanchuang-linux chenpeng]# [[ $name == "wen yao" ]] && echo "ok" || echo "error"
ok							#(注:推荐使用2个中括号)
[root@sanchuang-linux chenpeng]# [ "$name" == "wen yao" ] && echo "ok" || echo "error"
ok					#(注:使用引号连接在一起,表示一个整体)
============================================================================================

示例2:数值比较
-------------------------------------------------------------------------------------------
[root@mysql-binary shell_test]# echo $a
10
[root@mysql-binary shell_test]# echo $b
20
[root@mysql-binary shell_test]# [[ $a > $b ]] && echo "ok" || echo "error"
error
[root@mysql-binary shell_test]# [ $a > $b ] && echo "ok" || echo "error"
ok							#(注:出错)
[root@mysql-binary shell_test]# (( $a == $b )) && echo "ok" || echo "error" 
error

示例:
--------------------------------------------------------------------------------------------
[root@mysql-binary shell_test]# a=10
[root@mysql-binary shell_test]# b=20
[root@mysql-binary shell_test]# [[ $a -eq $b ]] && echo "ok" || echo "eroor"
eroor
[root@mysql-binary shell_test]# (( $a -eq $b )) && echo "ok" || echo "eroor"
-bash: ((: 10 -eq 20 : 表达式中有语法错误 (错误符号是 "20 ")
Eroor
----------------------------------------------------------------------------
[root@mysql-binary shell_test]# c=102
[root@mysql-binary shell_test]# b=20
[root@mysql-binary shell_test]# [[ $c > $b ]] && echo "ok" || echo "eroor"
eroor
[root@mysql-binary shell_test]# (( $c > $b )) && echo "ok" || echo "eroor"
ok

示例3:if条件判断的2种写法
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# a=10
[root@sanchuang-linux ~]# b=20
[root@sanchuang-linux ~]# if [[ $a > $b ]]; then echo "ok"; else echo "error"; fi
error
[root@sanchuang-linux ~]# [[ $a > $b ]] && echo "ok" || echo "error"
error

示例:字符串比较(( ))  也可以
--------------------------------------------------------------------------------------------
[root@sanchuang-linux ~]# a=abc
[root@sanchuang-linux ~]# b=abc1
[root@sanchuang-linux ~]# (( $a > $b )) && echo "ok" || echo "error"
error
[root@sanchuang-linux ~]# a=abc
[root@sanchuang-linux ~]# b=bac1
[root@sanchuang-linux ~]# (( $a > $b )) && echo "ok" || echo "error"
error
[root@sanchuang-linux ~]# a=abc
[root@sanchuang-linux ~]# b=abc
[root@sanchuang-linux ~]# (( $a == $b )) && echo "ok" || echo "error"
ok

结论:建议使用[[ ]]来比较运算,进行判断

SHELL函数 定义

知识点24 Shell函数 定义

示例:
add() {
    echo "两数相加为:$(( $num1 + $num2 ))"		#(注:函数里面的操作内容)
}
------------------------------------------------
调用的时候 add
case $options in 
1)
	add
	;;
2)……………………
--------------------------------------------------------------------------------------------
add(){
    echo "两数相加为:$(($num1 + $num2))"
}

case $options in
1)
    add			#(注:需要使用的时候调用)
    ;;
2)…………………………

判断方式 [] [[]] (()) test

知识点25 判断方式 [] [[]] (()) test

  • (( )) 判断数字 > < == !=

  • [[ ]] 判断字符串 或者 -eq -ne -gt -lt 判断数字

  • 有些语法 [ ] 是不支持的,建议使用[[ ]]

  • test(测试)判断,等同于1个中括号

示例:test
---------------------------------------------------------------------
[root@sanchuang-linux ~]# a=123
[root@sanchuang-linux ~]# b=123
[root@sanchuang-linux ~]# test a==b && echo ok
ok
[root@sanchuang-linux ~]# test a==b && echo ok || echo error
ok

相关内容