UNIXShell control structure-IF

Source: Internet
Author: User
UNIXShell control structure & mdash; IF flow control (DecisionMaking) IF statements have three formats: first, if...fistatementwww.2cto.com. The following is an instance: [plain] catif1.sh #! /Bin/sha10b20 # ① if [$ a-eq $ B... UNIX Shell control structure-IF flow control (demo-making) IF statements have three formats: first, if... fi statement www.2cto.com is an instance: [plain] cat if1.sh #! /Bin/sh a = 10 B = 20 # ① if [$ a-eq $ B]; then echo "a is equal to B "; fi if [$ a-gt $ B]; then echo "a is great than B "; fi # ② if [$ a-lt $ B] then echo "a is less than B"; fi # the EOF note: the condition www.2cto.com ① is separated from the processing command: if condition; then Processing Command fi ② another way to separate the condition from the processing command: if condition then Processing Command fi here needs to be selected according to your habits. In the above example, the value of the variable is left dead. to pass two parameters to this script, you can modify it as follows: [plain] cat if1.sh #! /Bin/sh # a = 10 # B = 20 if [$1-eq $2]; then echo "the first number is equal to the second "; fi if [$1-gt $2]; then echo "the first number is great than the second "; fi if [$1-lt $2] then echo "the first number is less than the second"; fi # the EOF transmits parameters to the script, you only need to add it after the sh command and separate it with spaces: [plain] sh if1.sh 1 2 the first number is less than the second sh if1.sh 12 1 the first number is great than the se Cond sh if1.sh 1 1 the first number is equal to the second: if... else... fi, as follows: if [expression] then statement (s) to be sxecuted if expression is trueelse statement (s) to be sxecuted if expression is not truefi a simple instance [plain] cat ifparam. sh #! /Bin/sh if [$ #-lt 3]; then echo "Usage: 'basename $ 0' arg1 arg2 arg3"> & 2 exit 1 fi # EOF echo "arg1: $1 "echo" arg1: $2 "echo" arg1: $3 "script annotation: 1. $ # indicates the number of input parameters. 2. basename $0 print the file name 3. if the input is smaller than three parameters, an information is output, which is treated as an error message (> & 2 ). Run the script sh ifparam. sh scott tomUsage: ifparam. sh arg1 arg2 arg3 sh ifparam. sh scott tom jimarg1: scottarg1: tomarg1: jim Let's take another test: [plain] cat ifeditor. sh #! /Bin/csh # if [-z $ EDITOR] ① # if [-z "'echo $ editor'"] ③ # the following statement cannot calculate the environment value correctly # because wc-c calculation includes new blank rows # if ['echo $ EDITOR | wc-C'-eq 0] ② if [-z "'echo $ editor'"] then echo "Your EDITOR environment is not set" else echo "Using $ EDITOR as the default editor" fi # EOF sh ifeditor. shYour EDITOR environment is not set here three methods are used to check whether environment variables are set; ① direct error: Syntax error ② correct writing, test detection, -z indicates that if it is set, the return value of the length is 0 and the return value is true. ③ wc-c calculation includes null rows, so it is inaccurate. third Type: if... elif... fi, details: [plain] if [expression 1]; then statement (s) to be sxecuted if expression 1 is true elif [expression 2]; then statement (s) to be sxecuted if expression 2 is true elif [expression 3]; then statement (s) to be sxecuted if expression 3 is true else statement (s) to be sxecuted if no expression is true fi below is an example of comparing two numbers: [plain] cat elif. sh #! /Bin/sh if [$1 = $2]; then echo "the first number is equal to the next" elif [$1-gt $2]; then echo "the first number is great than the next" elif [$1-lt $2]; then echo "the first number is great than the next" else echo "None of the condition met" fi # EOF is null when no number is entered. the result is as follows: sh elif. shthe first number is equal to the next when the number is entered, as follows: sh elif. sh 10 20elif. sh: test: unknown operator = This situation, We can input the error information into a file, as follows: sh elif. sh 10 20> log.txt 2> & 1cat log.txt elif. sh: test: unknown operator = when "=" is changed to-eq, the result is as follows: sh elif. sh 10 20the first number is less than the next is an if instance, including the three command formats. the script is used to create a directory. if no value is input, the description of the script is printed. if you enter the description, the system prompts whether to create the script. if you do not enter the description, an error is returned. Otherwise, follow the prompts. [Plain] #! /Bin/sh DIR = $1 if ["$ DIR" = ""]; then echo "Usage: 'basename $ 0' directory to create "> & 2 exit 1 fi if [-d $ DIR]; then echo "Directory $ DIR exists" else echo "The Directory does exist" echo-n "Create it now? [Y .. n]: "read ANS if [" $ ANS "=" y "] | [" $ ANS "=" Y "]; then echo "creating now" mkdir $ DIR> log.txt 2> & 1 if [$? -Ne 0]; then echo "Errors creating the directory $ DIR"> & 2 exit 1 fi echo "Creating successful" elif ["$ ANS" = "n"] | ["$ ANS" = "N"]; then echo "Giving up creating directory $ DIR" else echo "Bad input" fi # EOF 1. do not enter the sh ifmkdir parameter. shUsage: ifmkdir. sh directory to create2. enter an existing directory, prompting that the directory already exists: sh ifmkdir. sh testDirectory test exists: [-d test] echo $? 0 create the test1 Directory: sh ifmkdir. sh test1The Directory does existCreate it now? [Y.. n]: ycreating nowCreating successful3. run the script, but you do not want to create the Directory: sh ifmkdir. sh test2The Directory does existCreate it now? [Y.. n]: nGiving up creating directory test24. when the script is executed, enter sh ifmkdir. sh test2The Directory does existCreate it now? [Y. n]: dBad input

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.