All four of these command works just fine with the function call. Adjust your find specs as need be.. They all cater for spaces in file names. Personally, I can't see the point of shelling out to another bash instance, but I've included two versions which call bash.
IFS=$'\n'; f=($(find /tmp -maxdepth 1 -name "$USER.*")); f7zipi "${f[@]}"
IFS=; find /tmp -maxdepth 1 -name "$USER.*" | while read -r f ;do f7zipi "$f"; done
IFS=$'\n'; bash -c 'IFS=; f7zipi "$@"' 0 $(find /tmp -maxdepth 1 -name "$USER.*")
find /tmp -maxdepth 1 -name "$USER.*" -exec bash -c 'IFS=; f7zipi "$@"' 0 {} +;
What follows is how I've set up the function, using GNU bash 4.1.5 in Ubuntu 10.04
BTW. You should use local f
in your function, so that it does not clash with the calling script's variable of the same name.
This is exactly what I added to my ~/.bashrc
function f7zipi() {
local f
for f in $@; do
ls -alF "$f"
7za a -si -t7z -m0=lzma -mx=9 -mfb=64 \
-md=64m -ms=on "$f.7z" < "$f" &&
touch -r "$f" "$f.7z" &&
rm -fv "$f" &&
ls -alF "$f.7z"
done
}
export -f f7zipi
When I only assign the above function into a terminal's bash command line, a script running from that command line fails when it calls the function... If I further apply export -f f7zipi
to that same command line.. then the script succeeds... However the scipt only works for that particular commandline session.
When the function and export are included into ~/bashrc
, the script works every time, in any bash session..
This is the test script
#!/bin/bash
f=/tmp/$USER.abc
g=/tmp/$USER.lmn
rm -fv "$f" "$f".7z
rm -fv "$g" "$g".7z
printf 'abcdefg'>"$f"
printf 'lmnopqr'>"$g"
IFS=$'\n'; f=($(find /tmp -maxdepth 1 -name "$USER.*")); f7zipi "${f[@]}"
exit