In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the commonly used built-in variables in Shell scripts". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the commonly used built-in variables in Shell scripts"?
In the Bash interpreter, there are many variables built in. The functions of these variables are included in the interpreter. If we can use them flexibly when writing shell scripts, it will be of great help to the script writing efficiency and errors. Here are some of these variables.
$FUNCNAME, $LINENO, $PWD
FUNCNAME and LINENO variables are often used to debug scripts
FUNCNAME represents the name of the current function. It can only be used in the function and has no value outside the function.
LINENO indicates the line number in which the variable appears in the current script
PWD represents the current directory, corresponding to the pwd command
The existing script a.sh contains the following
1 #! / bin/bash 2 34 testa () 5 {6 echo 'func='$FUNCNAME,$LINENO 7} 8 9 10 testa 11 12 echo' lineno:'$LINENO 13 echo 'xx:'$FUNCNAME 14 echo' curpath:'$PWD
Execute the. / a.sh command, and the output is as follows
[tt@ecs-centos-7 ~] $. / a.sh func=testa,6 lineno:12 xx: curpath:/home/tt
In the testa function, the value of the FUNCNAME variable is testa, that is, the name of the function, and has no value outside the function.
The LINENO variable represents the current line number, whether in or outside the function.
$$, $PPID
These two variables represent the current process ID and the parent process ID in turn
The existing a.sh script, which contains the following
The sleep 20 sleep statement is designed to suspend the execution of the script process and verify the output in another terminal.
#! / bin/bash # set-u echo 'cur pid:'$$ echo' parent pid:'$PPID sleep 20
Execute. / a.sh on the current terminal, and the result is as follows
[tt@ecs-centos-7] $. / a.sh cur pid:13095 parent pid:12982
Before the script process that executes a.sh exits, open another terminal and execute the command ps-o pid,ppid,time,cmd-p 12982 pid,ppid,time,cmd 13095. The result is as follows
[tt@ecs-centos-7] $ps-o pid,ppid,time,cmd-p 12982 13095 PID PPID TIME CMD 12982 12981 00:00:00-bash 13095 12982 00:00:00 / bin/bash. / a.sh
As you can see from the above results, after executing the. / a.sh command, the $$variable represents the process ID 13095 that executes the a.sh script, and 12982 is its parent process ID, that is, the value of the PPID variable, which is an instance of the current bash
$050, $#, $#
$# indicates the number of parameters passed in to the script from the command line
The parameter passed from the command line to the script $0 is the name of the script $1 is the first parameter $2 is the second parameter, and so on $n is the nth parameter
The tenth parameter and subsequent parameters must be enclosed in curly braces. For example, ${10}, ${11} and ${12} represent the tenth variable, the eleventh variable and the twelfth variable in turn.
$*, $@
All represent position parameters, but there are some differences between them.
When using $*, if you add double quotation marks, that is, in the form of "$*", then the parameters of all positions will be treated as one word, and if they do not include double quotes, that is, in the form of $*, then the parameters of each position are treated as a separate word.
For $@, with or without double quotation marks, the parameters of each position are treated as a separate word
Existing c.sh, the content is as follows
#! / bin/bash cnt=1 echo 'test 1111' for var in "$*" do echo "arg$cnt=" $var let "cnt+=1" done echo cnt=1 echo' test 2222' for var in $* do echo "arg$cnt=" $var let "cnt+=1" done echo cnt=1 echo 'test 3333' for var in "$@" do echo "arg$cnt=" $var let "cnt+=1" done echo cnt=1 echo "test 4444" for var in $@ do echo "arg$cnt=" $var let "cnt+=1" done "
Execute. / c.sh 1 2 3, and the results are as follows
[root@ecs-centos-7] #. / c.sh 1 2 3 test 1111 arg1=1 2 3 test 2222 arg1=1 arg2=2 arg3=3 test 3333 arg1=1 arg2=2 arg3=3 test 4444 arg1=1 arg2=2 arg3=3
As can be seen from the above results, for $*, all positional parameters are treated as one word after adding double quotation marks
For $@, whether or not to add double quotation marks, the result is the same
Therefore, $* and $@ are different only when using double quotation marks
$?
The exit state of a command, function, or script is useful in judging the execution result of a command or the result of a function call.
Existing e.sh and f.sh test scripts
E.sh script
#! / bin/bash test_func () {if [[$1-eq 10]]; then return 5 fi return 6} if [$#-ge 1]; then name= "$1" shift 1$ name "$@" fi
F.sh script
#! / bin/bash sh e.sh test_func 3 echo 'exit code1:'$? Sh e.sh test_func 10 echo 'exit code2:'$? Test-f $PWD/xx.txt echo 'exit code3:'$? Test-f $PWD/e.sh echo 'exit code4:'$?
Execute the. / f.sh command, and the result is as follows
[root@ecs-centos-7 ~] #. / f.sh exit code1:6 exit code2:5 exit code3:1 exit code4:0
The function of the test_func function in the script e.sh is: when the parameter is equal to 10:00, the exit status is 5, otherwise it is 6
The sh e.sh test_func 3 command calls the test_func function in the e.sh script, with an argument of 3, so the exit status is 6. Similarly, the exit status of the sh e.sh test_func 10 command is 5.
In Linux, the command executes successfully, the exit status is 0, and the failure is non-0
The test-f $PWD/xx.txt command checks whether a xx.txt file exists in the current directory. Because there is no xx.txt in the current directory, the command execution fails, and the exit status is non-0.
Because e.sh exists in the current directory, the test-f $PWD/e.sh command was executed successfully with an exit status of 0
$IFS
This variable is used by Bash to identify strings or word boundaries. The default value is a space. The value of this variable can be modified as needed in the script.
The existing b.sh script, which contains the following
#! / bin/bash va= "a:b:c" vb= "x-y-z" vc= "eJournal fje g" IFS= ":" echo 'va:'$va echo' vb:'$vb echo 'vc:'$vc echo IFS= "-" echo' va:'$va echo 'vb:'$vb echo' vc:'$vc echo IFS= "," echo 'va:'$va echo' vb:'$vb echo 'vc:'$vc "
The results of executing. / b.sh are as follows
[tt@ecs-centos-7] $. / b.sh va:a b c vb:x-y-z vc:e,f,g va:a:b:c vb:x y z vc:e,f,g va:a:b:c vb:x-y-z vc:e f g
As can be seen from the result, when $IFS is:, the string "a:b:c" is parsed to a b c
When $IFS is -, the string "x-y-z" is parsed to x y z
When $IFS is, the string "eForce fjorg" is parsed into e f g.
$HOME, $USER, $UID, $GROUPS
HOME: user home directory USER: current user name UID: current user ID GROUPS: current user group ID [tt@ecs-centos-7 ~] $echo $HOME / home/tt [tt@ecs-centos-7 ~] $echo $USER tt [tt@ecs-centos-7 ~] $echo $UID 1003 [tt@ecs-centos-7 ~] $echo $GROUPS 1003
$HOSTTYPE, $MACTYPE, $OSTYPE
These variables represent the system hardware.
[tt@ecs-centos-7 ~] $echo $HOSTTYPE x8634 [tt@ecs-centos-7 ~] $echo $MACHTYPE x86_64-redhat-linux-gnu [tt@ecs-centos-7 ~] $echo $OSTYPE linux-gnu Thank you for reading. These are the contents of "what are the built-in variables commonly used in Shell scripts?" after the study of this article, I believe you have a deeper understanding of the built-in variables commonly used in Shell scripts. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.