Shell Scripting day three-special variables

Source: Internet
Author: User
First, position variables:

$ $ Gets the file name of the current execution script, including the script path.
$n gets the nth parameter of the current execution script, 1~9, greater than 9, enclosed in {}, for example: ${10}.
$# indicates the number of arguments

Seq 9|sed ' s#[0-9]#$& #g '
seq-s "$" 1 | sed ' s/1/$1/'


This function is described in the following two methods, which are used to produce a function seq, which is used to generate "$ $" until $ A.

Usage: seq [options] ... Tail
Or: seq [options] ... First Count Mantissa
Or: seq [options] ... First number increment Mantissa
Prints the number to the mantissa starting with the specified increment from the first number. The-f,–format= format uses the printf-style floating-point format-s,–separator= string to separate numbers using the specified string (by default: \ n)-w,–equal-width add 0 before the column to make the same width help display this help message and exit –v Ersion display version information and exit

If the first number or increment is omitted, the default value is 1, even if the mantissa is still less than the first number.
The first, increment, and mantissa are interpreted as floating-point numbers. Increments are generally positive when the first number is less than the mantissa,
Conversely, increments are generally negative when the first number is greater than the mantissa.
The specified format must apply to parameters that display the type "double", when the first number, increment, and mantissa are specified
The fixed-point decimal number for precision defaults to "%. Precision F", otherwise the default is "%g".

$ seq      #起始默认是 1, the interval default is also 1
$ seq 2     #从2打印到100, the interval default is 1
$ seq 1 3    #从1到10, the interval is 3, the result is: 1 4 7
$ s Eq-s "$" 1  #以 $ as separator print 1~10

For the use of SED functions, refer to:
http://blog.csdn.net/qicheng777/article/details/70847365

Run a simple shell program to observe the $ A and $ #具体含义:
Second, process status command:

1, \$? Gets the return value of the previous instruction.

It is generally used to determine whether the previous command succeeds if it succeeds to 0. The following is the return value of $?:

return value meaning
0 Indicates a successful run
2 Permission denied
1~125 Failed to run, script command, System command error, or parameter pass error
126 The command was found but could not be executed
127 The command to run was not found
>128 command is forced to end by system

Instance:

[Yangni@centos6 shell]$ find-aa
find:unknown predicate '-aa '
[YANGNI@CENTOS6 shell]$ echo $?  
1              //Parameter error   

[YANGNI@CENTOS6 shell]$ maek     
-bash:maek:command not found
[YANGNI@CENTOS6 shell]$ echo $?
] 127            //Command not found

[yangni@centos6 shell]$./a.sh
-bash:./a.sh:permission denied
[YANGNI@CENTOS6 Shell] $ echo $?
126            //Find command, unable to execute

[yangni@centos6 shell]$ ls
a.sh
[Yangni@centos6 shell]$ echo $?
0              //Successful execution return 0

2. $$ gets the current shell process number.

Write a dead loop to test:

#!/bin/bash
echo $$ > A.log while 
true
does
    uptime
    sleep 2
done

Some other status commands:

$! The PID of the previous instruction
$_ the last parameter of the previously executed command or script
$* all parameters as a single string
$@ all parameters of the program, which is the best way to pass parameters to other programs
   
the difference between $* and $@:

$* treats all parameters as a single string
$@ all parameters of this program, which is to pass parameters to other programs
Best way

If quoted:
"$1$2$3" single parameter

$@ all parameters of this program, which is the best way to pass parameters to other programs "$" "$" $  Three parameters

if you do not quote, the printing will be different

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.