Shell programming in the 4th Chapter Shell variable Advanced (upper)

Source: Internet
Author: User
1. Position variable
(1) $ ==> Take file name
[email protected] ~]# cat test.sh
Echo
[Email protected] ~]# sh test.sh
test.sh
[Email protected] ~]# sh ~/test.sh
/root/test.sh
Description: When executing a script, the script name is preceded by a path to the output path. The path is to be output if there is a path ahead. You can also use commands dirname and basename to take a value.
(2) $ #和 $n
$# ==> Number of parameters
Note: "" can only be counted as a variable, here set three variables first.
[[Email protected] ~]# set--"I am" ugly oldboy
[Email protected] ~]# echo $#
3
[Email protected] ~]# echo $
I am
[Email protected] ~]# Echo
Ugly
[Email protected] ~]# echo $3br/>oldboy
(3) The difference between $* and [email protected]
[email protected]~]# echo $</span>
I am Ugly Oldboy
[email protected] ~]# echo [email protected]
I am Ugly Oldboy
[[email protected] ~]# for i in "$
";d o echo $i;d one
I am Ugly Oldboy ==> The $* has only one parameter
[ [email protected]~]# for I in " [email protected]";d o echo $i;d one
I am
Ugly
Oldboy ==> [email protected]There are four parameters
2. Process state variables
(1) $? 0 for success, not 0 for failure
[ [email protected]~]# Date
May 01, 2018 Tuesday 18:58:42 CST
[ [email protected]~]# echo $?
0
[ [email protected]~]# RM/
RM: Cannot delete "/": is a directory
[ [email protected]~]# echo $?
1
In the enterprise scenario, the use of the $? Return value is as follows:
① to determine whether a program, such as a command, script, or function is successfully executed
② call Exit N in the script, will return this number to $?.
③ in the function, the number is returned by return N to the value of the function return to $?
(2) $$ get process number
Validation method ①:[ [email protected]~]# Cat testpid.sh
Echo $$
[ [email protected]~]# SH testpid.sh &
[3] 3357
[ [email protected]~]# 3357

[3]+ done sh testpid.sh
② (recommended): [[ Email protected] ~]# su-thzzc
[[email protected] ~]$ echo $$
2544
[[email protected] ~]$ kill-9 2544
[[email protected] ~]#
(3) $ The last argument of the last command (more than arguments, the same as the command, should be a word)
[[ Email protected] ~]# echo && echo $

Echo
[[email protected] ~]# echo Kobe && echo $
Kobe
Kobe
(4) $! Gets the process number of the last script
[email protected] ~]# cat testpid.sh
Sleep 100
[email protected] ~]# ps-ef |grep testpid
Root 6557 6311 0 15:48 pts/0 00:00:00 grep testpid
[[email protected] ~]# sh testpid.sh &
[1] 6558
[email protected] ~]# echo $!
6558
Summary: Position variable $ $n $# $* [email protected] 5 and process variables $$ $! $? $
4, all written down will be of great help to shell programming.

Shell programming in the 4th Chapter Shell variable Advanced (upper)

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.