Analyzing logic operations shell ---

Script execution parameters to determine whether the two numbers, compare the size

Here Insert Picture Description

[root@lb01 ~]# vim /server/scripts/1-13.sh 

#!/bin/bash
num1=$1
num2=$2
[ $# -ne 2 ] && {
echo "usage: $0 "
exit
}
expr "$num1" + "$num2" + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "must have two number"
exit
}

[ "$num1" -gt "$num2" ] && echo ">"
[ "$num1" -lt "$num2" ] && echo "<"
[ "$num1" -eq "$num2" ] && echo "="
[ "$num1" -ne "$num2" ] && echo "!="

Determining whether the value of two parameters

expr "$num1" + "$num2" + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "must have two number"
exit

expr is determined whether the parameter value is a value, then the operator returns 0, if the return value is not 0 represents just executed to verify whether the stone value fails, the parameter value just executed is not pure

logic operation


[root@lb01 ~]# [ -d /etc -a -f /etc/host ] && echo "1 chengli" || echo "0 buchengli"
0 buchengli
2020年 01月 13日 星期一 09:43:40 CST
[root@lb01 ~]# [ -d /etc -a -f /etc/hosts ] && echo "1 chengli" || echo "0 buchengli"
1 chengli
[root@lb01 ~]# [ -d /etc -o -f /etc/hosts ] && echo "1 chengli" || echo "0 buchengli"
1 chengli
[root@lb01 ~]# [ -d /etc -o -f /etc/ho ] && echo "1 chengli" || echo "0 buchengli"
1 chengli

10-100 Digital judgment

[root@lb01 ~]# cat /server/scripts/qq.sh 
#!/bin/bash
num=$1
[[ $num -ge 10 && $num -le 100 ]] && echo $num || echo "不符合条件"
[root@lb01 ~]# sh /server/scripts/qq.sh 222
不符合条件
[root@lb01 ~]# sh /server/scripts/qq.sh 22
22

Analyzing digital <10> 100

[root@lb01 ~]# cat /server/scripts/qq.sh 
#!/bin/bash
num=$1
[[ $num -lt 10 || $num -gt 100 ]] && echo $num || echo "不符合条件"
2020年 01月 13日 星期一 09:54:10 CST
[root@lb01 ~]# sh /server/scripts/qq.sh 22
不符合条件
2020年 01月 13日 星期一 09:54:18 CST
[root@lb01 ~]# sh /server/scripts/qq.sh 2
2
2020年 01月 13日 星期一 09:54:20 CST
[root@lb01 ~]# sh /server/scripts/qq.sh 222
222
2020年 01月 13日 星期一 09:54:22 CST

Here Insert Picture Description

[root@lb01 ~]# [[ 123 =~ ^[a-z]+$ ]] && echo 1 || echo 0
0
[root@lb01 ~]# [[ dddwdw =~ ^[a-z]+$ ]] && echo 1 || echo 0
1

[root@lb01 ~]# [[ 123 =~ ^[0-9]+$ ]] && echo 1 || echo 0
1
[root@lb01 ~]# [[ 12sdf3 =~ [0-9] ]] && echo 1 || echo 0
1

Determining whether the number 1 or 2

[root@lb01 ~]# cat /server/scripts/www.sh 
#!/bin/bash
s=$1
#[ $s -eq 1 -o $s -eq 2 ] && echo $1 || echo "既不是1也不是2" 
[[ ! "$s" =~ ^[12]$ ]] && {
echo input errpr
exit 1
}
[ "$s" -eq 1 ] && echo 1
[ "$s" -eq 2 ] && echo 2


Here Insert Picture Description

Supplementary :

Interactive variable assignment

[root@lb01 ~]# read -p 'input passwd:' pass
input passwd:123456
2020年 01月 13日 星期一 10:47:28 CST
[root@lb01 ~]# echo $pass
123456

To multiple variable assignment

[root@lb01 ~]# read -p 'input passwd:' n1  n2
input passwd:10 20
[root@lb01 ~]# echo $n1
10
[root@lb01 ~]# echo $n2
20
[root@manager ~]# cat  /server/scripts/2.8-1-comp-read.sh
#!/bin/bash
#author: oldboy

read -p "input num1 num2:"  num1 num2

#number
#expr $num1  + $num2 + 1   &>/dev/null 
#[ $? -ne 0 ] && {
[[ "$num1" =~ ^[0-9]+$ && "$num2" =~ ^[0-9]+$ ]] ||  {
echo "Usage:  Must have  two number" 
exit 2
}
#compare 
[ $num1 -gt $num2 ] && echo  "$num1 -gt $num2"
[ $num1 -lt $num2 ] && echo  "$num1 -lt $num2"
[ $num1 -eq $num2 ] && echo  "$num1 -eq $num2"
```



#ctrl + z 让进程去后台挂起 (暂停)
[root@manager ~]# sleep 666 
^Z
[2]+  已停止               sleep 666


#bg 让后台挂起的 继续运行  background  
[root@manager ~]# bg 
[2]+ sleep 666 &
[1]   完成                  sleep 99

#查看后台运行 
jobs 

#应用场景:杀掉 顽固进程 
##执行顽固进程
##ctrl + z 
##kill %1 #数字

Conditional check crond service is open

status status

Here Insert Picture Description
ps -ef process

[root@lb01 ~]# cat /server/scripts/ding.sh 
#!/bin/bash
. /etc/init.d/functions
cnt=`ps -ef|grep crond |grep -v grep |wc -l`
if [ $cnt -ge 1 ]
then
action "running" /bin/true
else
action "failed" /bin/false
fi
Published 15 original articles · won praise 0 · Views 235

Guess you like

Origin blog.csdn.net/weixin_45581487/article/details/103952449