Self-study Linux shell11.3-using variables

Source: Internet
Author: User
Tags php language

Click Back to learn the path of the Linux command line and shell script

11.3-Using variables

The execution of a shell script can typically take several ways:
1):bash script-name or sh script-name(recommended)
2): Path/script-name or./script-name (Execute script under current path)
3): Source Script-name or. Script-name #→ note "." Point number.
4): Sh<script-name or cat scripts-name|sh (same for bash)

when defining a variable, the variable name does not have a dollar sign (required for variables in the $,php language), such as:

your_name="Runoob.com"

the name of the variable must be named according to the following rules :

    • The name can only use English letters, numbers and underscores, and the first character cannot begin with a number
    • Cannot have spaces in the middle, you can use underscores (_)
    • Punctuation cannot be used
    • You can't use the keywords in bash (you can see the reserved keywords with the help command)
    • There can be no spaces between the variable name and the equals sign
    • If a row has only one command, each line does not end with a semicolon, and each command is separated by a newline
      If a row has multiple commands, each command is separated by semicolons, but only one command per line is recommended




1. Common variables:

1.1 Environment variables: variables must be uppercase

    • Configure persistent environment variables, global variables configuration files:/etc/profile,/ETC/BASHRC basically use/etc/profile
    • Configure profiles for user's environment variables: hidden files in the user's home directory: ~/.bash_profile, ~/.BASH_BASHRC
    • Temporary Settings # export Zsy=1
    • Set the directory in effect for all users:/etc/profile.d/: The directory must be a script or it cannot be used
    • If you write a Java script, put the Java environment variables in the script, especially when the scheduled tasks are executed
    • To display environment variables:
      # env //* environment variables displayed
      # Set //* show environment variables more
    • Canceling environment Variables
      # unset < environment variable name >//* temporary cancel variable
      Go to file Delete variable

1.2 Meaning of environment variables:

    • UID: UID of the current user
    • User: currently logged in users
    • Home: The current user home directory
    • _=: Last parameter of previous command
    • BASH: The full path used when invoking an instance
    • PS1: the current terminal
    • Path: Location of the command

1.3 Local Variables:

    • General consecutive strings, numbers, paths, etc. can be without any quotation marks, but the case without quotation marks is best replaced by double quotation marks.
    • If you use a different variable in a variable, you cannot use single quotation marks otherwise you cannot use it:

[Email protected] ~]# a= ' $USER '
[Email protected] ~]# echo $a
$USER

    • If there are spaces in the variable, you must use double quotation marks otherwise it cannot be output:

[Email protected] ~]# A=1 2 3
-bash:2: Command not found
[[email protected] ~]# a= "1 2 3"
[Email protected] ~]# echo $a
1 2 3

1.4 Meaning of quotation marks

    • No quotation marks: the output of the content, will not contain the space of the string as a whole output; If there is a command (to be in the inverted quotation mark), the variable, the special escape character resolves the result then the child in the output final content, if the string has the space and so on special character Fu Ze then cannot complete the output, needs to enclose the double quotation mark, the general continuous string, the number, the path and so on can not add any quotation marks However, it is best to use double quotes instead of quotation marks.
    • Single quotes: What you see is what you get: Everything inside the single quotation marks is output as is, or what you see in the inside will output what
    • Double quotation marks: The output of everything inside the double quotation marks, if there is a command (to be in the inverted quotation mark), a variable, a special escape character resolves the result, and the child outputs the final content.
    • Anti-quotes: Generally used to execute commands, executed when the command executes, similarly can use a=$ (LS): The result is the same

1.5 Summary of variable definitions:
Common variables:
A=1 consecutive string of numbers
A= "/etc/rc.local $USER" output after parsing results
A= ' $USER ' as-is output
Command content definition:
A= ' ls ' anti-quote//* the character in the anti-quote as the command output
a=$ (LS)

2. Special variables:
2.1-bit variable
$n: Gets the nth parameter value of the currently executing shell script (The parameter value is separated by a space by default, if the argument is double quotation marks, the whole of the double quotation marks as an argument, regardless of whether there are spaces, such as Example 3), n=1..9, when n is 0 indicates the file name of the script, if n is greater than 9, enclosed in curly braces ${10}, the parameters are separated by spaces. As follows:
Example 1:

