Practice of shell Special extended variables

Source: Internet
Author: User

Bo main name: Li Changming

Blog Address: http://keep88.blog.51cto.com


This note is from the old boy books: Learn Linux OPS Shell programming with old boys

Special extension variables

1. ${parameter:-word} Functional Practice

The role of ${parameter:-word} is to return the value of the word string substitution variable if the value of the parameter variable is empty or unassigned

For example:

[[email protected] ~]# echo $test #>== This time the test variable is not assigned [[email protected] ~]# echo ${test:-word} #>== can see the test The variable is not assigned a value, output word, indicates that the test variable is empty, and returns the character defined after "-", but be aware that Word will not be assigned to the test variable, just an identity. Word[[email protected] ~]# test= "ABC" #>== assign the test variable ABC to view the output [[email protected] ~]# echo $testabc [[Email protecte D] ~]# Echo ${test:-word} #>== This time the test variable is assigned, so the value of the test variable is output. Abc[[email protected] ~]#

Comments:

The colon ":" in ${parameter:-word} can be omitted. No difference from the above results


2. ${parameter:=word} Functional Practice:

Determine if the parameter variable has a value, if there is a value output variable value, if not assigned, then "-" after the character word (custom) assigned to the parameter variable

For example:

[[email protected] ~]# unset test[[email protected] ~]# echo $test #>== test variable not assigned [[email protected] ~]# A=${test:=wor D}[[email protected] ~]# echo $A #>== unassigned, assign Word to the variable Testword if the test variable has an assignment, output the variable of test directly [[email protected] ~]# test=  "5678" [[email protected] ~]# echo $test 5678[[email protected] ~]# b=${test:=word}[[email protected] ~]# echo $B #>== The test variable has an assignment that outputs the value directly and does not assign word to $test5678


The difference between the above two special variables:

${parameter:-word}: If parameter has an assignment, output value, no assignment, output "-" after the defined character, will not be assigned to the variable parameter, just display information

${parameter:=word}: If parameter has an assignment, output value, no assignment, the character defined after "=", assigns a value to the variable parameter

3. ${parameter:?word} Functional Practice:

If the parameter variable is not assigned a value, the error message is "?". After the defined character, if the value is assigned, the output value is directly

For example:

[[email protected] ~]# unset test #>== Cancel assignment of test variable [[email protected] ~]# echo ${test:? is not Value}-bash:test:is Not value #>== can be seen, not assigned to the case, output? Post-defined error message

What if there is a value assigned? View results:

[[email protected] ~]# test= "ABCDEFG" #>== assign value to test variable [[email protected] ~]# echo $TESTABCDEFG [[email protected] ~]# echo ${test:? Test is not having VALUE}ABCDEFG #>== test variable has value, direct output value


4. ${parameter:+word} Functional Practice

If the parameter is not assigned, the output is empty, and if the parameter variable has an assignment, the output + post-defined information is not assigned to the parameter variable


For example:

[Email protected] ~]# unset test[[email protected] ~]# echo $test [[email protected] ~]# echo ${test:+word} #>== The test variable is assigned, the output is empty [[email protected] ~]# Test=aaaaaaa[[email protected] ~]# echo ${test:+word} #>== output Word, indicating that the test variable has an assignment, However, word is not assigned to test, only output information that prompts Word[[email protected] ~]# echo $test #>== test variable or originally defined value AAAAAAA


Production Case:

Delete outdated data backups from 7 days ago

If you forget to define the path variable, and you do not want the path value to be null, you can define the return value of/tmp instead of the path null value.

For example:

[[email protected] ~]# cat delete.sh find ${path-/tmp}-name *.tar.gz-type f-mtime +7 | Xargs rm-f[[email protected] ~]# sh-x delete.sh + xargs rm-f+ find/tmp-name ' *.tar.gz '-type f-mtime +7 If the path variable is not set, Empty, the following content is returned: [[email protected] ~]# cat delete.sh Find ${path}-name *.tar.gz-type f-mtime +7 | Xargs rm-f[[email protected] ~]# sh-x delete.sh + xargs rm-f+ find-name ' *.tar.gz '-type f-mtime +7 #>== Execute the script at this time, You'll get an error.



This article is from the "Keep Keep your" blog, be sure to keep this source http://keep88.blog.51cto.com/11829099/1932538

Practice for Shell-specific extended variables

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.