Shell notes (basic knowledge)

Source: Internet
Author: User
Tags echo command

first, to write a shell programExample 1:"program"
1 #!/bin/sh2echo"Hello Word! "

Run

"description"The first line: #i/bin/sh means using an interpreter in the Ubuntu system to interpret the shell and run it/bin/sh. second line: echo "Hello Word!" Indicates that the character "Hello word" is printedalso in the shell # is used to annotate  second, the variables in the shell(1) Variable definition: direct use = can(2) Variable assignment: direct use =(3) variable reference: variable in front of $; (Two usage: $var and ${var})Example 2:
1 #!/bin/sh2string="new string"3  Echo $String

Operation Result:

  Three, the difference between single and double quotation marksExample 3:
1 #!/bin/sh2# Single and double quotation marks demo 3 str="new string" 4 Echo ' $str ' 5 Echo   $str6echo"$str"

Operation Result:

Description: The single quotation mark indicates the direct substitution, even if there is the $ symbol, is also the direct output $str;using $ in double quotes will read the value of the variable to use. Note: Anti-quotes indicate command substitution, for example:\$ represents $\ ' expression '\ "represents"\ \ means \Reference: Http://blog.csdn.net/miyatang/article/details/8077123  Iv. calling commands under LinuxExample 4:
1 #!/bin/sh2# on Linux downgrade command 3mkdir  test_dir   # Create a directory in the current directory 4cd test_dir      # Enter Test_dir directory 5touch  A.C        # Create a file 6CD            : # Return to Upper directory 7echo 'pwd'       # Print current directory

Description: In a shell script, the commands under Linux are called directly. The return value for getting a Linux command is to use the back quotation mark (the key under ESC on the keyboard, the symbol is ').

  v. IF structureTypical expressions:if [expression]; ThenXXXElseXXXfiApplication:(1) Determine if the file exists. (-f), using example: if [-f B.C]; Then(2) determine if the directory exists. (-D), using example: if [-D "/mnt"]; Then(3) Determine if the strings are equal. (=), using an example: if ["abc" = "abc"]; Then(4) Determine whether the number is equal (-EQ), greater than (-GT), less than (-LT), greater than or equal to (-ge), less than or equal to (-le). Example of use: if [$num 1-eq $num 2]; Then(5) Determine if the string is empty. (-Z), using example: if [-Z ' abc ']; Then Note: spaces in [] are required before and after, otherwise the error will be run. (6) If the '-O ' representation in the If judgment is logical OR. Examples of use: if ["1" = "2"-o "3" = "3"]; Then(7) logic with && and logic or | | Combined with a shorthand if expressionFor example:["123" = "abc"] | | echo "Not equal" # execution Result: output not equal["123" = "123"] && echo "equal" # Execution Result: output equalDescription: In | | , the following command does not execute if the preceding statement is true, and the subsequent command executes if it is not.   VI. echo Create file(1) You can create a new file directly in the shell using the echo commandExample: echo "abc" > A.txt # Create a file, content and ABC(2) >> to append a content to the end of the fileExample: echo "CDE" > A.txt # Append a character "CDE" to the end of the file  Nine, the shell of the communication parametersExample: Shell programOperation Result:$ A is the name of the parse that executes the shell program, which is the value of the first valid parameter, and the second one is ...  Vi. for, while loops and case structuresExample 5:
1  while[$#-GT0] ; Do2          Case " $" inch3--)Shift;4-a)Shift; Append=yes;;5-N)Shift; Board_name="${1%%_config}";Shift ;;6*) break;;7         Esac8  Done

(1) above is an example of a while and case nesting.

(2) break is not useful for case, it means jumping out of a while loop. (3) The statement in case is based on;; As the end. (4) Shif is the one that represents the left shift of the input parameter. (5) $ #表示执行shell时传递的参数个数(6) $ = The first parameter of the shell   ---------------------------------------------------------------------------------- Statement: Part of the content excerpt to "teacher Zhu's Internet of Things video tutorial"http://www.zhulaoshi.org/ ----------------------------------------------------------------------------------  

Shell notes (basic knowledge)

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.