In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Preface to variables in shell programming what are shell variables? Why do I need the shell variable? How do I define shell variables?
This article mainly takes you to understand the relevant concepts of shell variables, the use and role of shell variables and other basic knowledge, suitable for beginners to learn, understand.
I. the concept of shell variable
belongs to the concept of "variable" in all kinds of shell environments. Shell variables are used to store specific parameters (values) that the system and users need to use, and these parameters can be changed according to user settings or changes in the system environment. By using variables, shell programs can provide more flexible functions and more adaptability.
The common types of shell variables in include custom variables, environment variables, location variables, and predefined variables.
1. Custom variables
The system is defined by users and is valid only in their own shell environment.
The following examples explain how to define new variables, how to view and reference variables, and special operations for variable assignments.
[root@localhost shell] # Value=tome [root@localhost shell] # Version=6.0 [root@localhost shell] # echo $V$Value $Version [root@localhost shell] # echo $Value tome [root@localhost shell] # echo $Version 6.0[ root@localhost shell] # echo $Version4.5.5 [root@localhost shell] # echo ${Version} 4.56.04.5 / above is the variable definition and view operation / / the following is the variable assignment special operation [root@localhost shell] # vlan=vlan 5 .0bash: no command found. [root@localhost shell] # vlan= "vlan 5.0" / / A double quote [root@localhost shell] # echo $vlan vlan 5.0[ root@localhost shell] # ss=8.8 [root@localhost shell] # qq=" QQ $ss "[root@localhost shell] # echo $qqQQ 8.8 [root@localhost shell] # qq='QQ $ss' / / single quotes [root@localhost shell] # echo $qqQQ $ss [root@localhost shell] # Ls-lh `which useradd` / / reverse apostrophe (button below esc)-rwxr-x---. 1 root root 116K November 6 2016 / sbin/useradd [root@localhost shell] # read hehe / / enter 123 [root@localhost shell] # echo $hehe 123 from the keyboard
Set the scope of the variable, export sets the global variable, and bash can enter the child shell.
[root@localhost shell] # echo $hehe123 [root@localhost shell] # bash / / enter [root@localhost shell] # echo $hehe / / cannot call parent shell variable [root@localhost shell] # exit / / return exit [root@localhost shell] # echo $hehe123 [root@localhost shell] # export hehe / / define global [root@localhost shell] # bash [root@localhost shell] # echo $hehe123 in parent shell
Let's introduce the operation of numerical variables.
The numerical operations of shell variables are mostly used in the process control of script programs (such as the number of cycles, usage comparison, etc.). In Bash Shell environment, only simple integer operation can be performed, but decimal operation is not supported.
(1) addition operation: +
(2) subtraction:-
(3) multiplication: * (needs to be escaped)
(4) Division operation: /
(5) Modulus (remainder) operation:%
Operation example:
[root@localhost shell] # Xun 36 [root@localhost shell] # Yue 33 [root@localhost shell] # expr $X + $Y69 [root@localhost shell] # expr $X-$Y3 [root@localhost shell] # expr $X\ * $Y1188 [root@localhost shell] # expr $X / $Y1 [root@localhost shell] # expr $X% $Y3
two。 Special variable
(1) Environmental variables-can be modified
[root@localhost shell] # envXDG_SESSION_ID=11HOSTNAME=localhost.localdomainTERM=xtermSHELL=/bin/bashHISTSIZE=1000USER=rootLS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01 Lzmaooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 31PMORMUL01TOR 31DUBG. RPMUO1TOR 31TUBING .ACETRON01AND 31MAXING 31MAXING .ZOOOY01ANG 31PUBLING. 35 _ .flmvbang 01it.flmvbail.Avivo.FLINO1BZ .FLINO1BUTING .FLLATOR 01BING 35 SUBFOR .FLTVO1FLING 01FLING 01FLING 35 purveyors .GLING 01Extending 35 purposes.xcf01Tract35 purposes.xwd01ut35MUV01ut35MUB01MUBEN 35MUVOLING .AGMM01MYOUF01NT35MYOLINE .ANGVOLING .OGV01ATING 35 purposes.OGVOX01INE 35 purposes.OGX01ING .AAAC01ATOTRABING .AUAOTHUOTRABING. .mp3mist01it.mp3sec01it.mp3sec01it.mpcmist01lead.oggmist01it36purpose.rate01poli36purpose.axa01lead.axa01transfer36pur.oga01section36pur.spxaccount36fre.xspfroom01 36 _ / 0_=/bin/env [root@localhost shell] # echo $PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
(2) position variable-provides operation parameters for the program. Use $n (n range (1x 9))
[root@localhost shell] # vim add2.sh [root@localhost shell] # cat add2.sh #! / bin/bashsum= `expr $1 + $2`echo "$1 + $2 = $sum" echo $sum [root@localhost shell] #. / add2.sh 2 32 + 3 = 55
(3) predefined variables-you can only use variables that cannot be changed.
$#-the number of position parameters on the command line
$*-- the contents of all location parameters
$?-the status after the execution of the previous command. Returning 0 indicates correct and vice versa.
$0mure-script name
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.