Shell Programming Technology-fundamentals

Source: Internet
Author: User

I. Script structure

The script for the shell under Linux is to write a series of command sequences in a text file, and this text file can be executed. Development efficiency increases relative to the command line. So his architecture has 2 parts that make up #! and command sequences. where #! Indicates which parser is used by this script to parse. For example:

Two. Basic knowledge

2.1 Variables

The shell allows the user to set and use their own variables, which can be numbers or strings, and the user does not need to specify their type or be defined before use.

For example:

1 #!/bin/bash23 #2.24 a="Hello word"  5 b=56"Ais"  $a7"  Bis" $b

2.2 Parameters

As with C programs, command-line arguments can also be used in shell

$#: The number of command-line arguments passed in the script
$*: All command line parameter values, with spaces between individual parameter values
$: Command itself (shell file name)
$: Command itself (shell file name)
$: First command-line argument
$: Second command-line argument

For example:

#!/bin/bash#2.3Echo"Number of var:"$ #echo"Value of VARs"$*Echo"Name of script"$0Echo"Value of Var1"$1Echo"Value of Var2"$2

Terminal:./bash.sh a 3, the result can be verified

2.3 Mathematical calculations

The shell provides a specialized command expr to calculate mathematical expressions, such as expr 5 +1, but you must also use the inverse when you want to assign the results of the calculation to other variables. var= ' Expr 20/10 '. For example:

#!/bin/bash#2.4var1=tenvar2=var3= ' expr $var 2/ $var 1 ' var4= ' expr $var 2 + $var 1 ' echo $var 3echo $var 4

2.5 Flow Control Statements

2.5.1

The most basic Process Control statement in a shell script is if-then, using the format:

if [condition] (special reminder: the left and right sides of square brackets must have spaces)

Then
Commands
Else
Commands
Fi

For example:

#!/bin/bash#2.5. 1 var=Tenif [$var5  ]then     "Thevalue is Greater than 5"fi

2.5.2

Shell provides a for command to create a C language program similar to the for

Statement-like loops. Use format:

for Var in list

Do

Commands
Done

For example:

#!/bin/bash#2.5. 2 list="Sun Mon Tue Wed Thur Fri Sat" for in $listdo< /c10>     Echo $daydone

2.5.3

The shell provides a while command to create a language similar to that in C programming
The same loop as the while statement. Use format:
While condition
Do

Commands
Done

For example:

#!/bin/bash while [$var0  ]do     echo $var     var= ' expr $var1' done

Shell Programming Technology-fundamentals

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.