linux|linux shell (2) string operation
1.我们希望可以做到下面的事情 1.1.Ensure that variables exist (i.e., are defined and have non-null values)
1.2.Set default values for variables
1.3.Catch errors that result from variables not being set
1.4.Remove portions of variables' values that match patterns
2.字符串的操作 2.1.${varname:-word}
If varname exists and isn't null, return its value;
otherwise return word.
Purpose: Returning a default value if the variable is undefined.
Example: ${count:-0} evaluates to 0 if count is undefined.
2.2.${varname:=word}
If varname exists and isn't null, return its value;
otherwise set it to word and then return its value. Positional and special parameters cannot be assigned this way.
Purpose: Setting a variable to a default value if it is undefined.
Example: ${count:=0} sets count to 0 if it is undefined.
2.3.${varname:?message}
If varname exists and isn't null, return its value;
otherwise print varname: followed by message, and abort the current command or script (non-interactive shells only). Omitting message produces the default message parameter null or not set.
Purpose: Catching errors that result from variables being undefined.
Example: {count:?"undefined!"} prints "count: undefined!" and exits if count is undefined.
2.4.${varname:+word}
If varname exists and isn't null, return word;
otherwise return null.
Purpose: Testing for the existence of a variable.
Example: ${count:+1} returns 1 (which could mean "true") if count is defined.
2.5.${varname:offset:length}
Performs substring expansion.It returns the substring of $varname starting at offset and up to length characters. The first character in $varname is position 0. If length is omitted, the substring starts at offset and continues to the end of $varname. If offset is less than 0 then the position is taken from the end of $varname. If varname is @, the length is the number of positional parameters starting at parameter offset.
Purpose: Returning parts of a string (substrings or slices).
【linux|linux shell (2) string operation】Example: If count is set to frogfootman, ${count:4} returns footman. ${count:4:4} returns foot.
推荐阅读
- Shell-Bash变量与运算符
- Linux下面如何查看tomcat已经使用多少线程
- Beego打包部署到Linux
- Linux|109 个实用 shell 脚本
- jvm关于String
- LeetCode(03)Longest|LeetCode(03)Longest Substring Without Repeating Characters
- linux定时任务contab
- 芯灵思SinlinxA33开发板Linux内核定时器编程
- day16-Linux|day16-Linux 软件管理
- 如何在阿里云linux上部署java项目