An article that understands Shell scripts,

Source: Internet
Author: User

An article that understands Shell scripts,

Shell scripts use the Shell command interpretation function to parse a plain text file and then execute these functions. It can also be said that Shell scripts are a collection of commands.
Shell can be directly used on win/Unix/Linux, and can call a large number of internal functions of the system to explain the execution program. If you are familiar with Shell scripts, it makes it easier for us to operate computers, it also saves a lot of time.

What can Shell do in Shell application scenarios?
  • Simplify some complex commands (it may take many steps to submit github code at ordinary times, but Shell can be simplified as one step)
  • You can write some scripts to automatically replace the latest sdk (Library) in a project)
  • Automatic packaging, compilation, and release
  • Clear empty disk folders
  • In short, you can try all the regular active scripts.
Shell cannot do anything
  • When precision operations are required
  • High language efficiency
  • When some network operations are required
  • In short, Shell can quickly develop a script to simplify the development process, and cannot be used to replace advanced languages.
How Shell works

Shell can be called a scripting language because it does not need to be compiled, but is interpreted by the interpreter before being compiled and executed, compared with traditional languages, the process of interpretation is much more efficient than that of traditional directly compiled languages.

The simplest script:
#!/bin/bashecho "Hello World"

You only need to open the text editing tool, edit it as shown above, and save it as test. sh.

Run the script:
1. cd to the directory 2. chmod + x./test. sh # grant the script permission 3 ../test. sh # execute the script

1

In this way, we will write the first simplest script. Below we can try to write some complicated scripts.

Variables in Shell
myText="hello world"muNum=100

Note that there are no spaces before and after "=", and the naming rules are the same as those in other languages.

Access variable
myText="hello world"muNum=100echo $myTextecho muNum

To access a variable, use $. Otherwise, the output is plain text, as shown in.


4 arithmetic example programs in 2 shell
#!/bin/bashecho "Hello World !"a=3b=5val=`expr $a + $b`echo "Total value : $val"val=`expr $a - $b`echo "Total value : $val"val=`expr $a \* $b`echo "Total value : $val"val=`expr $a / $b`echo "Total value : $val"

Note that there is no space before and after "=" when defining a variable, but there must be spaces before and after the operator number during the four arithmetic operations, escape is required for multiplication.


3.png other operators =, =, and ,! = ,! ,-O,-

Example Program
a=3b=5val=`expr $a / $b`echo "Total value : $val"val=`expr $a % $b`echo "Total value : $val"if [ $a == $b ]then   echo "a is equal to b"fiif [ $a != $b ]then   echo "a is not equal to b"fi

4 Relational operators #! /Bin/sh example Program
a=10b=20if [ $a -eq $b ]then   echo "true"else   echo "false"fiif [ $a -ne $b ]then   echo "true"else   echo "false"fiif [ $a -gt $b ]then   echo "true"else   echo "false"fiif [ $a -lt $b ]then   echo "true"else   echo "false"fiif [ $a -ge $b ]then   echo "true"else   echo "false"fiif [ $a -le $b ]then   echo "true"else   echo "false"fi

5 string Operators

String
#! /Bin/shmtext = "hello" # define the string mtext2 = "world" mtext3 = $ mtext "" $ mtext2 # String concatenation echo $ mtext3 # output string echo $ {# mtext3} # output String Length echo $ {mtext3: 1: 4} # truncate a string

6 Arrays
#! /Bin/sharray = (1 2 3 4 5) # define array array2 = (aa bb cc dd ee) # define an array value =$ {array [3]} # Find the number of a lower mark, then assign echo $ value # print value2 =$ {array2 [3]} # Find the number of a lower mark, then assign echo $ value2 # print length =$ {# array [*]} # obtain the array length echo $ length

7. Output program echo
#! /Bin/shecho "hello world" echo hello world text = "hello world" echo $ textecho-e "hello \ nworld" # output and wrap echo "hello world"> a.txt # redirection echo 'date' to the file # output the current system time

8 printf

Similar to the C language.

Judgment statement
  • If
  • If-else
  • If-elseIf
  • Case
#!/bin/sha=10b=20if [ $a == $b ]then   echo "true"fiif [ $a == $b ]then   echo "true"else   echo "false"fiif [ $a == $b ]then   echo "a is equal to b"elif [ $a -gt $b ]then   echo "a is greater than b"elif [ $a -lt $b ]then   echo "a is less than b"else   echo "None of the condition met"fi
9test command
Test $ [num1]-eq $ [num2] # determine whether two variables are equal test num1 = num2 # determine whether two numbers are equal

 

For Loop
#!/bin/shfor i in {1..5}do    echo $idonefor i in 5 6 7 8 9do    echo $idonefor FILE in $HOME/.bash*do   echo $FILEdone

Effect 10while Loop
#! /Bin/shCOUNTER = 0 while [$ COUNTER-lt 5] do COUNTER = 'expr $ COUNTER + 1' echo $ COUNTERdoneecho 'Please input... 'Echo 'ctrl + d to stop the program 'while read FILMdo echo "Yeah! Great film the $ FILM "done

The above are the two usage of the while loop. The first is more conventional. Execute the loop and add the number of controls to 1 each time, so that the while loop can have exit conditions.

The second is that the user outputs the input text from the keyboard.

Skip Loop
Break # Jump out of all cycles break n # Jump out of layer n f loop continue # Jump out of the current loop

 

Function
#!/bin/shsysout(){    echo "hello world"}sysout

Define a function without return values, and then call the function.

#!/bin/shtest(){    aNum=3    anotherNum=5    return $(($aNum+$anotherNum))}testresult=$?echo $result

Define a function with a returned value, call this function, and output the result


11
#! /Bin/shtest () {echo $1 # receive the first parameter echo $2 # receive the second parameter echo $3 # receive the third parameter echo $ # receive the number of parameters echo $ * # receive all parameters} test aa bb cc

Defines a function for passing Parameters


12 redirection
$ Echo result> file # Write the result to a file. The result is not displayed on the console, but overwrite $ echo result> file # Write the result to the file, the result is not displayed on the console, but in the file, append the echo input <file # To get the input stream.

 

Write a script that automatically enters the command to automatically submit the script of the github repository.
#!/bin/bashecho "-------Begin-------"git add .git commit -m $1echo $1git push origin masterecho "--------End--------"

 


13

The above is my summary of shell knowledge. You are welcome to have a dim sum, comments, and discussions ~~

Script program

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.