Shell programming notes, shell programming

Source: Internet
Author: User
Tags case statement

Shell programming notes, shell programming
1. Bash variable
User-Defined variables # local variables
Environment Variable # global variable
Location Parameter Variable
Predefined Variables

X = val # value assignment
$ X # Call
The default value of the variable is quiet and cannot be calculated directly.
"$ X" $ {x} variable superposition, that is, string

Set # view all System Variables
-U: an error is reported if the variable does not exist.
Unset # Clear variable

Pstree # view the process tree

Export x # convert x into an environment variable

PATH = "$ PATH":/root

PS1 variable
\ D Display date "week Month day"
\ H displays the complete host name
\ T "HH: MM: SS"
\ A "HH: MM"
\ U current user name
\ W full name of the current directory
\ W the last directory of the current directory
\ $ Prompt

Language Variables
Echo $ LANG # current language environment
Locale-a | more # All Language Environments
/Etc/sysconfig/i18n # default language environment, which is enabled upon next boot

Location Parameter Variable
$ N is a number, $0 indicates the command itself, $1-$9 indicates the first to ninth parameters, more than 10 $ {10}
$ * Represents all the parameters in the command line, a whole
$ @ All parameters of the command line. Separate them.
$ # Number of command line parameters

Predefined Variables
$? The returned status of the Last Command executed. If it is 0, it is correct.
$ Pid of the current process
$! Pid of the last process running in the background

Read [Option] [variable name]
-P "prompt information": the output prompt information is waiting for input.
-T seconds: Specifies the waiting time.
-N characters: only the specified characters are allowed.
-S: input is not echo

2. Operators
Declare [+/-] [Option] variable name # variable type declaration
+ Cancelling variable type attributes
-Set type attributes
-A declares it as an array.
-I integer
-X Environment Variables
-R read-only # VARIABLES declared as read-only cannot be operated at all !!!
-P display type

Declare an array
Movie [0] = zp
Movie [1] = tp
Declare-a moive [2] = live
View Arrays
Echo $ {movie}
Echo $ {movie [2]}
Echo $ {movie [*]}

Export variable name <==> declare-x variable name

Declare-p # list all system variable types


Numeric operation
Expr let
Dd = $ (expr $ aa + $ bb)
$ (Expression ))
Ff = $ ($ aa + $ bb ))
$ [Expression]
Gg = $ [$ aa + $ bb]

Variable Test
# Used in script Optimization
# Visit www.imooc.com/video/6832

3. environment variable configuration file (loaded sequentially at startup)
Source configuration file #. Configuration File
/Etc/profile
/Etc/profile. d/*. sh
~ /. Bash_profile
~ /. Bashrc
/Etc/bashrc # defines PS1, that is, the logon prompt.

Umask # view the default system permissions ---- w -- w-
The Maximum File Permission is 666 # rw-
The directory has a maximum permission of 777 # rwxrwxrwx
The permission cannot be converted using numbers, but must contain letters.
Umask-defined permissions are the permissions to be discarded in the system's default permissions ~

Environment variable configuration file effective upon Cancellation
~ /. Bash_logout # logout Configuration
~ /. Bash_history # Save the history command. The current logon history command is stored in the memory.
#/Etc/profile HISTSIZE

Login file # only valid for local terminals
/Etc/issue # logon warning information
Escape characters that can be displayed
\ D current system date
\ S operating system name
\ L local terminal
\ M hardware architecture
\ N Host Name
\ O Domain Name
\ R kernel version
\ T current system time
\ U serial number of the current Login User

Remote Terminal welcome information # escape characters not supported
/Etc/issue.net # Add "Banner/etc/issue.net" to/etc/ssh/sshd_config to display (Restart sshd)

Information displayed after logon # supported by local remote connection
/Etc/motd


4. Condition judgment
File Type judgment, true
-Block B Devices
-C character device
-D directory
-E is true
-F: Common File
-L link file
-P MPs queue File
-S non-empty is true
-S socket File

Test-e/root/install. log
[-E/root/install. log]
[-D/root] & echo yes | echo no

File Permission judgment
-R read
-W write
-X execution
-U SUID
-G SGID
-K SBit

Compare two files
F1-nt f2 determine whether the f1 modification time is newer than f2
F1-ot f2 determines whether the f1 modification time is earlier than f2
F1-ef f2 determine whether Inode is consistent (determine hard link)

Two Integers
-Eq-ne-gt-lt-ge-le

String judgment
-Z string # True if it is null
-N string # non-null, true
String 1 = string 2 # equal to true
String 1! = String 2 # not equal to true

Condition Statement
If [conditional statement]; then
Program
Fi
# Or
If [condition 1]
Then
The condition is true, and the procedure 1
Elif [condition 2]
Then
The condition is not true, and the procedure 2
Else
Program
Fi

Case statement
Case $ variable name in
"Val1 ")
Part1
;;
"Val2 ")
Part2
;;
*)
Part default
;;
Esac

For Loop
For variable in val1 val2 val3...
Do
Program
Done

For (I = 1; I <100; I = I + 1 ))
Do
Program
Done

While & until Loop
While [conditional criterion]
Do
Program
Conditional increment # For example, I =$ ($ I + 1 ))
Done

The condition when until is equal to while

5. Regular Expression
Grep awk sed contains matching # Regular Expression
Ls find cp exact match # wildcard

* Any first character

Matching date
[0-9] \ {4 \}-[0-9] \ {2 \}-[0-9] \ {2 \}
Matching ip Address
[0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}\. [0-9] \ {1, 3 \}

Cut [Option] file name # field Extraction
-F Field
-D separator # default Tab

Printf'output type output format' output content
% Ns output several characters
% Ni
% M. Number of floating points of nf

Awk 'condition 1 {Action 1} condition 2 {Action 2}... 'file name # Complex
# Df-h | grep "/dev/sda2" | awk '{print $5}' | cut-d "%"-f 1
# Awk 'in in {print "..."}{...}'
FS specified delimiter
# Cat/etc/passwd | grep/bin/bash | awk 'in in {FS = ":"} {print $1 "\ t" $3 }'
# Cat student.txt | grep-v Name | awk '$4> = 70 {print $2 }'

Sed [Option] '[action]' file name
# Action
-A append
-Replace row c
-Insert before I row
-P print
-S
-D. delete a row.

Character Processing
Sort [Option] File Name
-F case-insensitive
-N is sorted in numerical order.
-R reverse sorting
-T specifies the delimiter
-K n [, m]: sort by specified field. Start with n and end with m.

Wc [Option] File Name
-L line
-W words
-M characters

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.