成于坚持,败于止步
处理和(或)扩展变量
${parameter}与$parameter相同, 也就是变量parameter的值. 在某些上下文中, ${parameter}很少会产生混淆,可以把变量和字符串组合起来使用.
#!/bin/bash
YOUR_ID=${USER}-on-${HOSTNAME}
echo "$YOUR_ID"
echo "Old \$PATH = ${PATH}"
PATH=${PATH}:/opt/bin
echo "New \$PATH = ${PATH}"
exit 0
结果:
root@ubuntu:~/resource/shell-study/0509-2013# ./test1.sh
root-on-ubuntu
Old $PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/jvm/java/jdk1.7.0_13/bin:/opt/FriendlyARM/toolschain/4.4.3/bin
New $PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/jvm/java/jdk1.7.0_13/bin:/opt/FriendlyARM/toolschain/4.4.3/bin:/opt/bin
root@ubuntu:~/resource/shell-study/0509-2013#
${parameter-default}, ${parameter:-default}
${parameter-default} -- 如果变量parameter没被声明, 那么就使用默认值.
${parameter:-default} -- 如果变量parameter没被设置, 那么就使用默认值.#!/bin/bash
echo ${username-`whoami`}
username=
echo ${username-`whoami`}
echo ${username:-`whoami`}
username=zhangsan
echo ${username-`whoami`}
echo ${username:-`whoami`}
exit 0
结果:
root@ubuntu:~/resource/shell-study/0509-2013# ./test2.sh
root
root
zhangsan
zhangsan
root@ubuntu:~/resource/shell-study/0509-2013#
第一行输出root因为username未定义,第二行输出空,因为username定义只是未设置,第三行输出root,因为username未定义,第四行,第五行,username定义并设置,正常输出,${parameter-default} 和${parameter:-default}在绝大多数的情况下都是相同的. 只有在parameter已经被声明, 但是被赋null值得时候, 这个额外的:才会产生不同的结果.
${parameter=default}, ${parameter:=default}
${parameter=default} -- 如果变量parameter没声明, 那么就把它的值设为default.
${parameter:=default} -- 如果变量parameter没设置, 那么就把它的值设为default.
这两种形式基本上是一样的. 只有在变量$parameter被声明并且被设置为null值的时候, :才会引起这两种形式的不同,他的使用方法跟上面${parameter-default}, ${parameter:-default}基本没有什么区别
#!/bin/bash
echo ${username=`whoami`}
username=
echo ${username=`whoami`}
echo ${username:=`whoami`}
username=zhangsan
echo ${username=`whoami`}
echo ${username:=`whoami`}
exit 0
结果是一模一样的:
root@ubuntu:~/resource/shell-study/0509-2013# ./test2.sh
root
root
zhangsan
zhangsan
root@ubuntu:~/resource/shell-study/0509-2013#
${parameter+alt_value}, ${parameter:+alt_value}
${parameter+alt_value} -- 如果变量parameter被声明了, 那么就使用alt_value, 否则就使用null字符串.
${parameter:+alt_value} -- 如果变量parameter被设置了, 那么就使用alt_value, 否则就使用null字符串.
这两种形式绝大多数情况下都一样. 只有在parameter被声明并且设置为null值的时候, 多出来的这个:才会引起这两种形式的不同,和上面的方法正好是相反的过程, 具体请看下边的例子.
#!/bin/bash
echo ${username+`whoami`}
username=
echo ${username+`whoami`}
echo ${username:+`whoami`}
username=zhangsan
echo ${username+`whoami`}
echo ${username:+`whoami`}
exit 0
还是比较好理解的,结果:
root@ubuntu:~/resource/shell-study/0509-2013# ./test3.sh
root
root
root
root@ubuntu:~/resource/shell-study/0509-2013#
${parameter?err_msg}, ${parameter:?err_msg}
${parameter?err_msg} -- 如果parameter已经被声明, 那么就使用设置的值, 否则打印err_msg错误消息.
${parameter:?err_msg} -- 如果parameter已经被设置, 那么就使用设置的值, 否则打印err_msg错误消息.
这两种形式绝大多数情况都是一样的. 和上边所讲的情况一样, 只有在parameter被声明并设置为null值的时候, 多出来的:才会引起这两种形式的不同.
root@ubuntu:~/resource/shell-study/0509-2013# unset username
root@ubuntu:~/resource/shell-study/0509-2013# echo ${username?"error"}
bash: username: error
root@ubuntu:~/resource/shell-study/0509-2013# username=`whoami`
root@ubuntu:~/resource/shell-study/0509-2013# echo ${username?"error"}
root
root@ubuntu:~/resource/shell-study/0509-2013# username=
root@ubuntu:~/resource/shell-study/0509-2013# echo ${username?"error"}
root@ubuntu:~/resource/shell-study/0509-2013# echo ${username:?"error"}
bash: username: error
root@ubuntu:~/resource/shell-study/0509-2013# username=`whoami`
root@ubuntu:~/resource/shell-study/0509-2013# echo ${username:?"error"}
root
root@ubuntu:~/resource/shell-study/0509-2013#
看一个shell实例:
#!/bin/bash
# 检查一些系统环境变量.
# 这是一种可以做一些预防性保护措施的好习惯.
# 比如, 如果$USER(用户在控制台上中的名字)没有被设置的话,那么系统就会不认你.
: ${HOSTNAME?} ${USER?} ${HOME?}
echo "Name of the machine is $HOSTNAME."
echo "You are $USER."
echo "Your home directory is $HOME."
echo "If you are reading this message,"
echo "critical environmental variables have been set."
echo "------------------------------------------------------"
ThisVariable=Value-of-ThisVariable
: ${ThisVariable?}
echo "Value of ThisVariable is $ThisVariable".
echo "------------------------------------------------------"
: ${ZZXy23AB?"ZZXy23AB has not been set."}
echo "You will not see this message, because script already terminated."
exit 0
结果:
root@ubuntu:~/resource/shell-study/0509-2013# ./test5.sh
Name of the machine is ubuntu.
You are root.
Your home directory is /root.
If you are reading this message,
critical environmental variables have been set.
------------------------------------------------------
Value of ThisVariable is Value-of-ThisVariable.
------------------------------------------------------
./test5.sh: line 17: ZZXy23AB: ZZXy23AB has not been set.
root@ubuntu:~/resource/shell-study/0509-2013#
下面说说一些有关对数组的操作:
${#var}
字符串长度(变量$var得字符个数). 对于array来说, ${#array}表示的是数组中第一个元素的长度.
例外情况:
${#*}和${#@}表示位置参数的个数,对于数组来说, ${#array[*]}和${#array[@]}表示数组中元素的个数.
#!/bin/bash
E_NO_ARGS=65
if [ $# -eq 0 ] # 这个演示脚本必须有命令行参数.
then
echo "Please invoke this script with one or more command-line arguments."
exit $E_NO_ARGS
fi
var01=abcdEFGH28ij
echo "var01 = ${var01}"
echo "Length of var01 = ${#var01}"
# 现在, 让我们试试在变量中嵌入一个空格.
var02="abcd EFGH28ij"
echo "var02 = ${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
结果:
root@ubuntu:~/resource/shell-study/0509-2013# ./test6.sh 1234 23 78
var01 = abcdEFGH28ij
Length of var01 = 12
var02 = abcd EFGH28ij
Length of var02 = 13
Number of command-line arguments passed to script = 3
Number of command-line arguments passed to script = 3
root@ubuntu:~/resource/shell-study/0509-2013#
下面看一个更改文件扩展名的脚本实例:
#!/bin/bash
# rfe.sh: 修改文件扩展名.
# 用法: rfe old_extension new_extension
# 将指定目录中所有的*.gif文件都重命名为*.jpg,
# 用法: rfe gif jpg
E_BADARGS=65
if [ $# -ne 2 ];then
echo "Usage: `basename $0` old_file_suffix new_file_suffix"
exit $E_BADARGS
fi
for filename in *.$1
do
mv $filename ${filename%$1}$2
done
exit 0
结果:
root@ubuntu:~/resource/shell-study/0509-2013/test7# touch 1.jpg 2.jpg 3.txt
root@ubuntu:~/resource/shell-study/0509-2013/test7# ls
1.jpg 2.jpg 3.txt test7.sh
root@ubuntu:~/resource/shell-study/0509-2013/test7# ./test7.sh jpg bmp
root@ubuntu:~/resource/shell-study/0509-2013/test7# ls
1.bmp 2.bmp 3.txt test7.sh
root@ubuntu:~/resource/shell-study/0509-2013/test7#
字符串的处理之后再多花点时间吧,比较细
先到这里了,O(∩_∩)O~
我的专栏地址:http://blog.csdn.net/column/details/shell-daily-study.html
待续。。。。。