[[email protected] ~]# echo $ A
A
[[email protected] ~]# echo $ A B
A b

Example 2:

[email protected] ~]# cat p.sh
echo $ $4 $ $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}
[Email protected] ~]# sh p.sh {a.. Z
A b c d e F g h i j k l m n o

Example 3:

[email protected] ~]# cat p.sh
Echo
[[Email protected] ~]# sh p.sh "1 2" "3 4"
3 4//* Note When an integer exceeds 9 o'clock, the number should be expanded with "{}" or the following result will occur.

[email protected] ~]# cat p.sh
echo $ $4 $ $6 $7 $8 $9 $ $11 $ $13 $14 $
[Email protected] ~]# sh p.sh {a.. Z
A b c d e F G H i a0 A1 A2 A3 A4 A5

$ to get the name of the current script itself
Get the first parameter that is currently passed to the shell script
$ $ Get the second parameter currently passed to the shell script
$# gets the number of arguments currently passed to the script
$$ gets the current process PID number of the current script run
[Email protected] Get a list of all the parameters currently passed to the script
$* gets the parameters that are currently passed to the script in a single string, with more than 9 parameters, unlike positional variables
$? is to display the exit status of the last command, 0 means no error, others indicate an error

$ $ is the first parameter passed to the shell script, and the general system startup file has this at the end, as a command to receive input such as
#/etc/init.d/sshd Start//* is to assign the start value to $ $, and then the script executes the command under this parameter: The following script:

[email protected] ~]# cat p.sh
Case "$" in
Start
echo "123" && exit 0
;;
Stop
echo "234" && exit 0
;;
Esac
Exit
[[email protected] ~]#/etp.sh start
123
[[Email protected] ~]# sh p.sh stop
234

The sum is used to take the name of the script itself, which is the inner name of the heel of your command , such as the a.sh in SH a.sh

[email protected] ~]# cat a.sh
Echo
[Email protected] ~]# sh a.sh
a.sh

Example 1: In the startup script there is also a $, when the startup script when the input is incorrect when it appears

[Email protected] ~]# tail-4/etc/init.d/sshd |head-1
echo $ "Usage: $ {Start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
[Email protected] ~]#/etc/init.d/sshd *
Usage:/etc/init.d/sshd {start|stop|restart|reload|force-reload|condrestart|try-restart|status}

Example 2: Separating a file from a name

[email protected] etc]# cat ~/a.sh
DirName
BaseName
[Email protected] etc]# sh ~/a.sh
/root
a.sh

* DirName $ get is the path
* Bashname-Won is the name

$#: Gets the number of parameters currently passed to the script, typically used to control the number of parameters, such as Example 1.

[email protected] ~]# cat a.sh
Echo $#
[[Email protected] ~]# sh a.sh 1 2 3 4 5 6
6

Example 1: Judging the parameters of less than two, tell the report script name plus "ARG1 ARG2", equal to or greater than two parameters, show money two parameters

[email protected] ~]# cat a.sh
#!/bin/bash
[$#-ne 2] && {
echo "$ ARG1 ARG2"
Exit 1
}
echo $
[Email protected] ~]# sh a.sh
a.sh ARG1 ARG2
[[Email protected] ~]# sh a.sh 1 2
1 2

* Exit 1 in the script refers to the return value: The following example 2

Example 2:

[email protected] ~]# cat p.sh
echo "123456789"
Exit 28
[Email protected] ~]# sh p.sh
123456789
[[Email protected] ~]# sh p.sh; echo $?
123456789
28

$? The return value of the command execution, 0 means no error, and the other indicates an error

[[email protected] ~]# ls; echo $?
a.sh iptables.sh p.sh
0
[email protected] ~]# 123; echo $?
-bash:123:command not found
127//* semicolon is a spacer, indicating the meaning of the preceding and subsequent two commands, executing the previous one, performing the latter one, and two commands without any dependencies

The return value represents the meaning:

    • 0: Success
    • 1 or 2: Permission denied, indicating insufficient permissions
    • 1~125: Indicates a run failure, script command, System command error, or parameter pass error;
    • 126: Command was found, but could not be executed
    • 127: Command not found,
    • Greater than 128: Exit from command execution

Self-study Linux shell11.3-using variables

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.