cerr <
ron.e...@gmail.com> wrote:
> Hi Dave,
>
> Thank you for taking the time to assist me with this.
> However, one more question:
>
> I now have /bin/tar -C ram/bin/ -cvjf ram/bin/136967148910.tar.bz2 \
> $(cd ram/bin/ ls +*.bin| head -20)
>
> which seems to work but as you say:
>
>> Which still won't handle spaces in filenames. Use the array method.
>
> I'm unsure how to use the array method, I've tried stuff with |@|0:20
> but my shell doesn't seem to like this. I'm not running on bash, my
> application is running on /bin/sh, may that be the reason?
Yes. Is ksh (the real one -- ksh93) available?
> As for the spaces, I don't expect that this would ever become an issue
> as my applicatoin is in charge of the file creation, always! Howevcer,
> if spaces were supported, it definitely would be a cleaner approaach...
> Any hints?
Use globbing and the positional parameters ("$@").
cd ram/bin
set -- *.bin
cd -
# How many filenames in the list?
want=20
# If there are more than $want elements in the argument list, rearrange
# the list so that the first $want elements are at the end then shift
# everything else out.
if [ $# -gt $want ]; then
i=0
while [ $i -lt $want ]; do
set -- "$@" "$1"
shift
i=$(( $i + 1 ))
done
shift $(( $# - $want ))
fi
/bin/tar -C ram/bin -cvjf ram/bin/13...10.tar.bz2 "$@"
To have the list in an order other than that provided by globbing it
will be necessary to use command substitution.
The following approach breaks on filenames containing newlines. Then
again, if you have filenames containing newlines this thing breaking
is unlikely to be your main problem.
list=$( cd ram/bin && ls *.bin | head -n 20 )
# Restrict word splitting to newlines
oifs=$IFS
IFS='
'
# Prevent the shell expanding any wildcards in filenames while expanding
# the unquoted $list (tar's arguments).
set -f
/bin/tar -C ram/bin -cvjf ram/bin/13...10.tar.bz2 $list
set +f
IFS=$oifs
# Similar, using the positional parameters.
oifs=$IFS
IFS='
'
set -f
set -- $( set +f ; cd ram/bin && ls *.bin | head -n 20 )
set +f
IFS=$oifs
/bin/tar -C ram/bin -cvjf ram/bin/13...10.tar.bz2 "$@"