shell IFS readLine.sh,ifsreadline.sh

來源:互聯網
上載者:User

shell IFS readLine.sh,ifsreadline.sh

processLine(){line="$@"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中的第一個值來確定的!下面這兩個例子還有細微的差別!






相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.