English 中文(简体)
绕行循环, 而不是空壳脚本循环中的单词
原标题:Iterate over lines instead of words in a for loop of shell script
  • 时间:2012-05-25 05:02:56
  •  标签:
  • linux
  • shell

Following is the shell script to read all the DSF present in the box. But since the line is having spaces, it is displaying them in different lines. For those of you who dont understand ioscan -m dsf, replace it by ls -ltr, then the output is such that the permission and names are displayed in different line, but i want them in the same line.

#!/usr/bin/ksh

for a in `ioscan -m dsf`
do
 echo  $a
done
最佳回答

环的 < code> for 环的设计不是要绕行绕行,而是绕行绕行。单词是用空格分隔的东西。行是用新线隔开的东西。 以后会有更多。

绕行绕行的方式是使用 圈与 read 结合使用 read :

ioscan -m dsf | while read -r line
do
  printf  %s
  "$line"
done

或者:

while read -r line
do
  printf  %s
  "$line"
done < <(ioscan -m dsf)

第二种变式是使用一种过程替代,可能并非在所有壳体中都可用。

两种变体都有利弊,如果你想在循环内操纵变量,这些变体的主要利弊就会明显可见。

详情请见


技术挑剔:

单词, 或在 shash 中被称作字段, 是由空间和制表符和新线分隔的东西。 基本上由白空分隔的东西 。

IFS 变量(内部字段分隔符的短时间)中定义了字段的分隔符。通常, $IFS 包含一个空间、一个标签和一个新线。

您通常会看到关于通过将 $IFS 的值改为仅新行来绕行的建议。

# not recommended
OLDIFS="$IFS"
IFS=$ 
 
for line in $(ioscan -m dsf)
do
  printf  %s
  "$line"
done
IFS="$OLDIFS"

(the $ is is called ANSI-C Quoting and might not be available in all shells)

我不建议更改 $IFS 。 许多命令依赖于 $IFS 的正常设置。 更改 $IFS 往往会造成无休止的捉虫恶梦。


另见:

问题回答

Using for

for l in $ () 执行基于 IFS 的单词分割 :

$ for l in $(printf %b  a b
c ); do echo "$l"; done
a
b
c
$ IFS=$ 
 ; for l in $(printf %b  a b
c ); do echo "$l"; done
a b
c

如果IFS以后不使用,则不必退后。

for l in $() 还执行路径名扩展 :

$ printf %b  a
*
  > file.txt
$ IFS=$ 
 
$ for l in $(<file.txt); do echo "$l"; done
a
file.txt
$ set -f; for l in $(<file.txt); do echo "$l"; done; set +f
a
*

If IFS=$ , linefeeds are stripped and collapsed:

$ printf %b  

a

b

  > file.txt
$ IFS=$ 
 ; for l in $(<file.txt); do echo "$l"; done
a
b

$( cat file.txt) (或 $( & lt; file.txt) ) 也将整个文件读成内存。

Using read

- 不使用 - r 反斜线用于连续线,并在其他字符之前删除:

$ cat file.txt
1\2
3
$ cat file.txt | while read l; do echo "$l"; done
123
$ cat file.txt | while read -r l; do echo "$l"; done
1\2
3

IFS 中的字符从线条的开始和结束处被剥除, 但没有倒塌 :

$ printf %b  1  2 
	3
  | while read -r l; do echo "$l"; done
1  2
3
$ printf %b   1  2 
	3
  | while IFS= read -r l; do echo "$l"; done
 1  2 
    3

如果最后一行没有以新行结尾, 读取给它指定 l, 但会在循环正文前退出 :

$ printf  x
y  | while read l; do echo $l; done
x
$ printf  x
y  | while read l || [[ $l ]]; do echo $l; done
x
y

如果在管道中有一个循环, 它也在一个子贝壳中, 所以变量无法在外观 :

$ x=0; seq 3 | while read l; do let x+=l; done; echo $x
0
$ x=0; while read l; do let x+=l; done < <(seq 3); echo $x
6
$ x=0; x=8 | x=9; echo $x
0

you need to use this basically IFS=$ and grep -x instead of grep as it will work like a equal to operator instead of like operator.





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签