红联Linux门户
Linux帮助

linux的一个看不懂的命令 请高手指教

发布时间:2011-04-14 19:22:54来源:红联作者:河魔
l=`expr "$1" : “.*”`
这是什么意思啊
还有他跟 l=`echo $1 | wc -c`
l=` expr $l - 1 `

这两个命令等同吗???
文章评论

共有 11 条评论

  1. Yangfn 于 2011-04-17 10:36:39发表:

    我承认我一点都不懂

  2. nature77 于 2011-04-16 23:24:50发表:

    虽然不是很懂但是还是顶下、、、、

  3. hlinuxer 于 2011-04-16 21:09:15发表:

    算了吧,一点都不懂~~

  4. yanyongkg 于 2011-04-16 12:57:24发表:

    请问楼主在学什么东西?

  5. 河魔 于 2011-04-16 05:55:15发表:

    谢谢大家了 终于懂了{:2_97:}

  6. yanyongkg 于 2011-04-15 10:11:02发表:

    下面这样可能更方更理解

    注:小写字母l全换成大小它母L
    $set abccdd #set可以设置位置变量
    $echo $1 #查看一下位置变量$1
    abccdd #显示如下
    $L=`expr "$1" : ".*"` && echo $L #前面$是提示符,从L开始才是输入哦
    6
    $L=`echo $1 |wc -c` #显示位置变量$1的内容,并把stdout(标准输出)通过管道传给wc计算
    $echo $L
    7 #咦,明明$1的内容是abccdd,长度是6啊,这里怎么是7?请接着看,
    $echo $1 |cat -A
    abccdd$ #原来wc连字串最后的换行字符$也计算进去了?这里我个人这样理解的,欢迎回贴指正

    #计算出的长度比正确的长度总多1,减去1不就可以了?
    $L=`expr $L - 1`
    6

    结论:按顺序执行L=`echo $1 |wc -c` 和L=`expr $L - 1`这两条命令的效果等价于
    L=`expr $1 : ".*"`这一条命令

    上面为什么要按顺序执行呢,你可以试试执行unset L后,再去执行L=`expr $L - 1`会怎么样,
    显然会提示错误,读取倒引号中的变量$L时会找不到$L是多少,先前我还以为L=`expr $1 : ".*"` 和L=`echo $1 |wc -c` 和L=`expr $L - 1`是三种方法,理解错误了!

  7. deepwhite 于 2011-04-15 09:09:11发表:

    type "info expr" in terminal, and the answer will come out to you:

    16.4.1 String expressions
    -------------------------

    `expr' supports pattern matching and other string operators. These
    have higher precedence than both the numeric and relational operators
    (in the next sections).

    `STRING : REGEX'
    Perform pattern matching. The arguments are converted to strings
    and the second is considered to be a (basic, a la GNU `grep')
    regular expression, with a `^' implicitly prepended. The first
    argument is then matched against this regular expression.

    If the match succeeds and REGEX uses `\(' and `\)', the `:'
    expression returns the part of STRING that matched the
    subexpression; otherwise, it returns the number of characters
    matched.

    If the match fails, the `:' operator returns the null string if
    `\(' and `\)' are used in REGEX, otherwise 0.

    和后面的 l=`echo $1 | wc -c`
    l=` expr $l - 1 `
    作用相同,但方法不同。

  8. troonv 于 2011-04-15 07:22:29发表:

    过来听课的

  9. yanyongkg 于 2011-04-15 00:54:21发表:

    [i=s] 本帖最后由 yanyongkg 于 2011-4-15 09:09 编辑 [/i]

    l=`expr "$1" : “.*”` 的意思:计算位置变量$1的长度,并把此长度赋值给变量l

    例:
    $expr "abcd" : ".*c"
    $3
    执行expr "abcd" : ".*c"后返回数值3。
    匹配.*c 这个正则表达式,abc正好匹配上,如是返回其长度3
    你提出这个问题想毕你应该知道位置变量$0,$1,这个就不说了
    .*c正则表达式这个就是重复以c结尾的任意字串,包括单字符c,因为.*是重复任意字符0次或多次的意思

  10. yanyongkg 于 2011-04-15 00:31:30发表:

    [i=s] 本帖最后由 yanyongkg 于 2011-4-15 00:36 编辑 [/i]

    $vi test.sh

    #!/bin/bash

    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    echo '$0'=$0
    echo '$1'=$1
    #罗嗦一下,三个L后面最外层的都是倒引号``,就是TAB键上面的那个符号
    #你的字母l小写的容易让人费解,跟数字1分不清,我全换成大写L做的测试

    L=`expr "$1" : ".*"` #冒号前后一定要有空格
    echo $L
    L=`echo $1 |wc -c`
    echo $L
    L=`expr $L - 1` #小横线前后一定要有空格
    echo $L
    exit 0

    $chmod a+x test.sh
    $./test.sh -abcd
    $0=./test.sh
    $1=-abcd
    5
    6
    5
    $echo '-abcd' |cat -A
    -abcd$
    $echo '-abcd' |wc -c
    6

  11. 1958586260 于 2011-04-14 23:55:36发表:

    我也是新手路过。。。。