GDPR-ohya4wai4eiJ
April 3rd, 2012, 08:06 PM
I want to write a script which can read comments from a file and parses them. The problem is that a for loop in bash interprets /* from beginning C-comments as directory listing. Here is an example script:
#!/bin/bash -e
IFS='
'
comment='/*
C-Comment
*/'
i=1
for line in $comment
do
echo "$i: $line"
i=`expr $i + 1`
done
This is what I want to have:
1: /*
2: C-Comment
3: */
This is what I'm currently getting:
1: /bin
2: /boot
3: /dev
4: /etc
5: /home
6: /initrd.img
7: /lib
8: /lib32
9: /lib64
10: /libnss3.so
11: /lost+found
12: /media
13: /mnt
14: /opt
15: /proc
16: /root
17: /run
18: /sbin
19: /selinux
20: /srv
21: /sys
22: /tmp
23: /usr
24: /var
25: /virtualbox
26: /vmlinuz
27: /wine
28: C-Comment
29: deb/
30: doc/
31: src/
32: upstart/
Quoting $comment in the for loop doesn't help because $IFS isn't splitting anymore in this case. I'm getting then the following:
1: /*
C-Comment
*/
Does anybody know how to solve this problem?
#!/bin/bash -e
IFS='
'
comment='/*
C-Comment
*/'
i=1
for line in $comment
do
echo "$i: $line"
i=`expr $i + 1`
done
This is what I want to have:
1: /*
2: C-Comment
3: */
This is what I'm currently getting:
1: /bin
2: /boot
3: /dev
4: /etc
5: /home
6: /initrd.img
7: /lib
8: /lib32
9: /lib64
10: /libnss3.so
11: /lost+found
12: /media
13: /mnt
14: /opt
15: /proc
16: /root
17: /run
18: /sbin
19: /selinux
20: /srv
21: /sys
22: /tmp
23: /usr
24: /var
25: /virtualbox
26: /vmlinuz
27: /wine
28: C-Comment
29: deb/
30: doc/
31: src/
32: upstart/
Quoting $comment in the for loop doesn't help because $IFS isn't splitting anymore in this case. I'm getting then the following:
1: /*
C-Comment
*/
Does anybody know how to solve this problem?