Ricardo

Bash 逐行读取与嵌套循环笔记

阅读时长: 1 分钟

shell readline for

Bash
1
2
3
4
while read -r line;
do
   echo "$line" ;
done < input.file
Bash
1
2
3
while read x; do echo $x; done << EOF
$(ls -l $1)
EOF
Bash
1
2
ls -l $1 | while read x; do echo $x; done
ls | while read  line;do echo "/data/path/start.sh";done
Bash
1
2
3
IFS='
'
for x in `ls -l $1`; do echo $x; done

不改变当前shell的IFS;Put a sub-shell around it if you don’t want to set IFS permanently:

Bash
1
2
3
(IFS='
'
for x in `ls -l $1`; do echo $x; done)

嵌套循环

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/bash

#set -x

cd /data/app


while read -r line;
do
   if [ ! -d $line ]
   then
       echo "$line not exist"
   fi ;
done < /tmp/applist
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
记录自己
Built with Hugo
主题 Stack 由 Jimmy 设计