I am attempting to write some some bash shell script (for the first time) to perform a few sequential actions (some copy, encrypt, upload, and simple logic checks) and I am struggling to work with the strings find
is returning as they have special characters /\*)'("
and spaces.
I have seen suggestions to fix this using printf %q
but I haven't been able to figure out the correct bash syntax.
Here is an excerpt of how I'm trying to change the strings:
#!/bin/sh
find "/upload" -type f |
while read -r path; do
fixedpath=printf %q "$path"
fixedpath=printf '%q' "$path"
fixedpath=printf '%q' $path
fixedpath=printf "%q" "$path"
fixedpath="printf %q $path"
fixedpath="printf '%q' $path"
fixedpath=$(printf '%q' $path)
echo $path >> list.txt
echo $fixedpath >> list.txt
done
exit 0
I could think of another 6-12 ways to write the printf
function as well but I think you get the point, which one do I need to have a valid path returned that I can use to pass as an argument to other commands?
I am working on Ubuntu 16.
find
is generally risky for the reasons you're noting. Depending on what you're trying to do your best bet might be to use the-exec
option tofind
so you don't have to pass it to anything else. Another option, if yourfind
supports it and the output processing handles it would be to use nul terminated output with-print0
and then something on the other side that knows how to process nul terminated strings as well (likexargs -0
or many that have a-z
flag or similar – Eric Renouf Dec 25 '17 at 15:55/bin/sh
is normally notbash
. – Cyrus Dec 25 '17 at 15:56printf
, the first four setfixedpath
and call a command named%q
, the two next ones just assign tofixedpath
. – ilkkachu Dec 25 '17 at 16:31find
to a file here. You might be better off just doing what you need from the output offind
directly, or withfind -exec
. So what it is you're actually trying to achieve in the end? – ilkkachu Dec 25 '17 at 16:33the-script
) that takes a filename as input and does the things you want to do. Thenfind … -exec the-script {} \;
– Fox Dec 25 '17 at 18:52