Branches in the shell

Source: Internet
Author: User
If statementSyntax 1
If condition
then command (set)
fi

The condition here is to use the test statement or the bracketed statement (the previous article has been summed up)
Note If statement must terminate with FI

Practice:

#if test
if ["-lt"]  # "13" before a space, "13" after a space.
then
        echo "Yes less then"
else
        echo "NO"
fi
Syntax 2
If condition 1
then 
     command 1
elif condition 2
then 
     command 2
Else
     command 3
fi
#!/bin/bash
#if Test
#this is a comment line
echo "Enter your filename:"
read myfile
if [-e $myf Ile]
then
   if [-S $myfile];then
      echo $myfile exist and size greater than zero '
   else
      Echo ' $MYF Ile exist but size is zero "
   fi
Else
    echo" File no exist "
fi
Case Multiple selection statement

Case multiple Choice Statement format:

Case variable/Expression in
mode 1)
    command 1
    ;;
Mode 2)
    command 2
    ;;
Esac
The case must be followed by a word in; each pattern must end with a closing parenthesis. The value can be a variable or a constant. After a match finds that the value is in accordance with a pattern, all subsequent commands begin to execute until; Pattern Matching * denotes any character;? [..] (Note: Only two points) represent any character in a class or range
#!/bin/bash
#case Select
echo-n "Enter a number from 1 to 3:"
read num
box $num in
1)
        echo "Y OU select 1 "
        ;;
2)
        echo "you select 2"
        ;;
3)
        echo "you select 3"
        ;;
y| Y)
        echo "You select Y"
        ;;
*)
        echo "' BaseName $ ': This is not between 1 and 3" >&2
        exit;
;; Esac
All patterns have precedence, and the order-preference pattern that appears can be written as an expression representing a range such as y| Y cannot write multiple patterns that perform the same operation, like C switch-case, if multiple patterns perform the same operation, combine them with | to indicate that one of them is matched to perform the corresponding branch operation.
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.