Shell syntax usage

Source: Internet
Author: User
The shell syntax first came into contact with shell programming in the past two days. The so-called shell programming is actually to combine various basic commands with certain syntaxes, so that shell programs can be interpreted and executed. If you have an understanding of windows dos, you can understand it in this way. In fact, shell script files and. bat batch file... the shell syntax first came into contact with shell programming in the past two days. The so-called shell programming is actually to combine various basic commands with certain syntaxes, so that shell programs can be interpreted and executed. If you have an understanding of windows dos, you can understand it in this way. In fact, shell script files are similar to. bat batch processing files, but shell in linux is much more powerful than dos in windows. There are many shell types, such as bash, sh, tcsh, and ksh. bash is used by default in linux. Next let's take a look at the first program to learn shell: enter the command vi my_01.sh, create a my_01.sh file, and open it with the vi editor. The edited content is as follows: 1 #! /Bin/bash2 echo "aaaa" 3 echo "first touch shell" the first line here refers to letting the bash shell execute this shell program. Echo is used for output. the second and third rows output two sentences respectively. You can use the following methods to execute this shell program: 1. /my_01.sh 2. my_01.sh 3 bash my_01.sh note: By default, the newly created file has no execution permission. Therefore, you must grant the execution permission to the file before execution. Authorization command: chmod u + x my_01.sh or chmod 755 my_01.sh is the second shell program: 1 #! /Bin/bash2 # my_02.sh3 read-p "please input your name:" name4 echo "$ name, welcome! "The third line of the read command here refers to waiting for user input, and-p is followed by the prompt. it also has a parameter-t that sets the waiting time. The program running result is as follows: [liuling @ bogon test] $./my_02.sh please input your name: liulingliuling, welcome! Note: The fourth line of echo uses double quotation marks. if single quotation marks are used, this sentence will directly output $ name, welcome!. Double quotation marks can be used to parse the variables, but single quotation marks cannot. Let's take a look at the syntax of the shell script: 1. use the condition to determine if else to write the if branch statement: if the condition test then meets the condition to execute what else does not meet the condition to execute the fi multi-branch statement: if condition 1 then execute command 2 elif condition 1 then execute command 2 else execute command 3fi note: if and fi must be paired, end with fi to determine this condition. If the first condition is not met, elif is used, rather than else if. The then must have an exclusive row. The following are several examples: 1 #! /Bin/bash 2 # my_03.sh 3 4 read-p "pelw.input your name:" name 5 read-p "pleaase input your password: "passwd 6 # if test $ name =" liuling "7 if [$ name =" liuling "-a $ passwd =" lz19921009 "] 8 then 9 echo" logon successful! "10 else11 echo" logon failed! "12 fi 1 #! /Bin/bash 2 # my_04.sh 3 4 read-p "please input your score:" score 5 6 if [$ score-gt 90] 7 then 8 echo "Excellent! "9 elif [$ score-gt 80-a $ score-le 90] 10 then11 echo" Good! "12 else13 echo" average! "14 fi 1 #! /Bin/bash 2 # my_05.sh 3 4 read-p "please input your score:" score 5 # if () is used, only <>>=< = 6 if ($ score> 90) 7 then 8 echo "Excellent! "9 elif [[$ score-gt 80 & $ score-le 90] 10 then11 echo" Good! "12 else13 echo" average! "14 whether the input file name of the fi test user is a directory or a file: 1 #! /Bin/bash 2 # my_06.sh 3 4 read-p "enter a directory or file name: "name 5 if [-z $ name] 6 then 7 echo" the information you entered is empty! "8 else 9 if [-f $ name] 10 then11 echo" this is a file! "12 elif [-d $ name] 13 then14 echo" this is a directory! "15 else16 echo" there is no such directory or file! "17 fi18 fi these three examples are about the use of if condition judgment. Conditions can be written in the following ways: [conditions], test conditions, [[conditions], and (conditions). The most commonly used conditions are the first. The commonly used judgment symbols are as follows: ①. the logical operator-a expr1-a expr2 logic and-o expr1-o expr2 logic or!! Expr1 logic non-②, numeric judgment-eq num1-eq num2 is equal-ne num1-ne num2 is not equal-gt num1-gt num2 is greater than-ge num1-ge num2 whether it is equal to-lt num1-lt num2 is less than-le num1-le num2 is less than or equal to ③, string judgment = str1 = str2 string is equal! = Str1! = Str2 string-n str1 string length is not equal to 0-z str2 string length is equal to 0 ④ the file determines whether the-r filename File exists and is readable -whether the-w filename File exists and can be written to the-s filename File, and whether the non-0-f filename File exists and is a common file-d filename File whether a directory exists or not. when using conditions to determine whether a directory exists, pay attention to spaces, for example, if [$ name = "liuling"-a $ passwd = "lz19921009"], there must be a space before and after, all judgment symbols and logical operators must have spaces before and after them. Otherwise, an error is reported. When [condition] is used, only the "&" symbol can be used as logic and instead of "-a". if (condition) is used, you can only use the <>>== symbol instead of the "-eq" symbol. II. case usage the advantages of case here are similar to the swith case syntax in java: case var inpattern 1) execution statement 1; pattern 2) execution statement 2; pattern 3) execute statement 3; *) execute statement 4; esac Note: *) match any other string in each case; end the following example 1. select 1 for the simulated menu #! /Bin/bash 2 # my_07.sh 3 4 echo "1-normal display" 5 echo "2-detailed display" 6 echo "3-show hidden files" 7 echo "4-release" 8 9 read-p "select the Operation number: "num10 case $ num in11 1) 12 ls; 13 2) 14 ls-l; 15 3) 16 ls-al; 17 4) 18 exit; 19 *) 20 echo "enter the correct operation (1--4)" 21 exit ;; 22 esac 3. Use of loop statements 1. for loop for variable name in value list do command sequence below is a small example for loop use: 1 #! /Bin/bash 2 # my_08.sh 3 4 for a in 'seq 1 3 10' 5 do 6 echo $ a 7 done 8 echo "-----------------" 9 for (I = 1; I <= 10; I ++) 10 do11 echo $ i12 done here the fourth line uses 'seq 1 3 10', ''is a back quotation mark, that is, a key under Esc on the keyboard. you can write the execution command in ''to return the execution result. Seq commands refer to sequences. seq start size max, start is the starting point, size is the step size, and max is the limited number. for example, if seq 1 3 10, 1 4 7 10 is returned. The 9th line of code is also a bit special, a bit like the for loop in java, but it is a double parentheses. it is also possible in shell. The following is a small program for counting the number of files: 1 #! /Bin/bash 2 # my_09.sh 3 4 I = 0 5 for name1 in 'ls/etc '6 do 7 # I = 'expr $ I + 1' 8 # let I ++ 9 # I = $ [$ I + 1] 10 # I = $ ($ I + 1 )) 11 (I ++) 12 13 done14 echo "total number of files: $ I" note: Lines 7, 8, 9, 10, and 11 are different self-incrementing numbers, yes. 2. while loop while condition do command sequence done below is a shell script for entering customer data: 1 #! /Bin/bash 2 # my_10.sh 3 4 while true 5 do 6 echo "register customer profile (c continue, q quit):" 7 read choice 8 9 case $ choice in10 c) 11 echo "enter customer name:" 12 read name113 echo "enter customer age:" 14 read age115 echo "name:" $ {name1} "-age: "$ {age1 }>> mermer.txt 16; 17 q) 18 exit19; 20 esac21 done Note:> This refers to the redirection of standard output to other output streams. The 15th line of this line indicates that the standard output is redirected to the customer.txt file. The content of all echooutputs will be output to the customer.txt file. >>And> difference> mer.txt is saved to the customer.txt file. if the file does not exist, it is automatically created.> Customer.txt will be written again to overwrite the original data. 4. call the function {} or function name () {} function of the function application: the following is a small example of the function name function parameter 1 parameter 2: 1 #! /Bin/bash2 # fun. sh3 function add () {4 echo $($1 + $2) 5} 6 add 20 307 add 20 90
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.