Shell Script Basics (ii)

Source: Internet
Author: User

I. Logical judgment in shell Scripts 1, Judgment statement IFA) without else

Format:

if 判断语句;then    commandfi

Instance:

[[email protected] shell]# vim if01.sh          //判断数值大小第一种方法用[],注意前后空格#! /bin/basha=5if [ $a -gt 3 ];then    echo "这个数字大于3"fi#-gt:大于,-lt:小于,-ge:大于或等于,-le:小于或等于,-eq:等于,-ne:不等于[[email protected] shell]# sh if01.sh 这个数字大于3[[email protected] shell]# vim if02.sh                //判断数值大小的第二种方式:(())#! /bin/basha=5if ((a>3));then   echo "这个数字大于3"fi#((a>3)),双括号是shell中的特有格式,只用一个括号或者不用都会报错[[email protected] shell]# sh if02.sh 这个数字大于3
b) with Else

Format:

if 判断语句;then    commandelse   commandfi

Instance:

[[email protected] shell]# vim ifelse.sh #! /bin/bashread -p "请输入分数:" aif [ $a -lt 60 ];then   echo "你没有通过测试!"else   echo "恭喜你,顺利通过测试!"fi#read命令用于和用户交互,它把用户输入的字符串作为变量值 [[email protected] shell]# sh ifelse.sh 请输入分数:60恭喜你,顺利通过测试![[email protected] shell]# sh ifelse.sh 请输入分数:59你没有通过测试!
c) with Elif

Format:

if 判断语句1;then   commandelif 判断语句2;then   commandelse   commandfi

Instance:

[[email protected] shell]# vim elif.sh #! /bin/bashread -p "请输入分数:" aif (($a<60));then   echo "未通过测试。"elif ((a>=60))&&(($a<85));then   echo "通过测试,成绩良好。"else   echo "通过测试,成绩优秀!"fi# &&表示并且,当然也可以用||表示或者[[email protected] shell]# sh elif.sh 请输入分数:60通过测试,成绩良好。[[email protected] shell]# sh elif.sh 请输入分数:34未通过测试。[[email protected] shell]# sh elif.sh 请输入分数:99通过测试,成绩优秀!
2, and document-related judgments

The If hi in the shell is often used to determine document properties. Common options are:

-E: Determine whether a file or directory exists;
-D: Determine if it is a directory and whether it exists;
-F: Determine if it is a file and whether it exists;
-R: Determine whether there is read permission;
-W; Determine if there is write permission;
-X: Determines whether there are execute permissions.

Format:

if [ -e filename ];then   commandfi

Instance:

[[email protected] shell]# if [ -d /home/ ]; then echo ok; fiok[[email protected] shell]# if [ -f /home/ ]; then echo ok; fi                //home是目录不是文件,所以没有输出ok[[email protected] shell]# touch /root/test.txt[[email protected] shell]# if [ -f /root/test.txt ]; then echo ok; fiok[[email protected] shell]# if [ -r /root/test.txt ]; then echo ok; fiok[[email protected] shell]# if [ -w /root/test.txt ]; then echo ok; fiok[[email protected] shell]# if [ -x /root/test.txt ]; then echo ok; fi[[email protected] shell]# if [ -e /root/test1.txt ]; then echo ok; fi[[email protected] shell]# if [ -d /root/test.txt ]; then echo ok; fi
3. Case Logic Judgment

Format:

case 变量 invalue1)              command                            ;;value2)              command                            ;;value3)              command                            ;;*)              command                            ;;esac

Does not limit the number of values, * denotes other value
Instance:

[[email protected] shell]# vim case.sh#! /bin/bashread -p "请输入一个数字:" na=$[$n%2]case $a in  1)      echo "这数字是奇数"      ;;  0)      echo "这数字是偶数"      ;;  *)      echo "这个不是数字"      ;;esac[[email protected] shell]# sh case.sh请输入一个数字:3这数字是奇数[[email protected] shell]# sh case.sh请输入一个数字:2这数字是偶数
4. Practice (Enter exam results, judge score level)
[[email protected] shell]# vim test01.sh#!/bin/bashread-p "Please input a number:" Nif [-Z "$n"]then echo "Ple    ASE input a number. " Exit # "Exit 1" indicates the return value after executing the partial command # that is, after the command executes, use the value of echo $ Fin1= ' echo $n |sed ' s/[0-9]//g ' #判断用户输入的字符是否为纯数字 # If it is a number, replace it with a null, assign to $ n1if [-N "$n 1"]then echo "please input a number." Exit # determines that $N1 is not empty (that is, $n is not a pure number) again prompts the user to enter the numbers and exits fi# if the user enters a pure number, execute the following command: if [$n- LT] && [$n-ge 0]then tag=1elif [$n-ge] && [$n-lt]then Tag=2elif [$n-ge] &A mp;& [$n-lt]then Tag=3elif [$n-ge] && [$n-le the]then tag=4else Tag=0fi#tag is set for judging conditions    Label, convenient to refer to case later $tag in 1) echo "not OK";;    2) echo "OK";;    3) echo "ook";;    4) echo "Oook";;        *) echo "The number range is 0-100." ;; Esac[[email protected] shell]# sh test01.sh Please input a number:aaplease input a number. [[email protected] shell]# sh test01.sh please input aNumber:78ok[[email protected] shell]# sh test01.sh Please input a number:90oook[[email protected] shell]# sh t est01.sh Please input a number:11not OK

Shell Script Basics (ii)

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.