Common commands and statements for Shell programming (SCRIPT) and Common commands for shell
Some common Shell programming (SCRIPT) commands and statements can meet general requirements.
Command Parameters received:
$ #
Command itself: $0 first parameter: $1 second parameter: $2 ......
Exit command:Exit
Echo command:
Echo
- Do not wrap after output:
Echo-n "select (y/n )? "
- Use escape characters to output double quotation marks:
Echo "Welcome to \" official server \ "Deployment Tool. "
- The output contains variables:
Echo "project to be deployed: $ project_name"
- Output A string with an escape character:
Echo-e "first \ tsecond"
Append to the end of the file: echo-e $ log_info> deploy. log overwrite the file content: echo-e $ log_info> deploy. log
Printf command:
(Echo can be used instead to format the output, which is the same as the printf function in C)
- Output The number of two digits after the decimal point and wrap the line:
Printf "The number is %. 2f. \ n" 100
- Output a string of the specified width:
Left alignment: printf "%-20 s %-15 s % 10.2f \ n" "Stephen" "Liu" 35 right alignment: printf "| % 10s | \ n" hello
Variable:
Profile = "production"
Echo $ profile
Log_info = "$ log_info, $ target_ip"
Array:
Servers = ("192.168.0.31" "192.168.0.39") projects = ("public" "industry" "logistics" "misc ")
$ {Projects [*]}
- Obtain the number of array elements:
$ {# Servers [@]}
If statement (condition judgment also applies to the while statement and for statement ):
- Determine whether a variable (project) is not empty:
If [$ project]; then echo "Variable \" project \ "is null." fi
If [$ #-ne 3]; then echo "command line parameters are not 3" fi
If ["$ is_ip_correct" = false]; then echo "is invalid ip address, use one of the following ip addresses:" echo $ {servers [*]} exit fi
If ["$ confirm" = "y"] & ["$ confirm "! = "N"]; do # do something... fi
- Regular Expression judgment
If [[$1 = ~ ^ Public | industry $] & [[$3 = ~ ^ [Yn] $]; then # do something... fi
If [[! $ Deploy_more = ~ [Yn]; then # do something... fi
- Determine whether a file exists:
If [! -F target/$ project. war]; then # do something...
Fi
- Determine whether a directory exists:
If [-d $2/webapps/$1]; then # do something... fi
- Determine the suffix of a file name (string)
Backup_file = "/backup/java_data/$1-$ today. war" backup_file = "/backup/java_data/1_1-1_today.gz"
If ["$ {backup_file ##*.} "=" war "]; then cp $ backup_file $ 1.war elif [" $ {backup_file ##*.} "=" gz "]; then tar zxvf $ backup_file else echo" backup file format incorrect "exit fi
Read Statement (read user input string ):
- Simplest usage: read user input to the variable yes_or_no
Read yes_or_no
- Prompt user to enter y or n
Read-e-p "backup? (y/n )? "-I" y "needbackup (parameter description:-e: I do not know what to use, but if it is removed,-I will become invalid;-p: the subsequent prompt statement; -I: followed by the default input. The last parameter is to save the user input variables .)
Select Statement (prompting you to select one from the list ):
- Modify the default prompt (the default value is "#? "):
PS3 = "select a project :"
- You are prompted to select a value from the array:
Select project in $ {projects [*]}; do if [$ project]; then break fi done
Or add the Exit condition: select target_ip in $ {servers [*]} "Exit (Exit)"; do if ["$ target_ip" = "Exit (Exit)"]; then echo "Thank you! Good-Bye! "Break fi
If [$ target_ip]; then # do something fi done
Case statement:Case $ project in public) project_name = ""; logistics) project_name = ""; misc) project_name = ""; esac
While statement:
- Use the regular expression to determine whether to use:
While [[! $ Needbackup = ~ ^ [Yn] $]; do read-e-p "backup? (y/n )? "-I" y "needbackup done
- Use "..." as the progress bar
Echo-n "waiting for $ port to open ...... "While [! $ Pid_new]; do # sleep 1 pid_new = 'netstat-nlp | grep $ port | awk' {print $7} '| awk-F "/"' {print $1} ''echo- n "... "Done
For statement:
For ip_t in $ {servers [*]}; do if ["$2" = "$ ip_t"]; then is_ip_correct = true break fidone
Call other programs:
- Use ''and get the output result:
PS3 = "select a branch:" select branch in 'svn list svn: // <svn_host>/java/code/branches '; do if [$ branch]; then svn_dir = "svn: // <svn_host>/java/code/branches/$ branch" project_dir = $ branch break fi done
- Use $ () and obtain the output result:
Today = $ (date + % Y-% m-% d/% H: % M: % S) svn_version = $ (svn info $ svn_dir | grep "Last Changed Rev: "| awk '{print $4 }')
- Call other programs silently (no result is output, that is, the result is output to a null device)
Rm $2/logs/m * $2/logs/l * $2/logs/h * $2/logs/catalina.2015 * &>/dev/null &
Awk (used to slice strings, usually used with grep ):
- Obtain the fourth string separated by spaces:
Svn_version = $ (svn info $ svn_dir | grep "Last Changed Rev:" | awk '{print $4 }')
- Returns the first string separated by a special separator ("/").
Awk-F "/" '{print $1 }'
- Get all processes whose port number is $ port and kill
For pid in 'netstat-nlp | grep $ port | awk' {print $7} '| awk-F "/"' {print $1} ''Do echo "= ============================================================ ========= "echo" cannot close the process normally, port: $ port, kill directly, process Number: $ pid "echo" ============================ Warning ======== ==================== "kill $ piddone
(For Original Articles, please note