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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the "shell programming variables, strings, arrays, functions of the use of" related knowledge, Xiaobian through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "shell programming variables, strings, arrays, functions of the use of" the article can help you solve the problem.
one。 Variable definition
When you define a variable, the variable name is not marked with a dollar sign
Your_name= "jb51.net"
Note that there can be no spaces between the variable name and the equal sign.
In addition to explicitly directly assigning values, you can also assign values to variables in sentences, such as
For file in `ls / etc` or for file in $(ls / etc)
Defined variables can be redefined, such as
Your_name= "tom" echo $your_nameyour_name= "alibaba" echo $your_name
Note that you cannot write $your_name= "alibaba" on the second assignment, but only add the dollar sign ($) when using variables.
Use variables
To use a defined variable, simply precede the variable name with a dollar sign, as shown in
Your_name= "qinjx" echo $your_nameecho ${your_name}
The curly braces outside the variable name are optional and can be added or not.
Curly braces are added to help the interpreter identify the boundaries of variables, such as in the following case
For skill in Ada Coffe Action Java; do echo "I am good at ${skill} Script" done
It is recommended to add curly braces to all variables, which is a good programming habit.
Read-only variable
Using the readonly command, you can define a variable as a read-only variable, and the value of a read-only variable cannot be changed.
The following example attempts to change a read-only variable and reports an error
#! / bin/bashmyUrl= "/ / www.yisu.com" readonly myUrlmyUrl= "http://www.jb51.com"
Run the script and the result is as follows
/ bin/sh: NAME: This variable is read only. Delete a variable
You can delete a variable using the unset command
#! / bin/shmyUrl= "/ / www.yisu.com" unset myUrlecho $myUrl
The execution of the above example will have no output.
Note: the unset command cannot delete read-only variables.
Variable type
When running shell, three variables exist at the same time:
(1) Local variables are defined in scripts or commands, and are only valid in the current shell instance. Other programs started by shell cannot access local variables.
(2) Environment variables all programs, including those started by shell, can access environment variables, and some programs need environment variables to ensure their normal operation.
Shell scripts can also define environment variables if necessary.
(3) shell variable shell variable is a special variable set by the shell program.
Some of the shell variables are environment variables and some are local variables, which ensure the normal operation of shell.
Special variable use $0 the file name of the current script $n is passed to the parameters of the script or function. N is a number that indicates the number of parameters. For example, the first parameter is
two. $# the number of parameters passed to the script or function. $* all parameters passed to the script or function. All parameters passed to the script or function by $@. When enclosed in double quotation marks (""), it is slightly different from $*, which will be discussed below. $? The exit status of the previous command, or the return value of the function. $the current Shell process ID. For Shell scripts, it is the process ID where these scripts are located. String single quotation mark and double quote
Strings can be in single quotation marks, double quotation marks or without quotation marks.
(1) restrictions on single quote strings any characters in single quotation marks will be output as is, and the variables in the single quote string are invalid.
A single quotation mark cannot appear in a single quote string (not even after using escape characters for single quotation marks), but it can appear in pairs and can be used as a string concatenation.
(2) the advantages of double quotes there can be variables in double quotes.
Escape characters can appear in double quotation marks
Your_name= "jb51" # concatenates greeting= "hello,"$your_name"! "greeting_1=" hello, ${your_name}! "echo $greeting $greeting_1# uses single quotes to concatenate greeting_2='hello,'$your_name'! 'greeting_3='hello, ${your_name}!' echo $greeting_2 $greeting_3hello, runoob! Hello, runoob! hello, runoob! Hello, ${your_name}! String length string= "abcd" echo ${# string} # output 4 extract substring
The following example intercepts 4 characters from the second character of the string
String= "jb51.net is a great site" echo ${string:1:4} # output b51. Look for substrings
Find the position of the character I or o (calculate which letter appears first):
String= "jb51 is a great site" echo `expr index "$string" i5` # output 3
Note: in the above script, `is a backquote, not a single quote'.
The string enclosed in backquotes is interpreted by shell as a command line
When executed, shell first executes the command line and replaces the entire backquote (including two backquotes) with its standard output.
OPDATE= `date`
Is to assign the result of the command date-d'- 1 day' +% Y%m%d to the variable OPDATE.
In addition, backquotes are an old usage, while $() is the new one, for example
OPDATE=$ (date) comments single line comment
Lines that begin with # are comments and are ignored by the interpreter.
Set multiline comments by adding a # sign to each line
#-# this is a comment # author:xxx#----# user configuration area starts # # here you can add script description information # # end of user configuration area # multiline comments: define an array
In Shell, arrays are represented by parentheses, and array elements are separated by a "space" symbol
Array name = (value 1, value 2. Value n)
For example:
Array_name= (value0 value1 value2 value3)
Or
Array_name= (value0value1value2value3)
You can also define individual components of an array:
Array_name [0] = value0array_name [1] = value1array_ name [n] = valuen
There can be no use of consecutive subscripts, and there is no limit to the range of subscripts.
Read array
The general format for reading array element values is
${Array name [subscript]}
For example:
Valuen=$ {array_ name[n]}
Use the @ symbol to get all the elements in the array, such as:
Echo ${array_name [@]} get the number of array elements length=$ {# array_name [@]}
Or
Length=$ {# array_name [*]}
Gets the length of a single element in the array
Lengthn=$ {# array_ name [n]} function No argument call #! / bin/bashdemoFun () {echo "this is my first shell function!"} echo "- function starts execution -" demoFunecho "- function execution completes -" Plain Text- function starts execution-this is my first shell function!-function execution completed -return the value #! / bin/bashfunWithReturn () {echo "this function adds the two digits entered." Echo "enter the first number:" read aNum echo "enter the second number:" read anotherNum echo "two digits are $aNum and $anotherNum!" Return $(($aNum+$anotherNum))} funWithReturnecho "the sum of the two numbers entered is $?!" This function adds two input numbers. Enter the first number: 1 enter the second number: 2 the two digits are 1 and 2 respectively! The sum of the two numbers entered is 3! Call #! / bin/bashfunWithParam () {echo "the first argument is $1!" Echo "the second parameter is $2!" Echo "the tenth parameter is $10!" Echo "the tenth parameter is ${10}!" Echo "the eleventh parameter is ${11}!" Echo "Total number of parameters is $#!" Echo "outputs all parameters as a string $*!"} funWithParam 1 2 34 5 6 7 8 9 34 73 the first parameter is 1! The second parameter is 2! The tenth parameter is 10! The tenth parameter is 34! The eleventh parameter is 73! There are 11 parameters in total! Output all parameters 1 2 34 5 6 7 8 9 34 73 as a string! This is the end of the introduction of "how to use variables, strings, arrays and functions in shell programming". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.