Processline () {line= "[email protected]" echo $line} file= "" if ["] = = =" "]; Then file= "/dev/stdin" Else file= "$" 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< $FILEWH Ile Read Linedo processline $linedoneexec 0<&3 ifs= $BAKIFSexit 0
Use IFS for file analysis, or to split file-specific characters
#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 would only work In ksh,# and it'll fail in bash ip=192.168.0.254ifs= "." tmpip=$ (Echo $IP) ifs= "" #spaceecho $TMPIP | Read Ip1 ip2 ip3 ip4invip= $ip 4. $ip 3. $ip 2. $ip 1echo $INVIP
We output the IFS content, but since some of the content is not directly visible, we can use some formatting to output the content:
echo $IFS | Od-decho $IFS | Od-becho $IFS | Od-x
each is represented by a different binary:
If it is caused by a colon (""), it means that the variable is not replaced with IFS!! So you can see the "original value" of this variable. Conversely, if you do not quote, the output will be based on the value of IFS to split after the combined output! $* is determined by the first value in the IFS! Here are two examples with subtle differences!
Shell IFS readline.sh