I have a script that uses strace, cp, awk, and stat, to create a cp with a progress bar. Here is the part of the code where it calls cp:
strace -q -ewrite cp -- `printf '%q ' $@` 2>&1 | awk {Lots of code here}
The problem is, I cannot copy anything with a space. How should I modify this script so it will work with spaces? Thanks
EDIT: Here is the output:
matt: ~/tmp $ bash -x cp-progress "q" "file"
++ printf '%q ' q file
++ stat -c %s q
+ strace -q -ewrite cp -- q file
+ awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%2d%% [", percent
for (i=0;i<=percent / 2;i++)
printf "→"
printf "→"
printf "]\r"
}
}
END { print "" }' total_size=5242880 count=0
100% [→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→]
matt: ~/tmp $ bash -x cp-progress "q" "file with spaces"
++ printf '%q ' q 'file with spaces'
+ strace -q -ewrite cp -- q 'file\' 'with\' spaces
++ stat -c %s q
+ awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%2d%% [", percent
for (i=0;i<=percent / 2;i++)
printf "→"
printf "→"
printf "]\r"
}
}
END { print "" }' total_size=5242880 count=0
0% [→→]
See the first one? That works fine, which executes cp -- q file. Now, the next one, cp -- q 'file\' 'with\' spaces how do I fix that?
use
"$@"man bash:
In your case, use this form: