Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #275364

Re: Shell function & variable usage.

From Greg Wooledge <greg@wooledge.org>
Newsgroups linux.debian.user
Subject Re: Shell function & variable usage.
Date 2024-12-07 01:00 +0100
Message-ID <JQQoN-eiL1-3@gated-at.bofh.it> (permalink)
References <JQQ5r-eiDW-13@gated-at.bofh.it> <JQQf8-eiHL-5@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Fri, Dec 06, 2024 at 18:46:06 -0500, eben@gmx.us wrote:
> You could do something
> overengineered like defining an array "excludepatterns" for patterns, and
> then doing something like
> 
> rsync  \
> $(for pattern in ${excludepatterns[@]} ; do
>   echo -- "--exclude  '$pattern'"
> done)
> 
> Modulo mistakes, of course.

This definitely has mistakes.  You're expanding each of the patterns
based on the files in the current working directory.

hobbit:~$ exclusions=( '*.mp3' '*.mp4' '*.wav' )
hobbit:~$ echo *.mp3
christmas-peebs.mp3 inmexico.mp3 Kristie_and_John_-_Bobby_McGee.mp3
hobbit:~$ for p in ${exclusions[@]}; do echo -- "--exclude '$p'"; done
-- --exclude 'christmas-peebs.mp3'
-- --exclude 'inmexico.mp3'
-- --exclude 'Kristie_and_John_-_Bobby_McGee.mp3'
-- --exclude '*.mp4'
-- --exclude '*.wav'

That's not what you want.  (You're also writing a literal -- which is
not wanted.)

As I wrote in the previous message, you can generate the second array
using a single command that doesn't perform pathname expansions:

hobbit:~$ opts=( "${exclusions[@]/#/--exclude=}" )
hobbit:~$ declare -p opts
declare -a opts=([0]="--exclude=*.mp3" [1]="--exclude=*.mp4" [2]="--exclude=*.wav")

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Shell function & variable usage. peter@easthope.ca - 2024-12-07 00:40 +0100
  Re: Shell function & variable usage. Greg Wooledge <greg@wooledge.org> - 2024-12-07 00:50 +0100
    Re: Shell function & variable usage. peter@easthope.ca - 2024-12-07 15:40 +0100
      Re: Shell function & variable usage. Greg Wooledge <greg@wooledge.org> - 2024-12-07 17:10 +0100
        Re: Shell function & variable usage. peter@easthope.ca - 2024-12-08 19:50 +0100
  Re: Shell function & variable usage. eben@gmx.us - 2024-12-07 00:50 +0100
    Re: Shell function & variable usage. eben@gmx.us - 2024-12-07 01:00 +0100
    Re: Shell function & variable usage. Greg Wooledge <greg@wooledge.org> - 2024-12-07 01:00 +0100
  Re: Shell function & variable usage. Karl Vogel <vogelke@pobox.com> - 2024-12-07 12:40 +0100
  Re: Shell function & variable usage. Anssi Saari <anssi.saari@debian-user.mail.kapsi.fi> - 2024-12-11 14:30 +0100

csiph-web