免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 12472 | 回复: 13

IFS=$'\n' IFS="\n" or IFS='\n' 差别 [复制链接]

论坛徽章:
0
发表于 2010-07-13 23:29 |显示全部楼层
RT
THANKS

论坛徽章:
0
发表于 2010-07-14 08:05 |显示全部楼层
在论坛中搜索IFS,也发现有人问这问题.

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
发表于 2010-07-14 08:25 |显示全部楼层
从效果上衡量没差别
从过程上看后两个没差别,第一个在命令解析时中\n已经转化为NL了,后两个在命令执行时转化

论坛徽章:
0
发表于 2010-07-14 08:35 |显示全部楼层
  1. ll
  2. total 32
  3. -rw-r--r--  1 root root   0 Jul 13 12:22 a bak
  4. -rw-r--r--  1 root root   0 Jul 13 12:22 b bak
  5. -rw-r--r--  1 root root   0 Jul 13 12:22 c bak
  6. -rwxr-xr-x  1 root root 137 Jul 12 17:29 test01.sh
  7. -rwxr-xr-x  1 root root 344 Jul 13 12:16 test02.sh
  8. -rwxr-xr-x  1 root root 347 Jul 13 14:49 test03.sh
  9. -rwxr-xr-x  1 root root 294 Jul 13 15:39 test04.sh
  10. -rwxr-xr-x  1 root root  87 Jul 13 15:25 test05.sh
  11. -rwxr-xr-x  1 root root 221 Jul 14 08:45 test06.sh
  12. -rwxr-xr-x  1 root root  91 Jul 13 16:58 test07.sh
  13. -rwxr-xr-x  1 root root 190 Jul 13 17:29 test08.sh
复制代码
想匹配有bak的文件
测试一,
  1. #!/bin/sh
  2. SAVEIFS=$IFS;
  3. #IFS=$'\n'
  4.   IFS='\n'
  5.   for file in `ls -1 /tmp/shell/bash`
  6.         do
  7.            if [[ "$file" =~ "bak" ]]; then
  8.                 echo $file
  9.             fi
  10.          #  echo "$file"
  11.         done
  12. IFS=$SAVEIFS
复制代码
执行结果:
a bak
b bak
c bak
test01.sh
test02.sh
test03.sh
test04.sh
test05.sh
test06.sh
test07.sh
test08.sh

测试二,
  1. #!/bin/sh
  2. SAVEIFS=$IFS;
  3. IFS=$'\n'
  4.   for file in `ls -1 /tmp/shell/bash`
  5.         do
  6.            if [[ "$file" =~ "bak" ]]; then
  7.                 echo $file
  8.             fi
  9.          #  echo "$file"
  10.         done
  11. IFS=$SAVEIFS
复制代码
执行结果:
a bak
b bak
c bak

我在 本论坛 的帖子 <某目录下含有空格的文件,如何处理> 有这个问题, 是黑哥提出用IFS=$'\n'的,想问为什么,黑兄要我man bash 嘿嘿(man了半天,没看出所以然来,所以再开一帖)

论坛徽章:
0
发表于 2010-07-14 08:38 |显示全部楼层
IFS=$'\n'  表示用 换行符 做分隔
IFS="\n" 与 IFS='\n'  都是用 n 字符作为分隔

论坛徽章:
0
发表于 2010-07-14 08:46 |显示全部楼层
哦,能否解释一下 IFS=$'\n'  这个$

论坛徽章:
0
发表于 2010-07-14 08:56 |显示全部楼层
IFS=$'\n'  表示用 换行符 做分隔
IFS="\n" 与 IFS='\n'  都是用 n 字符作为分隔
springwind426 发表于 2010-07-14 08:38



    已这个角本来理解的话,他所有IFS都是回车吧.

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
发表于 2010-07-14 08:59 |显示全部楼层
本帖最后由 blackold 于 2010-07-14 09:05 编辑

bash 3.2.48
$ var="foo\nbar
123"

$ echo -En "$var"
foo\nbar
123
$ echo -En "$var"|xxd
0000000: 666f 6f5c 6e62 6172 0a31 3233            foo\nbar.123

$ IFS=$'\n'

$ echo -n "$IFS"|xxd
0000000: 0a                                       .

$ c=0;for i in $var;do echo "$((++c)): [$i]";done
1: [foo\nbar]
2: [123]
; 分隔符为\n, 即回车符

$ IFS="\n"

$ echo -n "$IFS"|xxd
0000000: 5c6e                                     \n

$ c=0;for i in $var;do echo "$((++c)): [$i]";done
1: [foo]
2: []
3: [bar
123]
; 分隔符为\和n

$ IFS='\n'

$ echo -n "$IFS"|xxd
0000000: 5c6e                                     \n

$ c=0;for i in $var;do echo "$((++c)): [$i]";done
1: [foo]
2: []
3: [bar
123]
; 分隔符为\和n

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
发表于 2010-07-14 09:11 |显示全部楼层
回复 4# addictlinux

    Words of the form $aqstringaq are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

    \a
        alert (bell)
    \b
        backspace
    \e
        an escape character
    \f
        form feed
    \n
        new line
    \r
        carriage return
    \t
        horizontal tab
    \v
        vertical tab
    \\
        backslash
    \aq
        single quote
    \nnn
        the eight-bit character whose value is the octal value nnn (one to three digits)
    \xHH
        the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
    \cx
        a control-x character

论坛徽章:
0
发表于 2010-07-14 09:47 |显示全部楼层
学习了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP