ShellIFSreadLine. 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
Use IFS for file analysis, or separate specific file 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 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
We output the content of IFS, but some content cannot be directly displayed, but we can use some formatting to output the content:
echo $IFS | od -decho $IFS | od -becho $IFS | od -x
They are represented in different hexadecimal formats:
If it is caused by a colon (""), this variable does not need to be replaced by IFS !! So we can see the "original value" of this variable ". Otherwise, if no quotation marks are added, the output will be split Based on the IFS value and then merged into the output! $ * Is determined based on the first value in IFS! The two examples below are slightly different!