In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to analyze bash variables in shell programming, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Introduction to shell programming-an introduction to bash variables, which are units of computer memory in which values can be changed. When a Shell script needs to save some information, such as a file name or a number, it is stored in a variable.
Bash variable type
Local variable
The scope of the local variable is for the entire bash process. Local variables belong to local variables. The period in which variables are declared in bash exists with the cycle of the process. Define local variables: set VARNAME=Jerry reference variable: ${VARNAME}. Parentheses omit the declaration of local variables: local VARNAME=VALUE
Environment variable
The scope of the environment variable is declared for the current shell process and its child processes: the export VARNAME=VALUE or VARNAME=VALUE; export VARNAME script will start a child shell process when it is executed. The script started on the command line will inherit the current shell environment variable. The script that the system executes automatically (non-command line startup) requires self-definition and requires each environment variable.
Location variable
$1, $2,... shift: rotate the parameters. By default, rotate a parameter #! / bin/bash#echo $1shiftecho $1shiftecho $1shift./filetest.sh / etc/fstab / etc/inittab $1:/etc/fstab $2:/etc/inittab
Special variable
$?: return value of the last command execution status program execution, there may be two types of return values: program execution result program status return code (0-255) 0: correct execution 1-255: error execution. 1, 2, 127 system reserved $#: number of parameters $*: parameter list $@: parameter list
Revocation and viewing of variables
Undo variable
Unset VARNAME
View variabl
Set: view variables in the current shell (including environment variables and local variables) printenv, env, export: view environment variables in the current shell
Variable naming rules
1. Variables and their contents are connected with an equal sign "=", as follows: MyName=Dabric2 and equal signs cannot be directly connected with space characters, such as error: MyName=Dabric or Myname=Dab ric3, variable names can only be English letters and numbers, but the opening character cannot be numeric. 4. If the variable content is empty, you can use double quotation marks or single quotation marks to combine the variable content, but the special characters in the double quotation marks, such as $, can retain the original characteristics, as follows: if var= "lang is $LANG", then echo $var can get the special characters in lang is en_US.UTF-8 single quotation marks, while the special characters in lang is en_US.UTF-8 single quotation marks are only normal characters (plain text). As follows: var='lang is $LANG' then echo $var can get lang is $LANG in short in bash: "": weak reference, can implement variable substitution': strong reference, do not complete variable replacement 5, special characters (such as [ENTER], $,\, space,', etc.) can be changed into general characters, such as MyName=Dab\ ric6, in the execution of a series of instructions. When you need information provided by other additional instructions, you can use backquotes "`COMMAND`" or "$(COMMAND)". For example, if you want to get the core version settings: how version=$ (uname-r) echo $version enters your current core module directory: # cd / lib/modules/ `uname-r` / kernel # cd / lib/moudles/$ (uname-r) / kernel7, if you change the amount to the augmented variable content, you can use "$variable name" or ${variable} cumulative content As follows: PATH= "$PATH": / home/bin or PATH=$ {PATH}: / home/bin8. If this variable needs to be executed in other subroutines, you need to use export to make the variable into an environment variable: # export PATH9. Uppercase characters are usually the system default variables. If you set your own variables, you can use lowercase characters to facilitate judgment. 10. The way to cancel the variable is to use unset: "unset variable name" for example, cancel the setting of MyName: # unset MyName variable can be directly specified in the bash configuration file (~ / .bashrc) to ensure that the variable exists every time you log in to shell. That is, how to analyze bash variables in shell programming. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.