1.说明 ${parameter-default} --------如果变量parameter没被声明,那么就使用默认值
${parameter:-default} -------如果变量parameter没被设置,那么就使用默认值
filename=${1:-$DEFAULT_FILENAME}---------如果参数1没有设置,那么就取默认的DEFAULT_FILENAME
${parameter=default} ----如果变量parameter没声明,那么就把它的值设为default.
${parameter:=default} -----如果变量parameter没设置,那么就把它的值设为default.
${parameter+alt_value} ----如果变量parameter被声明,那么就是用alt_value,否则使用null字符串.
${parameter:+alt_value} -----如果变量parameter被设置,那么就是用alt_value,否则使用null字符串.
${parameter?err_msg} -----如果parameter已经被声明,那么就使用设置的值,否则打印err_msg错误消息.
${parameter:?err_msg} ----如果parameter已经被设置,那么就使用设置的值,否则打印err_msg错误消息.
冒号在linux中的使用
1、注释
: this is comment
: ' this is a multiple
lines comment.'
将注释作为参数传递给冒号命令, 因为没有输出所以可以当作注释阿,够巧妙吧。
2、清空文件
: > file
2.使用参数替换和错误消息
#! /bin/sh
: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}
echo
echo "Name of the machine is $HOSTNAME."
echo "You are $USER."
echo "Your home directory is $HOME."
echo "Your mail INBOX is located in $MAIL."
echo
echo "If you are reading this message,"
echo "critical environmental variables have been set."
echo
echo
ThisVariable=Value-of-ThisVariable
: ${ThisVariable?}
echo "Value of ThisVariable is $ThisVariable."
echo
echo
: ${ZZXy23AB?"ZZXy23AB has not been set."}
echo "You will not seen this message,because script already terminated."
HERE=0
exit $HERE
3.变量长度
${#array[*]}和${#array[@]}表示数组中元素的个数
#! /bin/sh
E_NO_ARGS=65
if [ $# -eq 0 ]
then
echo "Please invoke this script with one or two command-line arguments."
exit $E_NO_ARGS
fi
var01=abcdEFGH28ij
echo "var01 = ${var01}"
echo "Length of var01 = ${#var01}"
var02="abcd EFGH28ij"
echo "var01 = ${var02}"
echo "Length of var02 = ${#var02}"
echo "Number of command-line arguments passed to script = ${#@}" -----参数个数
echo "Number of command-line arguments passed to script = ${#*}" -----参数个数
exit 0
4.参数替换中的模式匹配
#! /bin/sh
var1=abcd12345abc6789
pattern1=a*c
echo
echo "var1 = $var1"
echo "var1 = ${var1}"
echo "Number of characters in ${var1} = ${#var1}"
echo
echo "pattern1 = $pattern1"
echo "---------1--------"
echo '${var1#$pattern1} = ' "${var1#$pattern1}"
echo '${var##$pattern1} = ' "${var1##$pattern1}"
echo;echo;echo
pattern2=b*9
echo "var1 = $var1"
echo
echo "pattern2 = $pattern2"
echo "-------------------"
echo '${var1%pattern2} = ' "${var1%$pattern2}"
echo '${var1%%pattern2} = ' "${var1%%$pattern2}"
echo
exit 0
5.修改文件扩展名
#! /bin/sh
E_BADARGS=65
case $# in
0|1)
echo "Usage: `basename $0` old file suffix new file suffix"
exit $E_BADARGS
;;
esac
for filename in *.$1
do
mv $filename ${filename%$1}$2
done
exit 0
6.用模式匹配来解析任意字符串
#! /bin/sh
var1=abcd-1234-defg
echo "var1 = $var1"
t=${var1#*-*}
echo "var1 (with everything,up to and including first - strippedout) = $t"
t=${var1##*-*}
echo "If var1 contains a \"-\",returns empty string... var1=$t"
t=${var1%*-*}
echo "var1 (with everything from the last - on stripped out) = $t"
echo
path_name=/home/bozo/ideas/thoughts.for.today
echo "path_name=$path_name"
t=${path_name##/*/} ------截去最大匹配/*/
echo "path_name,stripped of prefixes = $t"
t=${path_name%/*.*} ------从尾部截去/*.*
echo "path_name,stripped of suffixes = $t"
echo
t=${path_name:11}
echo "$path_name,with first 11 chars stripped of = $t"
t=${path_name:11:5}
echo "$path_name,with first 11 chars stripped off,length 5 = $t"
echo
t=${path_name/bozo/clown}
echo "$path_name with \"bozo\" replaced by \"clown\" = $t"
t=${path_name/today/}
echo "$path_name with \"today\" delete = $t"
t=${path_name//o/O}
echo "$path_name with all o's capitalized = $t"
t=${path_name//o/}
echo "$path_name with all o's deleted = $t"
exit 0
7.对字符串的前缀和后缀使用匹配模式
${var/#pattern/replacement}----如果变量var的前缀匹配pattern,那么就是用replacement来替换匹配到的pattern字符串
${var/%pattern/replacement}----如果变量var的后缀匹配pattern,那么就是用replacement来替换匹配到的pattern字符串
#! /bin/bash
v0=abc1234zip1234abc
echo "v0 = $v0"
echo
v1=${v0/#abc/ABCDEF}
echo "v1 = $v1"
v2=${v0/%abc/ABCDEF}
echo "v2 = $v2"
echo
v3=${v0/#123/100}
echo "v3 = $v3"
v4=${v0/%123/100}
echo "v4 = $v4"
exit 0
${parameter:-default} -------如果变量parameter没被设置,那么就使用默认值
filename=${1:-$DEFAULT_FILENAME}---------如果参数1没有设置,那么就取默认的DEFAULT_FILENAME
${parameter=default} ----如果变量parameter没声明,那么就把它的值设为default.
${parameter:=default} -----如果变量parameter没设置,那么就把它的值设为default.
${parameter+alt_value} ----如果变量parameter被声明,那么就是用alt_value,否则使用null字符串.
${parameter:+alt_value} -----如果变量parameter被设置,那么就是用alt_value,否则使用null字符串.
${parameter?err_msg} -----如果parameter已经被声明,那么就使用设置的值,否则打印err_msg错误消息.
${parameter:?err_msg} ----如果parameter已经被设置,那么就使用设置的值,否则打印err_msg错误消息.
冒号在linux中的使用
1、注释
: this is comment
: ' this is a multiple
lines comment.'
将注释作为参数传递给冒号命令, 因为没有输出所以可以当作注释阿,够巧妙吧。
2、清空文件
: > file
2.使用参数替换和错误消息
#! /bin/sh
: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}
echo
echo "Name of the machine is $HOSTNAME."
echo "You are $USER."
echo "Your home directory is $HOME."
echo "Your mail INBOX is located in $MAIL."
echo
echo "If you are reading this message,"
echo "critical environmental variables have been set."
echo
echo
ThisVariable=Value-of-ThisVariable
: ${ThisVariable?}
echo "Value of ThisVariable is $ThisVariable."
echo
echo
: ${ZZXy23AB?"ZZXy23AB has not been set."}
echo "You will not seen this message,because script already terminated."
HERE=0
exit $HERE
3.变量长度
${#array[*]}和${#array[@]}表示数组中元素的个数
#! /bin/sh
E_NO_ARGS=65
if [ $# -eq 0 ]
then
echo "Please invoke this script with one or two command-line arguments."
exit $E_NO_ARGS
fi
var01=abcdEFGH28ij
echo "var01 = ${var01}"
echo "Length of var01 = ${#var01}"
var02="abcd EFGH28ij"
echo "var01 = ${var02}"
echo "Length of var02 = ${#var02}"
echo "Number of command-line arguments passed to script = ${#@}" -----参数个数
echo "Number of command-line arguments passed to script = ${#*}" -----参数个数
exit 0
4.参数替换中的模式匹配
#! /bin/sh
var1=abcd12345abc6789
pattern1=a*c
echo
echo "var1 = $var1"
echo "var1 = ${var1}"
echo "Number of characters in ${var1} = ${#var1}"
echo
echo "pattern1 = $pattern1"
echo "---------1--------"
echo '${var1#$pattern1} = ' "${var1#$pattern1}"
echo '${var##$pattern1} = ' "${var1##$pattern1}"
echo;echo;echo
pattern2=b*9
echo "var1 = $var1"
echo
echo "pattern2 = $pattern2"
echo "-------------------"
echo '${var1%pattern2} = ' "${var1%$pattern2}"
echo '${var1%%pattern2} = ' "${var1%%$pattern2}"
echo
exit 0
5.修改文件扩展名
#! /bin/sh
E_BADARGS=65
case $# in
0|1)
echo "Usage: `basename $0` old file suffix new file suffix"
exit $E_BADARGS
;;
esac
for filename in *.$1
do
mv $filename ${filename%$1}$2
done
exit 0
6.用模式匹配来解析任意字符串
#! /bin/sh
var1=abcd-1234-defg
echo "var1 = $var1"
t=${var1#*-*}
echo "var1 (with everything,up to and including first - strippedout) = $t"
t=${var1##*-*}
echo "If var1 contains a \"-\",returns empty string... var1=$t"
t=${var1%*-*}
echo "var1 (with everything from the last - on stripped out) = $t"
echo
path_name=/home/bozo/ideas/thoughts.for.today
echo "path_name=$path_name"
t=${path_name##/*/} ------截去最大匹配/*/
echo "path_name,stripped of prefixes = $t"
t=${path_name%/*.*} ------从尾部截去/*.*
echo "path_name,stripped of suffixes = $t"
echo
t=${path_name:11}
echo "$path_name,with first 11 chars stripped of = $t"
t=${path_name:11:5}
echo "$path_name,with first 11 chars stripped off,length 5 = $t"
echo
t=${path_name/bozo/clown}
echo "$path_name with \"bozo\" replaced by \"clown\" = $t"
t=${path_name/today/}
echo "$path_name with \"today\" delete = $t"
t=${path_name//o/O}
echo "$path_name with all o's capitalized = $t"
t=${path_name//o/}
echo "$path_name with all o's deleted = $t"
exit 0
7.对字符串的前缀和后缀使用匹配模式
${var/#pattern/replacement}----如果变量var的前缀匹配pattern,那么就是用replacement来替换匹配到的pattern字符串
${var/%pattern/replacement}----如果变量var的后缀匹配pattern,那么就是用replacement来替换匹配到的pattern字符串
#! /bin/bash
v0=abc1234zip1234abc
echo "v0 = $v0"
echo
v1=${v0/#abc/ABCDEF}
echo "v1 = $v1"
v2=${v0/%abc/ABCDEF}
echo "v2 = $v2"
echo
v3=${v0/#123/100}
echo "v3 = $v3"
v4=${v0/%123/100}
echo "v4 = $v4"
exit 0