zoukankan      html  css  js  c++  java
  • bash中的字符串引用

    bash中的字符串引用是一件很简单的事情,我们大多数人对此看一眼就能明了,但是今天这个技巧,也许能够帮你在未来节省不少时间。

    引用符包括 “(双引号)和 ' (单引号),最基本的用法就是引用字符串。单引号同双引号稍有不同,单引号不支持转义,但是双引号支持。例如:

    a="hello \"the\" world"            输出        hello "the" world
    a='hello "the" world'              输出     hello "the" world
    a='hello \'the\' world'            输出        报错 unexpected EOF while looking for matching ‘
    a="hello 'the' world"              输出        hello 'the' world

    b="the"
    a='hello \"$b\" world'            输出        hello \"$b\" world
    a="hello \"$b\" world"            输出        hello "the" world

    OK,以上的这些使用都没有问题,我们经常遇到问题的地方是在处理文件名中有空格的文件时。例如:

    #/bin/bash

    for i in $(find .)
    do
        echo $i
    done

    打印的结果是

    .
    ./a.txt
    ./b.txt
    ./file
    with
    space.txt
    ./quote.sh

    一个解决办法是,将原来的字段分隔符(nternal Field Separator )替换为换行,如下:

    #/bin/bash

    newline='
    '

    OIFS=$IFS
    IFS=$newline
    files=($(find .))
    IFS=$OIFS

    for i in "${files[@]}"
    do
        echo $i
    done


    另外一个问题。当我们需要访问一些单词的时候,如何避免被转义字符所影响。例如:a="hello \"there big\" world"
    #!/bin/bash

    a="hello \"there big\" world"

    for i in $a
    do
        echo $i
    done

    程序的输出和我们希望的不太一样,解决的办法是使用eval,如下:

    #!/bin/bash

    a="hello \"there big\" world"
    eval set -- $a

    for i in "$@"
    do
        echo $i
    done

    看到这里,我们在处理单引号、双引号引用的时候,一般就不会出问题了。
  • 相关阅读:
    转ihone程序内发邮件,发短信,打开链接等
    plist 文件的读写
    转object c语法速成
    转iphone项目之间的引用。
    object c求nsstring 长度和去掉前后空格的方法
    object c runtime中类类型和消息支持检查
    转NSDictionary类使用
    设置UITableview 浮动的 header
    NSString 类型plist转为NSDictionary
    ObjectiveC Unicode 转换成中文
  • 原文地址:https://www.cnblogs.com/cocowool/p/1555470.html
Copyright © 2011-2022 走看看