Conditional judgment statements in the shell If~then~fi

Source: Internet
Author: User
Tags touch uppercase letter


The conditional judgment statement in the shell is an upgrade to the previous "conditional test statement in the shell", meaning that the preceding test statement serves the IF~THEN~FI statement for the current judgment statement.

We're still rolling it out in the form of attention points and code implementations:

1) The basic IF-THEN-FI statement can be used to judge the basic single-layer branching structure in the following form:

The test statement after if is generally done using the [] command. As in the following example:

<span style= "FONT-SIZE:14PX;" >#-----------------------------/chapter4/ex4-18.sh------------------
#!/bin/sh
#使用条件测试判断/bin/ Whether bash is a regular file
if [-f/bin/bash]; then 
	echo "/bin/bash is a file"
fi
</span>

2) The IF-THEN-ELSE-FI statement can handle two-level branch judgment statements. as follows:


<span style= "FONT-SIZE:14PX;" >#-----------------------------/chapter4/ex4-22.sh------------------
#!/bin/sh

#输出提示信息
echo " Please enter a number: "
#从键盘读取用户输入的数字
read num
#如果用户输入的数字大于10
if [" $num "-GT 10]; Then
		#输出大于10的提示信息
 	Echo, "the number is greater than."
#否则
Else
	#输出小于或者等于10的提示信息
		echo "The number is equal-or less than."
Fi
</span>


3) If-then-elif-then-elif-then-...-Else-fi. This statement can achieve multiple judgments, and note that you must end with an else. as follows:


<span style= "FONT-SIZE:14PX;" >#-----------------------------/chapter4/ex4-24.sh------------------
#!/bin/sh

echo "Please enter a Score: "

read score

if [-Z" $score "]; Then
   echo, "you enter nothing." Please enter a score: "
   read score
else
   if [" $score "-lt 0-o" $score "-GT 100];  Then
      echo "The score should is between 0 and 100.Please enter again:"
      read score
   else
      #如果成绩大于90
      if ["$score"-ge 90]; Then
         Echo, "The grade is A."
      #如果成绩大于80且小于90
      elif ["$score"-ge]; then
         echo "The grade is B."
      #如果成绩大于70且小于80
      elif ["$score"-ge]; then
         echo "The grade is C."
      #如果成绩大于60且小于70
      elif ["$score"-ge]; then
         echo "The grade is D."
      #如果成绩小于60
      Else
         echo "The grade is E."
      Fi fi fi
</span>

4) to exit the program, you can use the Exit Status statement. An exit status of 0 means that the script runs at the end of the run, and 0 indicates that there are some errors in the execution of the program, and that different errors correspond to different exit states. Although the user can customize the exit state status in the program, usually each status has a specific meaning, so be sure to avoid ambiguity when customizing to return to status. Examples are as follows:

<span style= "FONT-SIZE:14PX;" >01   #-----------------------------/chapter4/ex4-26.sh------------------   #!/bin/sh
03   #如果文件已经存在, the first parameter that is entered when the file name is directly exited,   if [-E "$"] "then"      echo "file $ exists ."      exit 1 #如果文件不存在, create a file,   use touch to create a file, or use redirection to create a file echo "hello~" >./test.log  That is, create a new Test.log file in the current directory. The other one      Touch "$" "
file $      been created."      exit 0   fi
</span>

5) CASE-ESAC statement to achieve multiple conditional judgment. as follows:


Note: At the end of each variable's content, you need two semicolons (;;) to represent the end of the paragraph of the program, the end of each variable case), the remainder is represented by the *), and finally the ESAC to the end, that is, the cases.

<span style= "FONT-SIZE:14PX;" >#-----------------------------/chapter4/ex4-27.sh------------------
#!/bin/sh

#输出提示信息
echo "hit A key,then hit return. "
#读取用户按下的键
Read KeyPress
#case语句开始 case
"$keypress" in
   #小写字母
   [[: Lower:]])
      echo "lowercase Letter. ";;
   #大写字母
   [[: Upper:]]
      echo "uppercase letter.";;
   #单个数字
   [0-9])
      echo "Digit.";;
   #其他字符
   *)
      echo "other letter.";;
Esac
</span>


Reference:

"Brother Bird's Linux private dish"

"Shell from Beginner to mastery"



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.