Practice and production practice of shell variable substitution technology

Source: Internet
Author: User

${value:-word}

If the variable name exists and is not NULL, the value of the variable is returned, otherwise the word string is returned.

${value:=word}

If the variable name exists and is not NULL, the value of the variable is returned, otherwise, the value of this variable is word and its value is returned.

${value:?message}

If the variable name exists and is not NULL, the value of the variable is returned, otherwise the variable name is displayed, and the current command or shell is exited.

Example: ${value:? " Notdefined "} If value is undefined, the-bash:value:not defined is displayed and exited.

${value:+word}

If the variable name exists and is not NULL, Word is returned, otherwise null is returned.

Example: ${value:+word} if value is already defined, return Word (that is, true)

1, ${value:-word}

When the variable is undefined or the value is empty, the return value is the word content, otherwise the value of the variable is returned

[Email protected] ~]# Result=${cmz:-unset}#当cmz没有定义合作和是空会返回UNSET
[Email protected] ~]# echo $result
UNSET
[Email protected] ~]# echo $CMZ

==> here is empty.

Conclusion: When CMZ has no content, it returns to unset.

[Email protected] ~]# cmz= "Caimengzhi"
[Email protected] ~]# Result=${cmz:-unset} #之前必须声明cmz变量
[Email protected] ~]# echo $result
Caimengzhi
Conclusion: When CMZ has content, it returns the contents of CMZ variables.

This variable function solves the work string problem, and the variable is undefined.

2, ${value:=word}

[[email protected] ~]# Result=${test:=unset}
[Email protected] ~]# echo $result
UNSET

[Email protected] ~]# echo $test
UNSET

3, ${value:? " Not defined "}

[Email protected] ~]# RESULT=${LMT:? Not defined}
-bash:lmt:not defined

4, ${value-word} remove Colon

That is, when httpd does not exist, it assigns-the/usr/sbin/httpd of the back to the httpd in front of the equal sign.

To manipulate the path of a variable, it is best to first determine whether the path is non-null, especially the delete operation.

Path=/server/backup

Find ${path:=/tmp/}-name "*.tar.gz"-type F | Xargs rrm-f

RM-RF ${path}

It's important.

[Email protected] shell_scrpit]# vim d.sh
[Email protected] shell_scrpit]# sh-x d.sh
+ find/tmp/-name ' *.tar.gz '-type f
+ Xargs Rrm-f
Xargs:rrm:No such file or directory
+ RM-RF
[email protected] shell_scrpit]# cat d.sh
#path =/server/backup

Find ${path:=/tmp/}-name "*.tar.gz"-type F | Xargs rrm-f

#此时path没有定义 path will become/tmp/

RM-RF ${path}


Practice and production practice of shell variable substitution technology

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.