Linux-based Shell programming (2)-conditional judgment, arithmetic operations, testing

Source: Internet
Author: User
Tags arithmetic shebang

What if the condition is judged in bash?

Condition test Type

    • Integer test
    • Character test
    • File test

Expressions for conditional tests

There are three kinds

[ expression ]  --方括号与表达式之间一定要有一个空格[[ expression ]] test expression
Integer test
-eq: 测试两个整数是否相等  例:$A -eq $B-ne: 测试两个整数是否不等  不等为真,相等为假-gt: 测试一个数是否大于另一个数;大于,为真;否则,为假;-lt: 测试一个数是否小于另一个数;小于,为真;否则,为假;-ge: 大于或等于-le: 小于或等于
The logical relationship between commands
逻辑与: &&    第一个条件为假时,第二条件不用再判断;    第一个条件为真时,第二条件必须得判断;逻辑或: ||    第一个条件为假时,第二条件必须得判断;    第一个条件为真时,第二条件不用再判断;

Example: Comparing two number sizes

[root @iZ28g26851kZ  ~]# a=< Span class= "Hljs-number" >12  [Root @iZ28g26851kZ  ~]# b= 15  [Root @iZ28g26851kZ  ~]# [ $A -gt  $B ] && echo " a relatively large " | | echo  "b relatively large"  b larger [root @iZ28g26851kZ  ~]# A=20  [Root @iZ28g26851kZ  ~]# [  $A -gt  $B ] && echo " a relatively large " | | echo  "b relatively large"  a larger [Root @iZ28g26851kZ  ~]#  

If a is greater than B is [ $A -gt $B ] true, it needs to be executed (judged) echo "A比较大"
Finally [ $A -gt $B ] && echo "A比较大" true, so no more executionecho "B比较大"

If statement--– single branch
if 判断条件 ;then    statement1    ...fi
--– Dual Branch
if 判断条件 ;then    statement1    ...else    statement2    ...fi
--– Multi-Branch
if 判断条件 ;then    statement1    ...elif 判断条件2 ; then    statement2    ...elif 判断条件3 ; then    statement3    ...else    statement4    ...fi

Example: To determine whether the user User1 exists, there is a display user already exists, does not exist the user is created, the password and user name are the same

adduser.sh

#!/bin/bash  name=user1if  ID  $NAME  &>/dev/null; then  echo   "user already exists"  else  echo   "current Total user ' wc-l/etc/passwd | Cut-d " "-f1 "People"  echo  " user does not exist, creating user " useradd  $NAME   echo   $NAME  | passwd--stdin  $NAME  &>/dev/null echo   "current Total user ' wc-l/etc/passwd | Cut-d "  fi  

Execution results

[root@iZ28g26851kZ ~]# ./adduser.sh 当前一共有用户30人用户不存在,正在创建用户当前一共有用户31人[root@iZ28g26851kZ ~]# ./adduser.sh 用户已存在[root@iZ28g26851kZ ~]# 
Arithmetic operations

As I said before, all the variables in the shell are strings, so the arithmetic operation has to be handled by the command.

Let

Let arithmetic expression
Cases:

[root@iZ28g26851kZ ~]# b=23[root@iZ28g26851kZ ~]# a=12[root@iZ28g26851kZ ~]# let c=$a+$b[root@iZ28g26851kZ ~]# echo $c35[root@iZ28g26851kZ ~]# 
$[Arithmetic expression]

Cases:

[root@iZ28g26851kZ ~]# c=$[$a+$b*2][root@iZ28g26851kZ$c58[root@iZ28g26851kZ
$ (arithmetic expression)

Cases:

[root@iZ28g26851kZ ~]# c=$(($a*2+$b*2))[root@iZ28g26851kZ$c70[root@iZ28g26851kZ
Expr

Expr is generally used for integer operations, which are automatically rounded and removed directly after the decimal point.
And there is a space between the operand and the operation symbol, and if there is no space, it will look like this

[root@iZ28g26851kZ ~]# expr 10/310/3[root@iZ28g26851kZ ~]# 

Output as is, so you have to add a space
Cases:

[root@iZ28g26851kZ ~]# expr 10 / 33[root@iZ28g26851kZ ~]# expr 10 + 313[root@iZ28g26851kZ ~]# expr 10 - 37[root@iZ28g26851kZ ~]# expr 10 * 3expr: syntax error

Can see, add, subtract, except, there is no problem, but the multiplication operation actually said grammatical error,
Okay, here's expr. * When matching characters are used, you need to escape

[root@iZ28g26851kZ ~]# expr 10 \* 330

OK, so it's done.

File test
-e FILE: 测试文件是否存在-f FILE: 测试文件是否为普通文件-d FILE: 测试指定路径是否为目录-r FILE: 测试当前用户对指定文件是否有读取权限-w FILE: 测试当前用户对指定文件是否有写入权限-x FILE: 测试当前用户对指定文件是否有执行权限

Example: Determine if a file exists, the number of rows is displayed

#!/bin/bashFILENAME=/etc/passwdif-e$FILENAMEthen   echo"文件存在"   echo"文件一共有` wc -l $FILENAME | cut -d"" -f1`行"else   echo"文件不存在"fi
How to exit Scripts halfway

Use Exit #即可 where you want to exit, #号为一个退出状态 (a value of 0-255).

0:表示脚本执行成功1-255:表示执行失败也可以什么都不返回,脚本会默认返回上一条结果的执行状态

Cases:
exittest.sh

#!/bin/bashecho"第一行"echo"第二行"exit18echo"第三行"

Execution results

[root@iZ28g26851kZ ~]# ./exittest.sh 第一行第二行[root@iZ28g26851kZ ~]# echo $?18[root@iZ28g26851kZ ~]# 
Show the process of executing a script

Sometimes, when a script is too long and cannot be quickly found after an error, you can use it bash -x xxx.sh to execute the script, which displays the process of executing the script.
Example: Judging whether a file exists

filetest.sh

#!/bin/bash#FILENAME=/etc/passwdif-e$FILENAME ];then  echo"文件存在"else  echo"文件不存在"fi

Perform

[[email protected] ~]# bash -x filetest.sh ‘[‘-e‘]‘echo $‘\346\226\207\344\273\266\345\255\230\345\234\250‘文件存在[[email protected] ~]# 

This makes it easy to check the script.

Character Test Equality comparison
stringstring ]

Remember, [space string space = = Space string space]
No one can not do oh ~ ~
Cases:

[root@iZ28g26851kZ"asd""asdd" ][root@iZ28g26851kZ$?1[root@iZ28g26851kZ"asd""asd" ][root@iZ28g26851kZ$?0[root@iZ28g26851kZ
Unequal comparison
stringstring ]

Cases:

[root@iZ28g26851kZ"asd""asd" ][root@iZ28g26851kZ$?1[root@iZ28g26851kZ"asd""asdd" ][root@iZ28g26851kZ$?0[root@iZ28g26851kZ

Linux-based Shell programming (2)-conditional judgment, arithmetic operations, testing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.