Shell script command line parameter passing

Source: Internet
Author: User

1.
#-------------------------------------------------------------------------
9 # Note that there must be no spaces before and after the assignment.
10 # what will happen if there is a space?
11
12 # If "variable = value ",
13 # ^
14 # + the script will try to run a "variable" command with a "= value" parameter.
15
16 # If "variable = value ",
17 # ^
18 # + script tries to run "value" command
18 # + the script will try to run a "value" command
19 # + the environmental variable "variable" set "".
19 # + "variable", an environment variable assigned "" value ".
20 #-------------------------------------------------------------------------

2.
############################### Start script ###### #################################
1 #! /Bin/bash
2 # "naked" variable
3
4 echo
5
6 # When is the variable "naked", such as when the front side is less than $.
7 # when it is assigned a value, rather than being referenced.
8
9 # assignment
10 A = 879
11 echo "the value of/" A/"is $ ."
12
13 # Use let to assign values
14 let a = 16 + 5
15 echo "the value of/" A/"is now $ ."
16
17 echo
18
19 # In the For Loop
20 echo-n "values of/" A/"in the loop are :"
21 For a in 7 8 9 11
22 do
23 echo-n "$"
24 done
25
26 echo
27 echo
28
29 # In the READ command status
30 echo-n "Enter/" /""
31 read
32 echo "the value of/" A/"is now $ ."
33
34 echo
35
36 exit 0
################################ End Script ###### ###################################

3. location parameters
Is the parameter passed in from the command line, $0, $1, $2, $3...
$0 is the name of the script file, $1 is the first parameter, $2 is 2nd..., see [1] (with $0 instructions), $9
Parentheses will be required in the future, such as $ {10}, $ {11}, ${12 }...

The shift Command Re-allocates the location parameter, which is actually to move a location to the left.
$1 <--- $2, $2 <--- $3, $3 <--- $4, and so on.
The old $1 will disappear, but $0 (Script Name) will not change. If you use a large number of location parameters
The shift command allows you to access more than 10 parameters, although {} notation also allows this.

Example:
#! /Bin/sh
# We have less than 3 arguments. Print the help text:
If [$ #-lt 3]; then
Cat <
Ren -- renames a number of files using sed Regular Expressions
Usage: Ren 'regexp' 'replace 'files...
Example: rename all *. HTM files in *. html:
Ren 'htm $ ''html' *. htm
Help
Exit 0
Fi
Old = "$1"
New = "$2"
# The shift command removes one argument from the list
# Command line arguments.
Shift
Shift
# $ * Contains now all the files:
For file in $ *; do
If [-F "$ file"]; then
Newfile = 'echo "$ file" | sed "s/$ {old}/$ {New}/g "'
If [-F "$ newfile"]; then
Echo "error: $ newfile exists already"
Else
Echo "Renaming $ file to $ newfile ..."
Mv "$ file" "$ newfile"
Fi
Fi
Done

Explanation:This is a complex example. Let's discuss it in detail. The first if expression determines whether the number of input command line parameters is smaller than 3 (special variable $ # indicates the number of parameters included ). If the number of input parameters is less than three, the help text is passed to the cat command and then printed on the screen by the cat command. Print the help text and exit the program. If the input parameter is equal to or greater than three, we assign the first parameter to the variable old, and the second parameter to the variable new. Next, we use the shift command to delete the first and second parameters from the parameter list, so that the original third parameter becomes the first parameter in the parameter list $. Then we start the loop. The command line parameter list is assigned to the variable $ file one by one. Then we determine whether the file exists. If yes, we use the SED command to search for and replace the file to generate a new file name. Then, assign the command result in the backslash to newfile. In this way, we get the old and new file names. Use the MV command to rename the file.

4.
"[]" Is usually used to represent a conditional test. Note that spaces are important. Make sure that the square brackets have spaces.
[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.
[-N "$ Var"]: determines whether the $ var variable has a value.
[-E "somefile"]: determines whether a file exists.
[-D "somefile"]: determines whether it is a folder.
[-R "somefile"]: determines whether the file is readable.
[-W "somefile"]: determines whether a file can be written.
[-X "somefile"]: determines whether the file is executable.
["$ A" = "$ B"]: determines whether $ A and $ A are equal.

5. Double quotation marks, single quotation marks, and reverse quotation marks (post reference)
Double quotation marks:All special characters in quotation marks are blocked for re-interpretation-including variable names-except for $, '(post reference) And. $ is reserved as a special character, so that variables ("$ Var") can be normally referenced in double quotation marks ").
Use "" to prevent word segmentation. [4] If double quotation marks are used in the parameter list, the parameters in double quotation marks are used as a parameter. even if the string in double quotation marks contains multiple words (that is, the blank part), it will not be changed to multiple parameters.

Single quotes:
The single quotation mark operation is generally similar to "", but variables cannot be referenced. Because the special meaning of $ is disabled, except 'in ''.
The characters have no special meaning. therefore, single quotes are stricter than double quotes. even if it is/, it is disabled in '', so you want to display the meaning of'' in '', and the expected effect will not be achieved.

Reverse quotation marks (post reference ):Command replacement will re-allocate the output of one or more commands; it will add the output of the command to another context in the field. A typical form of command replacement is that commands in the form of post reference ('...') and post reference (that is, commands in the form of reverse quotation marks) will generate command line text.
The significance of post-reference is that the command output can be used as a parameter passed to another command, saved to a variable, or even used to generateThe list of cyclic parameters.
For example:
#! /Bin/sh
File abc.zip # execute and output, but cannot assign the command line output value to a variable
File_type = 'file abc.zip '# execute and assign the output value to the variable file_type.

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.