Linux Shell Programming Commands-linux Basic Environment Command Learning notes

Source: Internet
Author: User
Tags case statement

1. Regular expressions

1) ^ Start * Previous character repeats more than 0 times

+ More than 1 times

? 0 or 1 times

. An arbitrary character (. *)

{m,n} m to n times

[0-9] [A-z] any number or letter

$ end Character

2) sed and awk commands

Sed ' match condition/executed action '/etc/passwd sed ' s/old/new/g '

Awk-f: ' {print '} '/etc/passwd 1 represents the first column, and 2 indicates the second column-F: The delimiter is:

2. Variables

1) environment variable system environment variable (/etc/profile) User environment variable (/home/user name/.bash_profile)

Set view

2) Position Variable $0-9

$ A script name

$1-9 Positional parameters

3) pre-defined variables

$! PID number of the last process running in the background

$? Program exit representatives (0 successes)

$* the contents of all parameters

$$ ID number of the current process

$# the number of arguments for the current shell

[email protected] All parameter contents (read one by one)

4) Custom Variables

Name=[value] PS: No spaces on either side of the equals sign, variable names are size sensitive

$ Variable Name value

3. Arithmetic operations

+ - * / %

$ ((expression))


Expr expression expr 5 + 2 with spaces between variables

4. Built-in test judgment

Test-tested expression

[Test expression] PS: There must be a space between the test expression and []

Numerical comparison

-lt less than Ex:test &a-lt &b echo $?

-GT Greater than

-eq equals

-ne Not equal to

-le less than or equal to

-ge greater than or equal to

String test

= equals

! = Not equal to

-Z String string length pseudo (0) True

The-n string length is not pseudo-true

File test

-e filename File exists then true

-r filename File exists and is readable as true

-W File name writable

-X File name executable

-S has at least one character in the filename file

-d filename file exists and is directory

-f filename File exists and is normal file

! (non) >>-a (with) >>-o (or)

5. Logical compliance between commands

&& Logic and

CMD1 && cmd2 cmd1 Execution succeeds CMD2

|| Logical OR

cmd1 | | CMD2 Cmd1 failed to execute CMD2

; no logical relationship

6. Common commands

Date-s "2016-04-12 15:20:13" to modify the system time

Date ' +y%m%d ' output system time according to date format

date-d ' 7 day ago ' +y%m%d ' output 7 days ago

7. Take a string

${variable name: Offset:length} Echo ${a%.*}

8. Writing code

#!/bin/bash declaring a running environment

How to run a. Granting permissions to run directly

B. Run bash sh by running environment

C.. Source

9. Control statements

1) For statement

A. For variable in value 1 value 2 ... Value n Variable value 1 ... Value n


Command set {1..50} ' SEQ 50 ' takes value from 1 to 50


B. for (initialize variable; end loop condition; variable operation))

2) While loop

A. While [condition] let i=i+1---i=$[i+1] I self-increment 1

B. While Read-r line


Echo $line

Done </etc/passwd reads lines from the path and assigns values to line

3) If statement

If condition


Command sequence

Elif conditions


Command sequence


Command sequence


4) Case statement

A. Case $ variable name in

Condition 1)

Command sequence


Condition 2)

Command sequence


*) above are not satisfied


10. Functions

A. Name () {

Command sequence


B. function name{

Command sequence


11. Scheduled Tasks

1) One-time scheduled tasks

At time Ctrl+d end input

At-l List Current Scheduled tasks

2) Recurring Scheduled task Service Crond start (Status,restart)

A. Boot from boot

Chkconfig--list lists whether system services are powered on from boot (3 and 5)

B. Developing the content of the planned tasks

CRONTAB-E (Enter) >> Enter VI command

Time and Command day and Night Week (no * substitution-Indicates continuous time, indicating a number of discontinuous time/interval time) cmd command */10 * * * * ntpdate time server IP synchronizes clocks every 10 minutes

C. Restart Service Crond Restart

Linux Shell Programming Commands-linux Basic Environment Command Learning notes

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: 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.