Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1093189
  • 博文数量: 242
  • 博客积分: 10209
  • 博客等级: 上将
  • 技术积分: 3028
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-12 09:27
文章分类

全部博文(242)

文章存档

2014年(1)

2013年(1)

2010年(51)

2009年(65)

2008年(124)

我的朋友

分类: LINUX

2010-09-07 11:30:02


在前面的 Bash命令行处理流程详解中,
第九步的 wrod spliting 用到了 LFS

http://blog.chinaunix.net/u2/63316/showart_2152062.html

摘自man bash 的关于 IFS 的东西  

    IFS    The  Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command.  The default value is ``''.

1.   Word Splitting
       The  shell  scans  the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.
       The shell treats each character of IFS as a delimiter, and splits the results of the other expansions  into  words on  these  characters.   If  IFS  is  unset,  or its value is exactly , the default, then any sequence of IFS characters serves to delimit words.  If IFS has a value other than the default, then sequences  of the  whitespace  characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character).  Any character in IFS that is not IFS whitespace,  along  with any adjacent IFS whitespace characters, delimits a field.  A sequence of IFS whitespace characters is also treated as a delimiter.  If the value of IFS is null, no word splitting occurs.
        Explicit null arguments ("" or '') are retained.  Unquoted implicit null arguments, resulting from  the  expansion of  parameters that have no values, are removed.  If a parameter with no value is expanded within double quotes, a null argument results and is retained.


        bash splits only the results of expansions on IFS, using
        POSIX.2 field splitting rules; sh splits all words on IFS.
 

2.      $*      Expands to the positional parameters, starting from  one.   When
              the  expansion occurs within double quotes, it expands to a sin-
              gle word with the value of each parameter separated by the first
              character of the IFS special variable.  That is, "$*" is equiva-
              lent to "$1c$2c...", where c is the first character of the value
              of  the IFS variable.  If IFS is unset, the parameters are sepa-
              rated by spaces.  If IFS is  null,  the  parameters  are  joined
              without intervening separators.

  3 .  Any element of an array may be referenced using ${name[subscript]}.  The braces are required  to  avoid  conflicts with  pathname  expansion.   If  subscript  is  @ or *, the word expands to all members of name.  These subscripts differ only when the word appears within double quotes.  If the word is double-quoted,  ${name[*]}  expands  to  a single  word with the value of each array member separated by the first character of the IFS special variable, and
${name[@]} expands each element of name to a separate word.  When there are no array members,  ${name[@]}  expands to  nothing.   If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of  the  original word.  This is analogous to the expansion of the special parameters * and @ (see Special Parameters above).  ${#name[subscript]} expands to the length of ${name[subscript]}.  If subscript is * or @, the expansion is  the number of elements in the array.  Referencing an array variable without a subscript is equivalent to referencing element zero.


以下是Advanced Bash-Scripting Guide中关于IFS的论述

$IFS defaults to (space, tab, and newline), but may be changed, for example, to parse a comma-separated data file. Note that uses the first character held in $IFS. See .

bash$ echo "$IFS"

(With $IFS set to default, a blank line displays.)



bash$ echo "$IFS" | cat -vte
^I$
$

(Show whitespace: here a single space, ^I [horizontal tab],
and newline, and display "$" at end-of-line.)




bash$ bash -c 'set w x y z; IFS=":-;"; echo "$*"'
w:x:y:z
(Read commands from string and assign any arguments to pos params.)

Caution

$IFS does not handle whitespace the same as it does other characters.

Example 9-1. $IFS and whitespace

#!/bin/bash
# ifs.sh


var1="a+b+c"
var2="d-e-f"
var3="g,h,i"

IFS=+
# The plus sign will be interpreted as a separator.
echo $var1 # a b c
echo $var2 # d-e-f
echo $var3 # g,h,i

echo

IFS="-"
# The plus sign reverts to default interpretation.
# The minus sign will be interpreted as a separator.
echo $var1 # a+b+c
echo $var2 # d e f
echo $var3 # g,h,i

echo

IFS=","
# The comma will be interpreted as a separator.
# The minus sign reverts to default interpretation.
echo $var1 # a+b+c
echo $var2 # d-e-f
echo $var3 # g h i

echo

IFS=" "
# The space character will be interpreted as a separator.
# The comma reverts to default interpretation.
echo $var1 # a+b+c
echo $var2 # d-e-f
echo $var3 # g,h,i

# ======================================================== #

# However ...
# $IFS treats whitespace differently than other characters.

output_args_one_per_line()
{
for arg
do
echo "[$arg]"
done # ^ ^ Embed within brackets, for your viewing pleasure.
}

echo; echo "IFS=\" \""
echo "-------"

IFS=" "
var=" a b c "
# ^ ^^ ^^^
output_args_one_per_line $var # output_args_one_per_line `echo " a b c "`
# [a]
# [b]
# [c]


echo; echo "IFS=:"
echo "-----"

IFS=:
var=":a::b:c:::" # Same pattern as above,
# ^ ^^ ^^^ #+ but substituting ":" for " " ...
output_args_one_per_line $var
# []
# [a]
# []
# [b]
# [c]
# []
# []

# Note "empty" brackets.
# The same thing happens with the "FS" field separator in awk.


echo

exit

(Many thanks, Stéphane Chazelas, for clarification and above examples.)






阅读(2071) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~