Shell script Programming--string variable handling

Source: Internet
Author: User
Tags first string

I believe that the students running on the path of the script, can feel that the script needs to apply variables in a very wide area, simply to see the variable is simply: var=value; if we go deep into it, we can also find that the knowledge of variables is actually very deep, Let's share with you today how much knowledge there is in this little variable. Shell variables are generally untyped, but the bash shell provides declare and typeset two commands to specify the type of the variable, and two commands are completely equivalent.

I. Advanced variable usage--defining a type variable
Declare [options] variable name
-r setting variable to read-only property is equivalent to using export var_name
-I defines a variable as an integral number
-a defines a variable as an array
-F Displays all function names and their contents defined before this script
-F displays only all function names defined before this script
-X declares a variable as an environment variable
-l convert variable value to lowercase
-u convert variable value to uppercase

Two, string slices:
1, ${#var}: Returns the length of the string variable var
Example code:
[Email protected] array]# vara=test12345
[[email protected] array]# echo ${vara}
test12345
[Email protected] array]# echo ${#vara}
9

2, ${var:offset}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, to the last part, the value of offset is between 0 and ${#var}-1
(negative value after bash4.2)
Example code:
[Email protected] array]# vara=test12345
[[email protected] array]# echo ${vara:4}
12345

3, ${var:offset:number}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, the part of the length number
Example code:
[Email protected] array]# vara=test12345
[[email protected] array]# echo ${vara:3:4}
T123

4, ${var:-lengh}: Take the rightmost character of the string
Note: You must have a blank character after a colon
Example code:
[Email protected] array]# vara=test12345
[[email protected] array]# echo ${vara:-3}
345
[[email protected] array]# echo ${vara:-5}
12345

Three, string processing
(i) substring based on pattern:
1, ${var#*word}: From left to right, find the var variable stored in the string, the first occurrence of Word, delete the string beginning to the first occurrence of all characters between word characters
Example code:
[Email protected] array]# vara=test12345
[[email protected] array]# echo ${vara#*12}
345
[[email protected] array]# echo ${vara#*es}
t12345

2, ${var##*word}: Ibid, different is, the deletion is the beginning of the string to the last character specified by word all the content
Example code:
[Email protected] array]# file=/var/log/messages
[[email protected] array]# echo ${file##*/}
Messages

3, ${var%word*}: From right to left, find the var variable stored in the string, the first occurrence of word, delete the last character of the string left to the first occurrence of all characters between word characters
Example code:
[Email protected] array]# file=/var/log/messages
[[email protected] array]# echo ${file%/*}
/var/log

4, ${var%%word*}: Ditto, except delete the rightmost character of the string to the left to the last occurrence of all characters between word characters
Example code:
[Email protected] array]# url=http://www.baidu.com:80
[Email protected] array]# echo ${url##*:}
80
[Email protected] array]# echo ${url%%:*}
http

(ii) Find and replace:
1, ${var/pattern/substi}: Find the string represented by Var, the first string to be matched to by pattern, and replace it with Substi
Example code:
[Email protected] array]# vara=test12345test
[[email protected] array]# echo ${vara/es/00}
T00t12345test

2, ${var//pattern/substi}: Find the string represented by Var, all the strings that can be matched to by pattern, and replace them with Substi
Example code:
[Email protected] array]# vara=test12345test
[[email protected] array]# echo ${vara/es/00}
T00t12345test
[[email protected] array]# echo ${vara//es/00}
t00t12345t00t

3, ${var/#pattern/substi}: Find the String that var represents, the string to which the beginning of the line is matched by pattern, and replace it with Substi.
Example code:
[Email protected] array]# vara=test12345test
[[email protected] array]# echo ${vara/#te/00}
00st12345test

4, ${var/%pattern/substi}: Find the string represented by Var, the string that the end of the line is matched to by pattern, and replace it with Substi
Example code:
[Email protected] array]# vara=test12345test
[[email protected] array]# echo ${vara/#te/00}
00st12345test
[[email protected] array]# echo ${vara/%st/000}
test12345te000

