Shell learns the next day

Source: Internet
Author: User
Tags function definition

Condition test

The command test or [can test whether a condition is true, if the test result is real, the exit status of the command is 0, if the test result is false, the exit status of the command is 1 (note the exact opposite of the logical representation of the C language).

Val=10test $val-gt 9 echo $?test $val-gt 10echo $? [$val-gt 9]echo $? [$val-GT 10] The #[command parameter "$val-GT 10]" must be opened with a space interval between echo $?

Note: GT (greater than), LT (less than), eq (equals), GE (greater than or equal), le (less than equals)

The results are as follows:

650) this.width=650; "style=" WIDTH:300PX;HEIGHT:57PX; "src=" http://s2.51cto.com/wyfs02/M00/85/9E/ Wkiom1eqhmsyfmjqaaahw7r4ry0045.png-wh_500x0-wm_3-wmp_4-s_78091049.png "title=" 1.png "border=" 0 "hspace=" 0 "vspace= "0" width= "height=" alt= "Wkiom1eqhmsyfmjqaaahw7r4ry0045.png-wh_50"/>

Test commands with and or without

[ ! EXPR] #! Represents a logical anti-[expr1-a EXPR2] #-A representation of logic with [Expr1-o EXPR2] #-O representation of logic or
[$val-gt 9]echo $? [$val-gt]echo $? [$val-gt 9-a $val-gt $]echo? [$val-gt 9-o $val-gt $]echo? [! $val-GT 9]echo $?

Results:

650) this.width=650; "style=" WIDTH:300PX;HEIGHT:109PX; "src=" http://s2.51cto.com/wyfs02/M02/85/9E/ Wkiom1eqi32ctk7uaaajpwcodj8642.png-wh_500x0-wm_3-wmp_4-s_3488666883.png "title=" 2.png "border=" 0 "hspace=" 0 " Vspace= "0" width= "height=" 109 "alt=" Wkiom1eqi32ctk7uaaajpwcodj8642.png-wh_50 "/>

2. If/then/elif/else/fi

Similar to the C language, in the shell with If, then, elif, else, fi these commands to achieve branch control if the two commands are written in the same line, you need to use it, the number is separated, a line only write one command will not need to write; The shell is automatically renewed, and the next line is followed by then and treated as a command. As with the command, be aware that the command and parameters must be separated by a space. If the parameter of the IF command consists of a sub-command, if the subcommand's exit status is 0 (true), then the subcommands that follow then are executed, and if exit status is not 0 (false), then the subcommands after elif, else, or fi are executed. The subcommand after the if is usually a test command, but it can also be another command.

echo "If you is a biy,please enter ' Boy '" echo "if you are a girl,please enter ' girl '" read sex #read的作用是等待一行字符串, the word string into the shell variable if ["X$sex" = "Xboy"];then echo "You is a good boy!" elif ["x$sex" = "Xgirl"];then echo "You ar e a beautiful girl "else echo" I have no words "fi

Results:

650) this.width=650; "style=" width:300px;height:101px; "src=" http://s3.51cto.com/wyfs02/M02/85/9E/ Wkiol1eqlfsqknjaaaawce8u-nu307.png-wh_500x0-wm_3-wmp_4-s_3413039171.png "title=" 4.png "border=" 0 "hspace=" 0 " Vspace= "0" width= "height=" 101 "alt=" Wkiol1eqlfsqknjaaaawce8u-nu307.png-wh_50 "/>

&& | | Used to connect two commands, while the-A and-o above are used only to connect two test conditions in the test expression, the difference between them is shown below

echo "1+1=?,please Enter your answer" read Val [$val-eq 2]&& {echo "You is right!"} echo "You are wrong" ech O "1+1=?,please Enter your answer" read Val [$val-eq 2]| | {echo "You are right!"} echo "You are wrong"

Results:

650) this.width=650; "style=" width:300px;height:162px; "src=" http://s5.51cto.com/wyfs02/M00/85/9F/ Wkiol1eqmqci51wqaaaebcjumdo685.png-wh_500x0-wm_3-wmp_4-s_1445158781.png "title=" 3.png "border=" 0 "hspace=" 0 " Vspace= "0" width= "height=" 162 "alt=" Wkiol1eqmqci51wqaaaebcjumdo685.png-wh_50 "/>

Looping statements

For I in {A.. F}do echo "$i" done

The notation of Class C

for ((i=0; i<10; i++)) does echo "$i" done

While/do/done

echo "Enter your lucky nums" read Nums while [! $nums = "All"] do echo "Again" read Nums done

Results

650) this.width=650; "style=" width:300px;height:176px; "src=" HTTP://S1.51CTO.COM/WYFS02/M01/85/A0/WKIOM1EQNBSSEPJ _aaarwtjjnva260.png-wh_500x0-wm_3-wmp_4-s_2979757196.png "title=" 5.png "border=" 0 "hspace=" 0 "vspace=" 0 "width=" 300 "Height=" 176 "alt=" Wkiom1eqnbssepj_aaarwtjjnva260.png-wh_50 "/>

Function
Similar to the C language, there is also the concept of a function in the shell, but there is no return value in the function definition and no argument list. For example: Note that the left curly brace {and subsequent commands must have spaces or line breaks between the function body, and if the last command and the closing curly brace} are written on the same line, the end of the command must have; A shell function that does not have a parameter list does not mean that arguments cannot be passed, in fact , a function is like a mini script that can be passed when calling a function
Any parameter (requires a function keyword in front of it)

6. How to debug shell scripts
The shell provides some options for debugging scripts, as follows:
-N: Read the command in the script but not execute it to check for syntax errors in the script
-v-x: Provides trace execution information, prints each command and result in sequence, using these options in three ways,

(1) Providing parameters on the command line
$ sh-x./script.sh
(2) Provide parameters at the beginning of the script

#! /bin/sh-x

(3) Enable or disable parameters with the SET command in the script
The set-x and set +x represent L and disable the-x parameter respectively, so that only one segment of the script can be tracked for debugging.

Array

Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays. Similar to the C language, the subscript of an array element is numbered starting with 0, and the format of the read element is generally:

${array_name[index]}

Actual use cases for arrays

Progress bar

I=0 str= "" arr={"|"     "/" "--" "\ \"} While [$i-le] does let index=i%4 printf "[%-100s][%d%%][%c]\r" "$str" "$i" "${arr[index]}" Sleep 0.1 Let i++ str+= ' # ' Done







Shell learns the next day

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.