Basic knowledge of linux shell scripts

Source: Internet
Author: User

Shell script:

1 ,#! Shell for Script Execution
2. # comment
3. Command or syntax structure
Echo "input content"
Empty line output by echo

Run:

1. shell script
Sh example
2. Grant shell script execution permission
Chmod u + x example

Case: example sysinfo. sh

3abc x cannot start with a number
Abc3
ABC3

Environment Variable-UPPERCASE

Command Execution result value variable use command replacement character''
PATH = $ PATH:/root/shell. example

Ls-l/etc/inittab/etc/fstab/etc/services
$0 $1 $2 $3

$0-$9
$ */Etc/inittab/etc/fstab/etc/services
$#3
$ PID
$? Command return value 0 non-0

Case: special. var

Read case: read

There must be spaces before and after the expr operator, and the multiplication character * must be an escape character.
Case: expr

Test
1. Check whether the string is equal and whether it is null.
2. Integer test equal, greater than, less
3. File test file type, File Permission, file existence, and file size

Single Cycle: if/then
If test conditions
Then
Perform operations
Fi

APACHE = '/usr/bin/pgrep httpd'

If ["$ APACHE" = ""]
Then
/Etc/rc. d/init. d/httpd start
Fi

The sh-x script displays the execution process ++ or ++ notes during execution.
*/2 12-14*3-6, 9-12 1-5/root/shell-example/apache. test

Double Cycle: if/else

If test statement
Then
True: Operation
Else
False: Operation
Fi

Case: test. apache autobak. sh

Tar-cf new file name directory

Multiple cycles: if/elif
Case: if_else determines the file type
Conditional connection-o logic or-a logic and

Exit statement
If ["$ #"! = 2]
Then
Echo "MUST two parm! "
Fi
Echo "this is a test"
Echo $1 $2

Sh exit first
Condition true exit 0 condition false exit 1 Non-0
Add exit statement sh exit first

For Loop
Case: for automatically kicks users out of the system killuser. sh

Awk-F domain separator 'COMMAND 'default Delimiter is space
Ps-le | grep httpd | awk '{print $4 }'
Awk-F: '$3 = 0 {print $1}'/etc/passwd
Count system users
Awk-F: '$3 >= 500 {print $1}'/etc/passwd | grep-v nfsnobody | wc-l
Awk-F: 'length ($2) = 0 {print $1} '/etc/shadow

Case study: case/etc/rc. d/init. d/httpd

The while condition is true and the loop is always going. Case: while useradd. sh
The until condition is a false continuous loop.

Echo password | passwd -- stdin User Name

While count Loop

There will be unrecognized controllers to process when writing scripts in Windows.

Dos2unix script

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.