SCP command and SCP command usage in Linux encyclopedia _linux Shell

Source: Internet
Author: User
Tags scp command
label:

Variable names can only contain numbers, letters, and underscores, because some variables containing other characters have special meanings, such variables are called special variables.

For example, $ represents the ID of the current shell process, that is, pid. Look at the following code:

$ echo $$
operation result

29949
 

List of special variables
Variable Meaning
$ 0 filename of current script
$ n Arguments passed to the script or function. n is a number indicating the number of parameters. For example, the first parameter is $ 1 and the second parameter is $ 2.
$ # The number of arguments passed to the script or function.
$ * All parameters passed to the script or function.
[email protected] All parameters passed to the script or function. When enclosed in double quotes (""), it is slightly different from $ *, as described below.
$? The exit status of the previous command, or the return value of the function.
$$ The current shell process ID. For shell scripts, this is the process ID where these scripts are located. Command line arguments
The parameters passed to the script when it is run are called command line parameters. Command line parameters are represented by $ n. For example, $ 1 represents the first parameter, $ 2 represents the second parameter, and so on.

Consider the following script:

#! / bin / bash
echo "File Name: $ 0"
echo "First Parameter: $ 1"
echo "First Parameter: $ 2"
echo "Quoted Values: [email protected]"
echo "Quoted Values: $ *"
echo "Total Number of Parameters: $ #"
operation result:

$. / test.sh Zara Ali
File Name: ./test.sh
First Parameter: Zara
Second Parameter: Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters: 2
Difference between $ * and [email protected]
Both $ * and [email protected] indicate that all parameters passed to the function or script are output as "$ 1" "$ 2"… "$ n" when not enclosed by double quotes ("").

But when they are enclosed in double quotes (""), "$ *" will output all parameters as a whole, and output all parameters as "$ 1 $ 2… $ n"; "[email protected]" will convert each parameter Separately, output all parameters as "$ 1" "$ 2"… "$ n".

The following example clearly shows the difference between $ * and [email protected]:

#! / bin / bash
echo "\ $ * =" $ *
echo "\" \ $ * \ "=" "$ *"
echo "\ $ @ =" [email protected]
echo "\" \ $ @ \ "=" "[email protected]"
echo "print each param from \ $ *"
for var in $ *
do
echo "$ var"
done
echo "print each param from \ $ @"
for var in [email protected]
do
echo "$ var"
done
echo "print each param from \" \ $ * \ ""
for var in "$ *"
do
echo "$ var"
done
echo "print each param from \" \ $ @ \ ""
for var in "[email protected]"
do
echo "$ var"
done
Run ./test.sh "a" "b" "c" "d" and see the following results:

$ * = a b c d
"$ *" = a b c d
[email protected] = a b c d
"[email protected]" = a b c d
print each param from $ *
a
b
c
d
print each param from [email protected]
a
b
c
d
print each param from "$ *"
a b c d
print each param from "[email protected]"
a
b
c
d
Exit status
$? Can get the exit status of the previous command. The so-called exit status is the result returned after the previous command was executed.

The exit status is a number. Generally, most commands return 0 if they are executed successfully and 1 if they fail.

However, there are commands that return other values that indicate different types of errors.

In the following example, the command is successfully executed:

$. / test.sh Zara Ali
File Name: ./test.sh
First Parameter: Zara
Second Parameter: Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters: 2
$ echo $?
0
$

$? Can also represent the return value of a function

 

 

 

 

 

1 shell variable basics
Shell variables are very "weak" variables. By default, a variable holds a string, and the shell doesn't care what the string means. So to perform mathematical operations, you must use some commands such as let, declare, expr, double brackets, etc. Shell variables can be divided into two categories: local variables and environment variables. Local variables are only available in the shell that created them. Environment variables can be used in the shell that created them and any child processes derived from them. Some variables are user-created, others are special shell variables. Variable names must begin with a letter or an underscore character. The remaining characters can be letters, numbers (0 ~ 9) or underscore characters. Any other characters mark the end of the variable name. The name is case sensitive. When assigning a value to a variable, there must be no whitespace around the equal sign. To assign a null value to a variable, you can follow the equal sign with a newline character. Use the set command to view all variables, and the unset var command can clear the variable var, which is equivalent to undefined. readonly var can turn var into a read-only variable. You cannot make any changes to var after it is defined. There are many ways to refer to shell variables. You can use these methods to easily obtain the value of the shell variable, the length of the variable value, a string of the variable, the value of the variable after it is partially replaced, and so on. Common ways to quote shell variables are as follows:

