Shell scripts--(symbols and syntax)

Source: Internet
Author: User
Tags switch case

Shell Script

Shell script: Always use, but no system to learn, just in the Linux command line mode to knock some simple common instructions, now the system to learn some.


First, create a shell script, generally end with. SH (linux suffix is just a display, no specific meaning) after the file is created, you need to chmod 0755 name to add permissions to it, and then you can use./Run the

Entering the file requires a shell script interpreter at the top, and all commands in the file will be interpreted by bash. Here's how the shell works.


Second, the Shell's working mode;

That's what I understand. When we write a program like this in a script,

#!/bin/bashls

At this point the shell explains that the current shell (bash) will fork a child bash to get all the commands in the script, and when it goes to the LS line, SH will fork a new bash to execv the LS executable. The result of the wait is then output to the current terminal.

The command of the shell is divided into two types, an internal command, a shell executable application, and the Frok child bash executes the command within its space when executing internal commands.



Third, some common symbols in shell scripts:

1> Simple Symbols

  1 #!/bin/bash  2  #shell中用 # as a comment   3 count=1              #变量的定义不用声明类型, Shell interpreter will help you to do   4 val=0   5 echo  $count         #$ symbol to take the value of the variable   6    7 echo ${count}t      #建议加上 {} Because sometimes you're going to output    8 like this. The function of    9 val=$ (date)      #$ () is command substitution, which means to pass the command output of date to Val  10 echo  $val          11  12 val= ' The function of the date '        # ' anti-quote is the same as the $ () command substitution, the difference is "cannot be used"  13 echo  $val          14  15 echo  ' Val '     The function of     # ' single quotation mark is to keep the string itself, but it can no longer have a string  16  17 echo  "va ' l '"       &nbspThe difference between;#  "" and the single quote is that it can have a string '                                                                                                                                                                                                                                                                                                  6,0-1         All

2> meaning symbol


[] The meaning of the brackets is a judgment statement note [is a command so add a space later, you can also use the test command. For example

 19 val=10 20  21 test  $val  -eq 12    #test命令的书写格式, Determine if the value of Val equals 12 22 echo $?                23 [  $val  -eq 10 ]   #[ ] Command writing format  24  echo $? 25 [  $val  -eq 11 ] 26 echo $? 27   28                                                                                                                                                                                                                                                                       20,0-1         Bottom

echo $? means to get the exit code for the last execution statement, and its return value succeeds to 0 with a failure of 1.


Comparison commands under the 3>shell;


-eq; equal to

-ne; not equal to

-lt; less than

-le; less than or equal to

-GT; greater than

-ge; greater than or equal to


Another common comparison command

-D to determine if the directory is true

-F to determine if the file is True


Logic with-a [$val-gt 10-a $val-lt 100] greater than 10 less than 100

Logical OR-o [$val-eq 10-a $val-eq 100] is equal to 10 or a number equal to 100

Take the reverse! [$val-eq 10-a $val-eq 100] Not equal to 10 or not equal to 100 of the number



Iv. Special variables

$: Equivalent to the C language main function argv[0]

$ ...

These are called positional parameters (positional Parameter), equivalent to the C language main function of argv[1], argv[2] ...

$#

Equivalent to the C language main function of the argc-1, note that the following # does not indicate a comment

[Email protected]

Represents the parameter list "$" "$" ..., for example, can be used in the for loop in the back.

$?

It's been mentioned above.

$$

Displays the process number of the current shell

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/2D/wKioL1dspoXiXJEDAAB5cUwN_2s115.png "title=" Untitled. png "alt=" Wkiol1dspoxixjedaab5cuwn_2s115.png "/>


It can be seen that different from C is that its arcg->$ #只输出不包括./This column.


The shift command shifts the position to the left, such as the above program with Shift 2, and the output is one, and Shift 1 is the default non-moving value




V. The difference between common symbols

1> [[The difference between double brackets and [single brackets]


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/2F/wKioL1dsqnOj_tRLAABJ0bqol-I469.png "title=" Untitled. png "Width=" "height=" 310 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:310px; "alt=" Wkiol1dsqnoj_trlaabj0bqol-i469.png "/>

[you can use = =! && | | Regular expressions to judge, but the above results are obviously problematic, [[in the && | | It's hard to judge right when you judge


2> "with $ ()

The function of #$ () is to replace the command, the line of code means to pass the command output of date to val# ' anti-quote function and $ () is the same as command substitution, the difference is "can not be used"


3> (()) and let


(()) and let can perform operations, basically interchangeable


((val++))

Let val++



Six, Shell syntax


While loop;

  1 #!/bin/bash  2   3 val=0  4 while [  $val  -lt 10 ]  5 do  6     echo  $val   7     let val++  8 done  9   #注意do  done's Writing                                                                                                                          4,1           All 

if judgment:

  1 #!/bin/bash  2   3 val=10  4   5  if [  $val  -eq 10 ];  6 then  7      echo  $val   8 fi  9 # if [] ; fi  formats                                                                                                                                                                                                                                                                                                5,1            All 

For statement:

two ways to do it  

    2  #写法一   3 val=0  4   5 for val  in {0..100}  6 do  7     echo  $val    8 done  9  #写法二   10 val=0  11 for  (( val;  val<=100 ;val++))   12 do  13     echo $ val  14 done                                                                                                                                                                                                                                                                                                                  12,2           All

Switch case:


1 #!/bin/bash

2

3 Read I

4

5 Case $i in

6 a)

7 Echo ' a '

8;;

9 b)

Ten echo ' B '

11;;

Esac




Summary: Shell scripts are Class C-style, and the shell is characterized by its many tools, which are explained in a later blog post.




This article is from the "Traces" blog, be sure to keep this source http://wpfbcr.blog.51cto.com/10696766/1792517

Shell scripts--(symbols and syntax)

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.