免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1606 | 回复: 2
打印 上一主题 下一主题

Shell 参数扩展 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-06 18:14 |只看该作者 |倒序浏览
#!/bin/sh

# {x..y[incr]},
# ~
echo a{a,c,b}e

echo ~

echo ~/foo

echo ~root/foo

echo ~+/foo

echo ~-/foo

# ${parameter:-word}

unset x;
y="abc def";
echo "/${x:-'XYZ'}/${y:-'XYZ'}/$x/$y/"

# ${parameter:=word}
unset x;
y="abc def";
echo "/${x:='XYZ'}/${y:='XYZ'}/$x/$y/"

# ${parameterword}
( unset x;
  y="abc def";
  echo "/${x'XYZ'}/${y'XYZ'}/$x/$y/" )>>so.txt 2>se.txt

# ${parameter:+word}
unset x;
y="abc def";
echo "/${x:+'XYZ'}/${y:+'XYZ'}/$x/$y/"

# ${parameterffset}
# ${parameterffset:length}
x="some value"
echo "${x:3}"
echo "${x:3:5}"

# ${!prefix*}
# ${!prefix@}

zhang=Zhang
firstname=zhang
firstx=xxxxx
firsty=yyyyy
echo ${firstname}
echo ${!firstname}
echo ${!first*}
echo ${!first@}
echo "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
# ${!name[@]}
# ${!name
  • }
    # bash 3.0 support ??

    array=(a b c def)
    echo ${array}
    echo ${array
  • }
    echo ${array[@]}
    echo ${!array
  • }
    echo ${!array[@]}

    # ${#parameter}

    firstname=zhang
    echo ${#firstname}
    echo ${#array[@]}
    echo ${#array
  • }

    # ${parameter#word}
    # ${parameter##word}

    x="a1 b1 c2 d2"
    echo ${x#*1}
    echo ${x##*1}

    # ${parameter%word}
    # ${parameter%%word}
    echo ${x%1*}
    echo ${x%%1*}

    # ${parameter/pattern/string}
    # ${parameter//pattern/string}
    echo ${x/1/3}
    echo ${x//1/3}

    # ${parameter^pattern}
    # ${parameter^^pattern}
    # ${parameter,pattern}
    # ${parameter,,pattern}

    是如何用的?
  • 论坛徽章:
    0
    2 [报告]
    发表于 2010-01-06 18:24 |只看该作者
    LZ的脚本貌似是一个学习shell如果处理字符串的测试脚本?

    论坛徽章:
    0
    3 [报告]
    发表于 2010-01-07 09:13 |只看该作者
    ${parameter^pattern}
    ${parameter^^pattern}
    ${parameter,pattern}
    ${parameter,,pattern}
    This expansion modifies the case of alphabetic characters in parameter. The pattern is expanded to produce a pattern just as in pathname expansion. The ‘^’ operator converts lowercase letters matching pattern to uppercase; the ‘,’ operator converts matching uppercase letters to lowercase. The ‘^^’ and ‘,,’ expansions convert each matched character in the expanded value; the ‘^’ and ‘,’ expansions match and convert only the first character in the expanded value. If pattern is omitted, it is treated like a ‘?’, which matches every character. If parameter is ‘@’ or ‘*’, the case modification operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the case modification operation is applied to each member of the array in turn, and the expansion is the resultant list.

    这个方式不知道是怎么用的
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP