"Learn Linux with older boys Koriyuki shell Programming Practice"-Chapter III Shell variable knowledge advanced

Source: Internet
Author: User
Tags echo command

This article explains the advanced knowledge of shell variables.

The following is a summary of the knowledge points, about the practice, this article does not involve, but also the small partners to practice their own!

For special positional parameter variables in 1.shell, see the following table:

650) this.width=650; "Width=" "height=" 276 "title=" Shell6.png "style=" WIDTH:600PX;HEIGHT:276PX; "alt=" Wkiom1j2-b3zklsbaaeirq4futi660.png "src=" https://s2.51cto.com/wyfs02/M01/91/96/ Wkiom1j2-b3zklsbaaeirq4futi660.png "border=" 0 "vspace=" 0 "hspace=" 0 "/>

In the enterprise scenario, "$?" Use of:

(1) Determine whether the command, script or function, and other programs to perform successfully;

(2) If the "exit number" is called in the script, the number will be returned to "$?" Variable

(3) If it is in a function, pass "return number", the number is passed to "$?" in the form of function return value.


The following knowledge points may also be used when applying the above-mentioned special position variables:

    • Get the path to the script: DirName path/script-name

    • Get script file name: basename path/script-name

    • For example, for a simple test with a for loop, for I;do Echo $i;d One, many netizens may see some do not understand, general for loop

      Will write for I in ..., which omits the in variable list, which is equivalent to the quoted in "[email protected]"

    • Shift the position parameter with shift (move left)

2.Bash shell built-in variable command

(1) Echo: Output information on the screen

Command format: Echo args xxx ==>xxx can be a combination of strings and commands

Function Description: Displays the string and variables specified by args behind the echo command to the standard output.

Common parameters are as follows:

650) this.width=650; "Width=" "height=" 149 "title=" Shell9.png "style=" WIDTH:600PX;HEIGHT:149PX; "alt=" wKiom1j2_ Gwblekxaaarlrohztw459.png "src=" https://s3.51cto.com/wyfs02/M00/91/98/wKiom1j2_GWBleKxAAArlrOhZTw459.png "border = "0" vspace= "0" hspace= "0"/>

(2) Eval

Command format: eval cmd ==>cmd contains variables and other commands

Function: When the shell program executes to the Eval statement, the shell reads the parameter cmd and combines them into a new command, then executes, in simple words,

That is, Eval will scan the back of the cmd two times, if after the first scan, CMD is a normal command, then execute this command, if CMD contains

Variable, the first scan confirms the value of the variable and then scans it twice to get the result, for example:

[[email protected "]# set--Arg1 arg2        ==> set two parameters via set
[[email protected]]# Echo $#                     ==>$ #表示传参的个数
2
[email  protected]]# echo \$$#                  ==> because $ #为2, this command becomes echo $, but does not output a value of $, but instead outputs the $,

This is also mentioned above to mention a scan that will $ #替换为 $
$
[[Email protected]]# eval "echo \$$#" ==>eval for the 2nd time, directly output echo value ARG2
Arg2

(3) Exec

Command format: EXEC command parameters

function: The EXEC command can go to execute the specified command without creating a new child process, and when the specified command is completed, the process will

Terminated, if the session is a remote connection, the session exits directly after the command is executed.

As follows:

[[email protected] scripts]# exec date ==> run out of this command and exit the current session directly
Wed APR 16:18:31 CST 2017

Connection closed.

Type ' help ' to learn. Xshell prompt.
Xshell:\>
Connection closed by foreign host.

Also, when the file is opened with exec, the read command moves the file pointer to the next line of the file each time until the end of the file,

This enables processing of file content.

For example:

[[email protected] ~]# SEQ 5 >/tmp/seq.txt

[email protected] scripts]# cat exec.sh
EXEC </tmp/seq.txt
While Read line
Do
Echo $line
Done
Echo OK
[Email protected] scripts]# sh exec.sh
1
2
3
4
5
Ok

(4) Read

Command format: Read variable name table

Function: Read a string from a standard input and pass it to a variable defined inside the shell program (explained in the next section)

(5) Shift: Shift the positional parameter (left), move the position parameter $, $, etc. to the left, i.e. if the position parameter is $ $, $, $,

Then after a shift is performed, the $ $2,$2 becomes $1,$1 disappears.

(6) Exit:shell program, of course exit can also choose to execute the number as the return value.

3.shell variable Quantum string knowledge

650) this.width=650; "Width=" 720 "height=" 305 "title=" 1.png "style=" width:720px;height:305px; "alt=" Wkiol1j4d1jcztx-aafomugkd9o187.png "src=" https://s5.51cto.com/wyfs02/M01/91/B9/ Wkiol1j4d1jcztx-aafomugkd9o187.png "border=" 0 "vspace=" 0 "hspace=" 0 "/>

Tips:

The colon of the expression in the above ID 11-14 can be omitted, and if the colon is omitted, the "empty or Unassigned" section in each definition is changed to "Unassigned".

For ease of understanding, here is an example of ${#parameter # #word}:

${#parameter percent A*c}: Here the a*c represents a matching string, * means match all, a*c represents a string that matches the beginning of a, the middle is any number of characters, and the end is C.



Above for I read "with the old boy learn Linux Yun Koriyuki Shell programming Combat" This book when the note, if there is any copyright issues, please contact [email protected].




This article is from the "shayatou_1990" blog, make sure to keep this source http://shayatou1990.blog.51cto.com/12806916/1917612

"Learn Linux with older boys Koriyuki shell Programming Practice"-Chapter III Shell variable knowledge advanced

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.