To learn the shell programming of Linux in a face

Source: Internet
Author: User

1: What is Shell???

(1) The shell is an interface that the user interacts with the kernel, and the most popular shell is now called the Bash shell
(2) The shell is also a programming language < interpretation of programming languages, that is, shell script
(3) A system can have more than one shell, you can use the Cat/etc/shells command to view the shell installed in the system, different shells may support the command syntax is not the same

2:shell How scripts are executed:

(1) The first type: the absolute or relative path of the input script
The first thing to give +x permissions: [[email protected] test]# chmod +x./hello.sh
/root/helloworld.sh
./helloworld.sh

Or, instead of giving +x permission, use the interpreter to interpret the execution
SH helloworld.sh

(2) Second type: Bash or sh + script
sh/root/helloworld.sh
SH helloworld.sh

(3) The third type: Add "before the path of the script." "
. /root/helloworld.sh
. ./helloworld.sh

Attention:
Difference: The first and second will open a new bash, different bash variables cannot be shared

Variables in the 3:shell:

The variables in the Linux shell are divided into "system variables" and "user-defined variables", which can be viewed through the SET command.
System variables: $HOME, $PWD, $SHELL, $USER, and more

Displays all variables in the current shell: set

4: Define variables

(1) variable = value (for example STR=ABC)
(2) No space on either side of the equal sign
(3) variable names are generally used in uppercase
(4) Double quotation marks and single quotation marks are different, double quotation marks only the space, single quotation marks will be all special characters of the
(5) str= "Hello World"
A=9
unset A undo variable a
readonly b=2 declare static variable b=2, You cannot unset the
Export variable name to promote a variable to a global environment variable, which can be used by other shell programs

(6) assigns the return value of a command to a variable

a= ' Ls-la ' anti-quote, run the command inside and return the result to the variable A
a=$ (Ls-la) equivalent to the inverse quotation mark
(7) Special variables in the shell

$?  indicates the status of the last command exit
$$  indicates the current process number
$ Represents the current script name
$n an input parameter representing n position (n for number, n>=1)
$#      Indicates the number of parameters, commonly used for looping
$* and [email protected] Both indicate that the argument list
(8) $* and [email protected] differ
$* and [email protected] all represent all arguments passed to the function or script, and are not enclosed by double quotes "", all $1    ... $n output all parameters
when they are enclosed by double quotation marks, "$*" will all parameters as a whole, output all parameters in the form of "$ $n" and "[email protected]" will separate the parameters to "$ "" ... Output all parameters in the form of "$n"

5: operator

Format: expr m + N or $ ((m+n)) Note that there is a space between the expr operators
For example, calculate the value of (2 +3) x4
1. Step calculation s= ' expr 2 + 3 ' expr $S \* 4
2. Complete the calculation in one step
Expr ' expr 2 + 3 ' \* 4
echo ' expr \ ' expr 2 + 3\ ' \* 4 '
Or
$ (((2+3)))

6:for Cycle

The first type:
For N in 1 2 3
Do
Echo $N
Done
Or
For N in 1 2 3; do Echo $N; Done
Or
For N in {1..3}; do Echo $N; Done

The second type:
for ((i = 0; I <= 5; i++))
Do
echo "Welcome $i times"
Done
Or
for ((i = 0; I <= 5; i++)); Do echo "Welcome $i times"; Done

7:while Cycle

First Kind
While expression
Do
Command
...
Done

The second Kind
I=1
while ((i<=3))
Do
Echo $i
Let i++
Done

8:case statements

Format
Case $ in
Start
echo "Starting"
;;
Stop
echo "Stoping"
;;
*)
echo "Usage: {start|stop}"
Esac

9:read command

READ-P (hint statement)-N (number of characters)-T (wait time)
Read-p "Please input your name:" Name

10:if judgment

syntax
if condition 

then     statements  

[elif condition     

then statements.  ...]  

[else     statements ] 

fi

Pending......

11: Judgment Statement

[Condition] (Note that there are spaces before and after condition)
#非空返回true, you can use $? authentication (0 is true,>1 to false)


#空返回false
[  ]

[Condition] && echo OK | | Echo Notok
Condition satisfies, executes the following statement

12: Common Judging conditions

= string comparison
-lt less than
-le less than or equal to
-eq equals
-GT Greater than
-ge greater than or equal to
-ne Not equal to

-R has Read permission
-W has Write permission
-X has permission to execute
-F file exists and is a regular file
-s file exists and is not empty
-D file exists and is a directory
-B file exists and is a block device
-l file exists and is a link

13:shell Custom Functions

Grammar
[Function] funname [()]
{
Action
[Return int;]
}

function start ()/function Start/start ()

Attention
1. The function must be declared before the function is called, and the shell script runs on a row-by-line basis. Not pre-compiled like any other language
2. function return value, only through $? System variables are obtained and can be displayed plus: return returns, if not added, will run the result as the last command, as the return value. return followed by value N (0-255)

14: Script Debugging

SH-VX helloworld.sh
Or add Set-x in the script

15:sed command

SED full name is: Stream editor, is a good text processing tool, itself is a pipeline command, processing, the current processing of the row is stored in a temporary buffer, and then use the SED command to process the contents of the buffer, after the completion of processing, the contents of the buffer to the screen. Then proceed to the next line. It is handled in a behavioral unit and can be used to replace, delete, add, and select data rows for specific tasks.

16:sed Options

-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed on the terminal. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
-E: Action editing of SED directly in command-line mode;
-I: Directly modifies the contents of the read file, not the output to the terminal.

17:sed function

A: New, a can be followed by a string, and these strings will appear on a new line (the next line)
D: Delete, because it is deleted, so D usually does not take any content after
I: Insert, I can be followed by a string, and these strings will appear on a new line (the current line)
P: Print, that is, print out a selected data. Normally p will run with parameter sed-n
S: Replace, can be directly replaced by the work! Usually this s action can be paired with formal notation! such as 1,20s/old/new/g

18:awk command

Awk is an excellent tool for text processing. Its name is from the first letter of its founder Alfred Aho, Peter Weinberger and Brian Kernighan's last name, and AWK offers extremely powerful features: style loading, flow control, mathematical operators, Process control statements, and even built-in variables and functions. It has almost all the beautiful features that a complete language should have. In fact, Awk does have its own language: The awk programming language, a three-bit creator has formally defined it as "style scanning and processing language." It allows you to create short programs that read input files, sort data, manipulate data, perform calculations on input, and generate reports, as well as countless other features.

awk command format

awk ' pattern1 {action1} pattern2 {action2} ... ' filename

Cond......

To learn the shell programming of Linux in a face

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.