写java命令文件名怎么带空格_迭代带空格的文件列表

有几种可行的方法可以实现这一目标 .

如果你想紧贴原始版本,可以这样做:

getlist() {

IFS=$'\n'

for file in $(find . -iname 'foo*') ; do

printf 'File found: %s\n' "$file"

done

}

如果文件名中包含文字换行符,则仍然会失败,但空格不会破坏它 .

但是,没有必要弄乱IFS . 这是我首选的方法:

getlist() {

while IFS= read -d $'\0' -r file ; do

printf 'File found: %s\n' "$file"

done <

}

如果您发现 <

相比于几乎等同的版本,这样做的优点

getlist() {

find . -iname 'foo*' -print0 | while read -d $'\0' -r file ; do

printf 'File found: %s\n' "$file"

done

}

是否保留了while循环体中的任何变量赋值 . 也就是说,如果您如上所述管道 while ,则 while 的主体位于子壳中,这可能不是您想要的 .

流程替换版本优于 find ... -print0 | xargs -0 的优势是最小的:如果您只需要打印一行或对文件执行单个操作, xargs 版本就可以了,但如果您需要执行多个步骤,则循环版本更容易 .

EDIT :这是一个不错的测试脚本,因此您可以了解解决此问题的不同尝试之间的区别

#!/usr/bin/env bash

dir=/tmp/getlist.test/

mkdir -p "$dir"

cd "$dir"

touch 'file not starting foo' foo foobar barfoo 'foo with spaces'\

'foo with'$'\n'newline 'foo with trailing whitespace '

# while with process substitution, null terminated, empty IFS

getlist0() {

while IFS= read -d $'\0' -r file ; do

printf 'File found: '"'%s'"'\n' "$file"

done <

}

# while with process substitution, null terminated, default IFS

getlist1() {

while read -d $'\0' -r file ; do

printf 'File found: '"'%s'"'\n' "$file"

done <

}

# pipe to while, newline terminated

getlist2() {

find . -iname 'foo*' | while read -r file ; do

printf 'File found: '"'%s'"'\n' "$file"

done

}

# pipe to while, null terminated

getlist3() {

find . -iname 'foo*' -print0 | while read -d $'\0' -r file ; do

printf 'File found: '"'%s'"'\n' "$file"

done

}

# for loop over subshell results, newline terminated, default IFS

getlist4() {

for file in "$(find . -iname 'foo*')" ; do

printf 'File found: '"'%s'"'\n' "$file"

done

}

# for loop over subshell results, newline terminated, newline IFS

getlist5() {

IFS=$'\n'

for file in $(find . -iname 'foo*') ; do

printf 'File found: '"'%s'"'\n' "$file"

done

}

# see how they run

for n in {0..5} ; do

printf '\n\ngetlist%d:\n' $n

eval getlist$n

done

rm -rf "$dir"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值