(iii) Find and delete:
1, ${var/pattern}: Find the string represented by Var, delete the first string matched by pattern
Example code:
[Email protected] array]# vara=test12345test
[[email protected] array]# echo ${vara/12345}
Testtest

2, ${var//pattern}: Find the string represented by Var, delete all strings that are matched by pattern
Example code:
[Email protected] array]# vara=test12345test
[[email protected] array]# echo ${vara//t}
Es12345es
[Email protected] array]# file=/var/log/messages
[[email protected] array]# echo ${file///}
Varlogmessages

3, ${var/#pattern}: Find the string represented by Var, delete the string to which the beginning of the line is matched by pattern
Example code:
[Email protected] array]# vara=test12345test
[Email protected] array]# echo ${vara/#test}
12345test
[Email protected] array]# file=/var/log/messages
[[email protected] array]# echo ${file/#\/var}
/log/messages

4, ${var/%pattern}: Find the string represented by Var, delete the string to which the end of the line is matched by pattern
Example code:
[Email protected] array]# file=/var/log/messages
[[email protected] array]# echo ${file/%ges}
/var/log/messa
[Email protected] array]# vara=test12345test
[[email protected] array]# echo ${vara/%test}
test12345

(d) Character case conversion:
1. ${var^^}: Convert all lowercase letters in VAR to uppercase
Example code:
[Email protected] array]# vara=test
[[email protected] array]# echo ${vara^^}
TEST

2, ${var,}: Converts all uppercase letters in VAR to lowercase
Example code:
[Email protected] array]# Vara=wangjun
[Email protected] array]# echo ${vara,,}
Wangjun

Iv. High-level method of variable assignment
1. ${var:-value}: Returns value if Var is empty or not set, otherwise returns VAR
Example code:
[[email protected] array]# echo ${varb:-100}
100

2. ${var:+value}: Returns value if Var is not empty
Example code:
[Email protected] array]# Varb=wangjun
[[email protected] array]# echo ${varb:+100}
100

3, ${var:=value}: If Var is empty or not set, then return value and assign value to Var; otherwise, the value of VAR is returned
Example code:
[Email protected] array]# Varb=wangjun
[[email protected] array]# echo ${varb:=100}
Wangjun
[[email protected] array]# echo ${varc:=100}
100

4, ${var:?error_info}: Returns Error_info if Var is empty or not set, otherwise returns the value of Var
Example code:
[Email protected] array]# Varb=wangjun
[[email protected] array]# echo ${varb:?4}
Wangjun
[[email protected] array]# echo ${vard:?4}
-bash:vard:4

Assigning a value to a script variable using a configuration file
(1) Define a text file, each line defines "Name=value"
(2) Source This file in the script to

V. Indirect variable References
If the value of the first variable is the name of the second variable, referring to the value of the second variable from the first variable is called an indirect variable reference
Var1=var2
Var2=value
The value of the var1 is var2, and VAR2 is the variable name, and the value of VAR2 is values, and the indirect variable reference refers to the behavior of getting the value of the variable by var1

The bash shell provides two formats for implementing indirect variable references
Eval var3=\$ $var 1
VAR3=${!VAR1}

Example code:

[Email protected] useradmin]# Var1=user
[Email protected] useradmin]# user=test
[Email protected] useradmin]# echo $var 1
User
[Email protected] useradmin]# eval var3=\$ $var 1
[Email protected] useradmin]# echo $var 3
Test

[[email protected] useradmin]# VAR4=${!VAR1}
[Email protected] useradmin]# echo $var 4
Test

eval Command
The eval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for a time that does not function. This command scans the variable two times
Example code:
[Email protected] useradmin]# Prefix=user
[Email protected] useradmin]# $num
Bash:005:command not found ...
[Email protected] useradmin]# num=005
[Email protected] useradmin]# eval "Echo $prefix {001: $num}"
user001 user002 user003 user004 user005


Students who are learning scripts, when you see here at the same time, have any idea, believe that once the confusion was opened, at least when I learned here myself, suddenly enlightened.

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1841659

Shell script Programming--string variable handling

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.