文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>对于IFS的一点总结

对于IFS的一点总结

时间:2009-07-21  来源:tekkamanninja

在编写shell脚本的时候,时常会碰到IFS变量,但我对于具体的使用和设置常常比较模糊,今天花了一点时间googel和实验了一下,总结如下:

(1)什么是IFS ?
IFS是bash的内部变量,称为内部域分隔符.这个变量用来决定Bash在解释字符串时如何识别域,或者单词边界.

(2)如何查看当前的IFS值?

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"


由于IFS默认为空白(空格,tab和新行),所以使用以上的命令似乎看不到字符。没关系,你可以用od命令看16进制,或是2进制值:

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004


注意:$*使用$IFS 中的第一个字符,比如:

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w x y z



(3)如何修改IFS值?
普通的赋值命令即可:

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS=":"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"
:
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 3a 0a
0000002
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w:x:y:z



(4)实验:$*使用$IFS 中的第一个字符

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS="\\:;"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"
\:;
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 5c 3a 3b 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w\x\y\z
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS=":;\\"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS"
:;\
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 3a 3b 5c 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ set w x y z;echo "$*"
w:x:y:z


(5)备份IFS

tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ OLDIFS="$IFS"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$OLDIFS" | od -t x1
0000000 20 09 0a 0a
0000004
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS=":"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 3a 0a
0000002
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ IFS="$OLDIFS"
tekkaman@tekkaman-desktop:~/working/abs_test/IFS$ echo "$IFS" | od -t x1
0000000 20 09 0a 0a
0000004



参考资料: 《[精彩] 关于IFS的疑问》
         《ABS 3.7.2中文版》翻译:杨春敏 黄毅
相关阅读 更多 +
排行榜 更多 +
浴血混战官方下载

浴血混战官方下载

飞行射击 下载
检票员模拟器免广告下载

检票员模拟器免广告下载

模拟经营 下载
最终前哨最终版手机版下载

最终前哨最终版手机版下载

休闲益智 下载