Learn about shell scripts in an article

Source: Internet
Author: User
Tags arithmetic naming convention readable

A shell script is a function that uses the Shell's command interpretation to parse a plain-text file and then perform these functions, or it can be said that a shell script is a collection of commands.
The shell can be used directly above the win/unix/linux, and can invoke a large number of internal system functions to explain the execution of the program, if skilled in the shell script, can let us operate the computer easier, also save a lot of time.

Shell application Scenario What the shell can do
    • Simplify some of the complex commands (usually we may need a lot of steps to submit a GitHub code, but it can be simplified into one step with the shell)
    • Can write some scripts automatically implement a project automatically replace the latest SDK (library)
    • Automatic packaging, compiling, publishing and other functions
    • Clean Disk empty Folder
    • Anyway, all the regular live scripts can try
What the shell can't do
    • When a precise operation is required
    • When you need a high language efficiency.
    • When you need some network operation,
    • In short, the shell is able to quickly develop a script to simplify the development process, not to replace the high-level language
How the Shell works

The shell can be called a scripting language, because it does not need to be compiled, but it is interpreted by the interpreter and then compiled and executed, compared with the traditional language more than the interpretation of the process, so the efficiency is slightly less than the traditional direct compilation of the language.

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

Just open the text Editing tool, edit it as above, and save it to test.sh

Run the script:
cd 到该目录下2. chmod +x ./test.sh  #给脚本权限3. ./test.sh #执行脚本

1

So we write the first simplest script, and we can try to write some complicated scripts.

Variables in the shell
myText="hello world"muNum=100

It is important to note that there can be no spaces before and after "=" and that the naming convention is the same as in other languages.

accessing variables
myText="hello world"muNum=100echo $myTextecho muNum

When you want to access the variable, you need to use $, otherwise the output will be plain text content, as shown in.


Arithmetic in 2Shell
operator meaning
+ Addition operation
- Subtraction operations
* Multiplication operations
/ Division operation
Example Program
#!/bin/bashecho  "Hello world!" a=3b=5val= ' expr  $a +  $b ' echo Span class= "hljs-string" > "total value:  $val" val= "expr  $a- Span class= "hljs-variable" > $b ' echo  "total Value:  $val "val=" expr  $a \*  $b ' echo  $val "         

It is important to note that the definition of the variable "=" before and after the space can not be, but the arithmetic when the operation symbol must have a space before and after the multiplication of the time need to be escaped.


3.png other operators =, = =,! =,! ,-O,-a
operator meaning
% Seeking redundancy
== Equal
= Assign value
!= Not equal
! And
-O Or
-A Non -
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
operator meaning
-eq Two numbers equal returns True
-ne Two number not equal returns True
-gt The left number is greater than the right number returns true
-it The left number is less than the right number returns true
-ge The left number is greater than or equal to the right number returns true
-le Left number less than equals right number returns True
Example Program
#!/bin/sha=10b=20if [$a-eq$B]ThenEcho"True"ElseEcho"False"Fiif [$a-ne$B]ThenEcho"True"ElseEcho"False"Fiif [$a-gt$B]ThenEcho"True"ElseEcho"False"Fiif [$a-lt$B]ThenEcho"true"else echo "false"fiIf [ $a-ge $b]then echo "true"else  echo " false"fiIf [ $a-le $b]then echo "true"else echo "false" C24>fi                 

5 String operators
operator meaning
= Two strings equal returns True
!= Two strings not equal return True
-Z String length of 0 returns True
-N String length does not return true for 0
operator meaning
-D File Detects if the file is a directory, and returns True if it is
-R File Detects if the file is readable and returns true if it is
-W File Detects if the file is writable and returns true if it is
-X File Detects if the file is executable and returns true if it is
-S file Detects whether the file is empty (the file size is greater than 0, not NULL returns True
-E File Detects whether the file (including the directory) exists and, if so, returns True
String
#!/bin/shmtext="hello"  #定义字符串mtext2="world"mtext3=$mtext" "$mtext2 #字符串的拼接echo $mtext3 #输出字符串echo ${#mtext3} #输出字符串长度echo ${mtext3:1:4} #截取字符串

6 arrays
#!/bin/sharray=(1 2 3 4 5)  #定义数组array2=(aa bb cc dd ee)  #定义数组value=${array[3]}  #找到某一个下标的数,然后赋值echo $value #打印value2=${array2[3]} #找到某一个下标的数,然后赋值echo $value2 #打印length=${#array[*]} #获取数组长度echo $length

7 Output program Echo
#!/bin/shecho "hello world"  echo hello world  text="hello world"echo $textecho "hello \nworld" #输出并且换行echo "hello world" > a.txt #重定向到文件echo `date` #输出当前系统时间

8printf

With C language, not too much introduction

Judgment statement
    • If
    • If-else
    • If-elseif
    • Case
#!/bin/sha=10b=20if [$a = =$B]ThenEcho"True"Fiif [$a = =$B]ThenEcho"True"ElseEcho"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]< Span class= "Hljs-keyword" >then echo  "A is less than B" else echo  "None of the condition met" Span class= "Hljs-keyword" >fi            

9test command
test $[num1] -eq $[num2]  #判断两个变量是否相等test num1=num2  #判断两个数字是否相等
Parameters meaning
-E File File exists then returns True
-R File File exists and is readable returns true
-W File File exists and can be written back to true
-X File File exists and is executable to return true
-S file File exists and the content is not empty returns true
-D File The file directory exists and returns true
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  $COUNTER doneecho  Please enter ... ' echo  ' Ctrl + d ' to stop the program ' while Span class= "hljs-built_in" >read filmdo echo " yeah! Great film The  $FILM "done      

The above is the two usages of the while loop, the first is more conventional, the execution loop, and then each time the number of controls added 1, you can let the while loop has the exit condition.
The second is the user from the keyboard data, and then the user input text output.

Jump out of the loop
break  #跳出所有循环break n  #跳出第n层f循环continue #跳出当前循环
Function
#!/bin/shsysout(){    echo "hello world"}sysout

Define a function that does not return a value, and then call the function

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

Defines a function that has a return value, invokes the function, and outputs the result


11
#!/bin/shtest(){    echo $1 #接收第一个参数 echo $2 #接收第二个参数 echo $3 #接收第三个参数 echo $# #接收到参数的个数 echo $* #接收到的所有参数}test aa bb cc

Defines a function that requires passing parameters


12 redirects
$result > file  #将结果写入文件,结果不会在控制台展示,而是在文件中,覆盖写$result >> file #将结果写入文件,结果不会在控制台展示,而是在文件中,追加写input < file #获取输入流
Write a script that automatically enters the command to automatically submit the GitHub repository script
#!/bin/bashecho "-------Begin-------"git add .git commit -m $1echo $1git push origin masterecho "--------End--------"

13

The above is my knowledge of shell summary, welcome snacks, comments, together to discuss ~ ~

Learn about shell scripts in an article

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.