您的位置:首页 > 运维架构 > Shell

shell中#的几种用法

2011-12-22 23:49 239 查看
关于shell脚本中#的几种用法:

1.#! 文件头中指定命令解释器;

2.# 作为注释符;

3.$# 、 ${#@} 、${#*}命令行参数的个数;

4.${#array[@]}表示数组的长度、$#a 表示变量a的长度(包括空格);

5.${2#10011}表示2进制数值,该值表示十进制的19

6.剥去传递来的参数中的某个pattern的字符 gerenal format is : ${1#abc} 如果此时命令行参数为sh test.sh abcdef ,echo ${1#abcdef} //def; (只能剥去一个符合条件的串);

7. 剥去符合条件的最大个数的串

shopt -s extglog

echo ${1##+(abc)}

shopt -u extglog

比如输入 sh test.sh abcabcdef //def

example:

#! /bin/bash

#this is an example and this sentence is comment.

if[ $# -ne 1 ]

then

echo "there is no paraments!"

exit 0

fi

a="abcabc efg"

echo "$a"

echo "${1#abc}"

string_getHead ()

{

shopt -s extglog

local var="${1##+(abc)}"

echo $var

shopt -u extglog

_string_getHead=${var:abc}

}

string_getHead()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: