Brief introduction and application of shell script program

Source: Internet
Author: User

Notes for shell script programs:
<1> variables can be directly used without prior declaration;
<2> the variable does not have any data type. The default value is a string;
<3> no space is allowed on any side of the equal sign of the value assignment operation. Pay attention to this if you have the habit of adding spaces on both sides of the operator;
<4> Notes starting.

Example 1: A simple program (with three variables: Home path save) that reads the variable value of a specified file ):
Filename = pathfile
If [! -F $ filename]; then
#-F (check whether a file exists and is a common file)
Echo "file: $ filename does not exit! "
Exit 1
Fi
Cat $ filename | grep-V "#" | while read VaR
#-V (the row that does not contain the specified string)
# Read VAR (assign the read content to the variable VAR)
Do
Name = 'echo $ var | awk' begin {FS = "="} {print $1 }''
# FS (specifying separators)
Path_is = 'echo $ var | awk' begin {FS = "="} {print $2 }''
If [-N $ name]; then
#-N (non-null judgment)
If ["$ name" = "home"]; then
# If judgment can be in the IF test "$ name" = China or if ["$ name" = China] format, note that test treats all operands and operators as separate parameters, that is, they must be separated by at least one blank character (the equal signs must be separated by spaces ); if the second format is used, spaces are required before [and ].
Home = $ path_is
Export Home
# The Export command allows shell to export the given variable flag. Their values must be passed to the sub-shell, which is equivalent to setting it as a global variable.
Fi
If ["$ name" = "path"]; then
Path = $ path_is
Export path
Fi
If ["$ name" = "save"]; then
Save = $ path_is
Export save
Fi
Else
Echo "name is null! "
Exit 1
Fi
Done;

Example 2: The following is a simple program used to kill a specified process (the process name is passed by the variable path in the example program ):
Kill_id (){
PS-Ef | grep $ {path} | while read line
Do
If ["'echo $ Line | awk-C: '{print $3}'-'" = "1"]; then
App_epid = 'echo $ Line | awk-C: '{print $2 }''
Kill-9 $ app_epid &
Sleep 1
Fi
Done;
}

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.