Shell Programming if statement (simple calculator scripting)

Introduction (three basic types of if statements)

In a simple Shell script by the proper use of the if statement (if ...... so ......), Shell scripts can have a certain "common sense", according to different conditions to complete various administrative tasks, if basic sentence structure three types.

A single branch of the if statement

For single-branch structure, only if "conditions are met" will execute the corresponding code, or do nothing.
Here Insert Picture Description

1.1 syntax
if 条件测试操作
then
	命令序列 
fi
1.2 Application example of a single limb

For example :

(1) create a shell script jiaoben.sh.

[root@localhost opt]# touch jiaoben.sh

(2) View opt directory with ls found no folders.

[root@localhost opt]# ls
jiaoben.sh  rh
[root@localhost opt]# vi jiaoben.sh 

(3) if the statement is written in jiaoben.sh: If no aaa folder you created aaa opt directory folder.

#!/bin/bash
if [ ! -e "/opt/aaa/" ]
        then
                mkdir /opt/aaa
fi

(4) execute the script, see the opt directory found aaa folder has been created.

[root@localhost opt]# source jiaoben.sh 
[root@localhost opt]# ls 
aaa jiaoben.sh  rh
[root@localhost opt]# 

Second, the two-branch if statement

For dual branch structure, when "the condition is not satisfied", else it will conduct another operation.
Here Insert Picture Description

2.1 syntax
if 条件测试操作
then 命令序列1
else 命令序列2
fi
2.2 Application example of the bifurcation

For example :

(1) In the above jiaoben.sh we write the script if statement: If you do not opt ​​aaa file directory folder then create a folder aaa, aaa If you already have files in the directory then opt to create aaabbb folder.

#!/bin/bash
if [ ! -e "/opt/aaa/" ]
        then
                mkdir /opt/bbb
        else
                mkdir  /opt/aaabbb
fi

(2) to opt to view the directory with ls, found no folders, perform jiaoben.sh script, find the folder created aaa, aaa when the folder already exists, then execute the script, found to create aaabbb folder.

[root@localhost opt]# ls
jiaoben.sh  rh
[root@localhost opt]# source jiaoben.sh 
[root@localhost opt]# ls
aaa  jiaoben.sh  rh
[root@localhost opt]# source jiaoben.sh 
[root@localhost opt]# ls
aaa  aaabbb jiaoben.sh  rh
[root@localhost opt]# 

Third, multi-branch if statement

For a multi-branched structure, i.e. a plurality of nested condition in the if condition with elif. They are able to perform different operations in accordance with a plurality of mutually exclusive conditions.
Here Insert Picture Description

3.1 syntax
if 条件测试操作1
then 命令序列1
elif 条件测试操作2
then 命令序列2
else 
命令序列3
fi
3.2 Application example multibranched

For example :

(1) a simple calculator written

#!/bin/bash
echo "----------------------------------"
aaa=欢迎使用XX的计算器
echo  "$aaa"
echo "----------------------------------"

  read -p  "请输入第一个整数:"  bbb
  read -p  "请输入你需要进行的运算,加法(+),减法(-),乘发(x),除法(/),取余(%):"  ccc
  read -p  "请输入第二个整数: " ddd
if [ $ccc = "+" ]
   then
       num=`expr $bbb + $ddd`
   echo  "$bbb+$ddd结果为$num"

elif [ $ccc = "-" ]
   then
       num=`expr $bbb - $ddd`

  echo  "$bbb-$ddd结果为$num"


elif [ $ccc = "x" ]
        then
                num=`expr $bbb \* $ddd`
   echo  "$bbb x $ddd 结果为$num"


elif [ $ccc = "/" ]
        then
                num=`expr $bbb / $ddd`
   echo  "$bbb / ddd结果为$num"

elif [ $ccc = "%" ]
        then
                num=`expr $bbb % $ddd`
  echo   "$bbb % $ddd结果为$num"
else 
   echo "请输入正确的运算"
fi

(2) execute the script
Here Insert Picture Description

Precautions

1, in the shell, then and if are separate statement on the same line if you want to enter inside, you need to use a semicolon to separate them.

2, [] indicates the test conditions, after the note "[" and front "]" must include a space.

3. If you simply use the> or <number, the system will be considered as output or input redirection, although it is possible to display the correct result, but actually wrong, need to add turn and symbol in front of "\" in turn meaning.

Published 43 original articles · won praise 56 · views 7923

Guess you like

Origin blog.csdn.net/weixin_42953006/article/details/103266154