Reading null delimited strings through a Bash loop

泪湿孤枕 提交于 2019-11-27 17:42:27

In bash you can use a here-string

while IFS= read -r -d '' line ; do
    # Code here
done <<<"$var"

Note that you should inline the IFS= and just use -d '' but make sure there is a space between the 'd' and the first single-quote. Also, add the -r flag to ignore escapes.

Also, this isn't part of your question but might I suggest a better way to do your script when using find; it uses process substitution.

while IFS= read -r -d '' file; do
    # Arbitrary operations on "$file" here
done < <(find /some/path -type f -print0)

I tried working with the bash examples above, and finally gave up, and used Python, which worked the first time. For me it turned out the problem was simpler outside the shell. I know this is possibly off topic of a bash solution, but I'm posting it here anyway in case others want an alternative.

import sh
import path
files = path.Path(".").files()
for x in files:
    sh.cp("--reflink=always", x, "UUU00::%s"%(x.basename(),))
    sh.cp("--reflink=always", x, "UUU01::%s"%(x.basename(),))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!