首页 > 学技术 > 技术网文 > Linux Shell > 正文

[精彩] 双引号前的$是什么意思


来源 chinaunix.net 酷勤网整理

以前寫的,用來加強對 bash builtin 的記憶..hehehe
灌水相當合用 :)
#! /bin/bash
# A little game to guess bash builtin command
# written by no_spam_twf_cc@yahoo.com.hk
# $date: Jul 20,2004 $PROG: guess.sh   $LICINCE: PUBLIC DOMAIN

# Program name
PROG=${0##*/}

# array, contains command name of bash builtin
KEYWORD=(alias bind builtin cd compgen continue dirs echo eval
         exit false fg for getopts help if kill local popd pushd
         read return set shopt suspend time trap type ulimit unalias
         until wait bg break case command complete
         declare disown enable exec export fc for function hash history
         jobs let logout printf pwd readonly select shift source test
         times true typeset umask unset variables while \: \(\( \[\[ \[)

# Get a random word to guess
WORD=${KEYWORD[$(($RANDOM%${#KEYWORD[@]}))]}

# Length of the word
LENGTH="${#WORD}"

# Split the word , array
SPLIT_WORD=($(echo $WORD | sed 's/./& /g'))

# replace the word by -
GUESS=$(echo $WORD | sed 's/./-/g')

# function to get user input
function MyGuess {
echo -n 'Guess the command: ' >&2
read -n 1
}

# function, displays information
function ShowInfo {
echo "The builtin command contains $LENGTH letters"
echo 'Guess the letter one by one.'
echo "$GUESS"
}
# header
cat<<!
#######################################################

$(echo "Hello $(whoami)"), welcome to $(echo $PROG)
It is a little game to guess BASH builtin command
Here we go.

#######################################################

!

ShowInfo
# main loop

for ((i=0 ; i<$LENGTH ; ++i))
do
     MyGuess
     if [ "$REPLY" != "${SPLIT_WORD[$i]}" ] ; then
           echo
           echo -e "The letter is \033[31m${SPLIT_WORD[$i]}\033[39m"
     else
           echo
           echo -e "\033[32mRight.\033[39m"
     fi

           ANSWER="$ANSWER${SPLIT_WORD[$i]}"
           DISPLAY="${ANSWER}${GUESS:$(($i + 1))}"
           echo "$DISPLAY"

done
echo 'Thankyou for playing.'
echo ""
# Display usage
help $(echo $WORD)

重看自己的「作品」,真是進步不大 ...:(



 網中人 回复于:2005-09-08 14:15:15

man bash, 找到 QUOTING 那段說明:

       Words of the form $'string' are treated specially.  The word expands to
       string,  with  backslash-escaped characters replaced as specifed by the
       ANSI C standard.  Backslash escape sequences, if present,  are  decoded
       as follows:
              \a     alert (bell)
              \b     backspace
              \e     an escape character
              \f     form feed
              \n     new line
              \r     carriage return
              \t     horizontal tab
              \v     vertical tab
              \\     backslash
              \'     single quote
              \nnn   the  eight-bit  character  whose value is the octal value
                     nnn (one to three digits)
              \xHH   the eight-bit character whose value  is  the  hexadecimal
                     value HH (one or two hex digits)
              \cx    a control-x character

       The  expanded  result  is  single-quoted, as if the dollar sign had not
       been present.

[color=darkred]       A double-quoted string preceded by a dollar sign  ($)  will  cause  the
       string  to  be translated according to the current locale.  If the cur-
       rent locale is C or POSIX, the dollar sign is ignored.  If  the  string
       is translated and replaced, the replacement is double-quoted.[/color]


 yearnx 回复于:2005-09-08 14:19:51

引用:原帖由 "網中人" 发表:
      A double-quoted string preceded by a dollar sign  ($)  will  cause  the 
      string  to  be translated according to the current locale.  If the cur- 
      rent locale is C or POSIX, the dollar sign is ignored.  If  the  string 
      is translated and replaced, the replacement is double-quoted.



good job

看来也没有什么用!


 網中人 回复于:2005-09-08 14:21:49

有用啊. 不是提到 locale 了嗎?


 網中人 回复于:2005-09-08 14:22:42

嗯? 怎麼搞的, 我是回貼的人, 怎變了發貼的?


 waker 回复于:2005-09-08 14:23:56

引用:原帖由 "網中人"]嗯? 怎麼搞的, 我是回貼的人, 怎變了發貼的?
 发表:


这叫粹取 :mrgreen:


 大蚂蚁 回复于:2005-09-08 15:17:06

If the current locale is C or POSIX, the dollar sign is ignored.


  the  string     is translated and replaced, the replacement is double-quoted.

我想我会很快忘记的


 yearnx 回复于:2005-09-08 16:17:25

大侠~你理解的locale是什么意思啊?
能用中文解释下Me?


 網中人 回复于:2005-09-08 16:25:22

簡單來說, 就是"當地語系", 比方 zh_CN, zh_TW.Big5 之類.
你可輸入 locale 命令查看語系設定.
你可用 export LC_ALL=xxxxxxxx 來改變語系設定值.
其中, LANG=xxxxxxxx 則需另設.

更多的, "找男人"跟"挖股溝"吧..........  ^_^


 寂寞烈火 回复于:2005-09-08 16:30:15

试了几个locale,没感觉啥 :?:


 網中人 回复于:2005-09-08 16:32:40

那在 X window 下測中日韓越幾種不同的"文字"看看?


 waker 回复于:2005-09-08 16:42:28

引用:原帖由 "寂寞烈火"]试了几个locale,没感觉啥 :?:
 发表:


你试echo是没用的
localmessage记录在每个程序所用的mo文件中比如
LC_ALL=zh_CN date
其实要去 /usr/share/local/zh_CN/之类的地方找到date.mo取出中文信息来显示

我的系统中是没有echo.mo和bash.mo的 :evil:  :mrgreen:


 waker 回复于:2005-09-08 16:44:04

gettext大概就是干这个活儿的:mrgreen:




原文链接:http://bbs.chinaunix.net/viewthread.php?tid=608189
转载请注明作者名及原文出处



收藏本页到: