• bash字符串前导美元符号的作用


    problem

    bash内置变量IFS作为内部单词分隔符,其默认值为<space><tab><newline>, 我想设置它仅为 ,于是:

    OLD_IFS=$IFS
    IFS='
    '
    # do some work here
    IFS=$OLD_IFS
    

    但结果为:IFS把单独的字符当作了分隔符,即分隔符被设置成下划线和字母n 。

    Why ?

    Solution

    通过google搜索,得知需要把 转化成ANSI-C Quoting,
    方法是把字符串放入$'string'中,即应该设置成:

    IFS=$'
    '
    

    顺便搜了下$字符的用途,在Unix & Linux,
    中解释了字符串前面加$字符的两种形式,一种是单引号,一种是双引号,即

    There are two different things going on here, both documented in the bash manual

    $'

    Dollar-sign single quote is a special form of quoting:
    ANSI C Quoting
    Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

    $"

    Dollar-sign double-quote is for localization:
    Locale translation
    A double-quoted string preceded by a dollar sign (‘$’) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored.
    If the string is translated and replaced, the replacement is double-quoted.

    因此单引号表示转化成ANSI-C字符,双引号则表示将字符串本地化。

    以下是一个实例,ping /etc/hosts的主机名为video-开头的主机名,检查网络状况!

    #!/bin/bash
    trap "echo 'interrupted!';exit 1" SIGHUP SIGINT SIGTERM
    OLD_IFS=$IFS
    IFS=$'
    '
    for i in `awk '$0!~/^$/ && $0!~/^#/ && $2~/^video/ {print $1,$2}' /etc/hosts`
    do
    	ADDR=$(echo $i | cut -d' ' -f 1)
    	DOMAIN=$(echo $i | cut -d' ' -f 2)
    	if ping -c 2 $ADDR &>/dev/null
    	then
    		echo $DOMAIN ok!
    	else
    		echo $DOMIN dead!
    	fi
    done
    IFS=$OLD_IFS
    
  • 相关阅读:
    【转】Java学习---HashMap的工作原理
    【转】Java学习---集合框架那些事
    Linux学习---linux下的彩蛋和各种有趣的命令
    【转】VMware虚拟机三种网络模式超详解
    沃顿商学院的MBA课程
    本杰明-富兰克林的13节制
    美学需要读的书
    芒格推荐书单
    回声消除(AEC)原理
    adc0和adc1
  • 原文地址:https://www.cnblogs.com/int32bit/p/5310525.html
Copyright © 2020-2023  润新知