標籤:shell ifs readline 解析
processLine(){line="[email protected]"echo $line} FILE="" if [ "$1" == "" ]; then FILE="/dev/stdin"else FILE="$1" if [ ! -f $FILE ]; then echo "$FILE : does not exists" exit 1 elif [ ! -r $FILE ]; then echo "$FILE : can not read" exit 2 fifi BAKIFS=$IFSIPS=$(echo -en "nb")exec 3<&0exec 0<$FILEwhile read linedo processLine $linedoneexec 0<&3 IFS=$BAKIFSexit 0
使用IFS來進行檔案分析,或者是對檔案特定的字元進行分割
#shell environment $IFS useage# S : Internal Field SeparatorIFS=:a=Hello:Worldecho $a #OutPut is Hello World #IFS save Key Value, maybe space\tab\'\n'\other token#sparse input or output key value # The following code segment will only work in ksh,# and it will fail in bash IP=192.168.0.254IFS="."TMPIP=$(echo $IP)IFS=" "#spaceecho $TMPIP | read ip1 ip2 ip3 ip4INVIP=$ip4.$ip3.$ip2.$ip1echo $INVIP
我們將IFS的內容輸出來,但是由於一些內容是不能直接顯示出來,但是我們可以將使用一些格式化將內容輸出:
echo $IFS | od -decho $IFS | od -becho $IFS | od -x
分別是使用不同進位的表示出來:
如果是用冒號("")引起來,表示這個變數不用IFS替換!!所以可以看到這個變數的"原始值"。反之,如果不加引號,輸出時會根據IFS的值來分割後合并輸出! $* 是按照IFS中的第一個值來確定的!下面這兩個例子還有細微的差別!
shell IFS readLine.sh