2 environment variables
The environment variables are defined as follows:
var = value
export var
When the shell is initialized, it will execute initialization scripts such as profile. The script defines some environment variables that will be passed to the child process when the child process is created.
Use the env command to view the current environment variables. The common system environment variables are as follows:
_ (Underscore) The last parameter of the previous command
BASH expands to the full path name used when calling the bash instance
CDPATH Search path for the cd command. It is a colon-separated list of directories through which the shell searches for the target directory specified by the cd command. For example :::: / usr
EDITOR pathname of built-in editor emacs, gmacs or vi
ENV Environment file that is executed when each new bash shell (including script) is launched. The file name usually assigned to this variable is .bashrc.
EUID expands to the effective ID of the current user that was initialized at shell startup
GROUPS The group to which the current user belongs
HISTFILE Specifies a file that holds the history of the command line. The default value is ~ / .bash_history. If reset, interactive shell will not save command line history when exiting
HISTSIZE records the number of commands in the command line history file. Default is 500
HOME home directory. When no directory is specified, the cd command will go to that directory
IFS internal field delimiters, usually space, tab, and line break, used for field substitution of words generated by command substitution, looping through tables in tables, and read input
LANG is used to determine the locale class for types that are not explicitly selected for variables that begin with LC_
OLDPWD Previous working directory
PATH command search path. A colon-separated list of directories that the shell uses to search for commands. A common value is / usr / gnu / bin: / usr / local / bin: / usr / ucb: / usr / bin
PPID Process ID of the parent process
PS1 main prompt string, the default value is $
PS2 secondary prompt string, the default value is>
PS3 Selection prompt string used with the select command. The default value is #?
PS4 Debug prompt string used when tracing is enabled. The default value is +. Tracing can be turned on with set -x
PWD current working directory. Set by cd
Each time RANDOM references the variable, it generates a random integer. The random number sequence can be initialized by assigning a value to RANDOM. If RANDOM is reset, it will lose certain properties even if it is subsequently set
REPLY Set when no parameter is provided for read
SHELL When the shell is invoked, it scans the environment variable for that name. The shell sets default values for PATH, PS1, PS2, MAILCHECK, and IFS. HOME and MAIL are set by login (1)
SHELLOPTS contains a list of open shell options, such as braceexpand, hashall, monitor, etc.
UID expands to the user ID of the current user, initialized at shell startup
3 Numeric variables
Variable values are treated as strings by default in the shell, for example:
age = 22
age = $ {age} +1
echo $ {age}
The output is 22 + 1, not 23, because the shell interprets it as a string, not a mathematical operation.
You can use the let command to perform mathematical operations, such as:
let age = $ {age} +1
You can also use declare to define a variable as an integer. E.g:
declare -i age = 22
Here, the -i option is used to define age as an integer. Each operation thereafter recognizes the rvalue of age as an arithmetic expression or number.
4 Array
You can use arrays in the shell, for example:
array [0] = 0
array [1] = 1
array [2] = 2
Then array is an array. You can also initialize the array like this:
array = (0 1 2) // elements are separated by spaces
You can access an element in the array through $ {array [$ i]}. The return value of $ {array [*]} is a string consisting of all elements of the array, and the return value of $ {# array [*]} is an array The number of elements, $ {array [*]: 0: 2} returns a string consisting of the first and second elements. 0 indicates the starting position, 2 indicates the number of elements to be returned, and the starting position can be 0-2 (0 minus 2) and the like, indicating that it starts from the penultimate element.
Let's write a slightly more complicated example:

1 #! / Bin / bash
2 for ((i = 0; i <100; i ++))
3 do
4 array [$ i] = $ i
5 done
6 for ((i = 0; i <100; i ++))
7 do
8 echo $ {array [$ i]}
9 done
If you want to use a two-dimensional array or even a three-dimensional array, you need to use the eval command to simulate the function of the array.
The function of the eval command is to scan the command twice and then execute it. If you do not use eval, scan only once and then execute it. Look at an example:
[email protected]: ~ $ name = Barry
[email protected]: ~ $ $ name = hello
Barry = hello: command not found
Why does the second sentence assign a value to a Barry variable? From the error message, it can be found that the shell did not recognize this as an assignment statement, but executed Barry = hello as a command, and of course it will report an error. Why can't we recognize that this is an assignment statement? In the first scan, because the $ symbol is scanned, this sentence cannot be regarded as an assignment statement. The left side of an assignment statement is always a variable name, and should not begin with $. So the first scan only recognized the $ name variable and replaced it, but did not recognize the assignment statement.
What if eval $ name = hello?
[email protected]: ~ $ name = Barry
[email protected]: ~ $ $ name = hello
Barry = hello: command not found
[email protected]: ~ $ eval $ name = hello
[email protected]: ~ $ echo $ Barry
hello
It can be seen that after using eval,
e = hello replaces $ name in the first scan and does not recognize the assignment statement. The second scan identifies the assignment statement and executes it. Now you can think about how to implement a two-dimensional array with eval.
Each row of the two-dimensional array implemented below represents a person's information record, including name, age.

 1 for ((i = 0; i <2; i ++))
 2 do
 3 for ((j = 0; j <2; j ++))
 4 do
 5 read man $ i $ j
 6 done
 7 done
 8 echo "next print:"
 9 for ((i = 0; i <2; i ++))
10 do
11 for ((j = 0; j <2; j ++))
12 do
13 eval echo -n "\ $ man $ i $ j:"
14 done
15 printf "\ n"
16 done
5 Special variables
$ 0: the file name of the current script
$ num: num is a number starting from 1, $ 1 is the first parameter, $ 2 is the second parameter, and $ {10} is the tenth parameter
$ #: The number of parameters passed to the script
$ *: All positional parameters (as a single string)
[email protected]: All positional parameters (each as a separate string).
$ ?: The return value of the previous command in the current shell process. If the previous command is successfully executed, the value of $? Is 0, otherwise it is a non-zero value. It is often used as an if statement.
$$: pid of the current shell process
$ !: pid of the last process running in the background
$-: Display the current options used by the shell
$ _: The last parameter of the previous command

Shell special variables: Shell $ 0, $ #, $ *, [email protected], $ ?, $$ and command line arguments
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.