Tech Support > Operating Systems > Linux / Variants > Jockers in BASH script
Jockers in BASH script
Posted by Remi Villatel on January 23rd, 2004


Hi everybody,

I have the following line in a BASH script:

cp $1 /srv/www/htdocs/

It works, except that if I use a jocker (*), only the first file is copied.

I inserted echo $1 before the cp command. It showed me that the variable
changed into the first matching filename instead of being directly
transmitted as I expected.

The question is simple: Why and how can I avoid this behavior?

See ya,

=====================
Remi Villatel
maxilys@normandnet.fr
=====================

Posted by Michael Zedeler on January 23rd, 2004


Remi Villatel wrote:

Where does the joker come into play? Are you setting $1=*? (ie. 1='*' or
maybe 1=*)

Hmmmm... I think it would be nice to see a larger part of your script or
maybe a small, stand alone example that demonstrates your problem.

M.

Posted by Chris F.A. Johnson on January 23rd, 2004


On Fri, 23 Jan 2004 at 23:36 GMT, Remi Villatel wrote:
cp "$@" /srv/www/htdocs/

The shell converts wild cards into a list of matching file
names. Your script will not see the asterisk unless you quote it
on the command line.

You wouldn't want cp to see the asterisk, as it would look for a
file called "*":

$ cp "*" tmp
/bin/cp: cannot stat `*': No such file or directory

--
Chris F.A. Johnson http://cfaj.freeshell.org
================================================== =================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Posted by Floyd Davidson on January 23rd, 2004


Remi Villatel <maxilys@SPAMCOP.normandnet.fr> wrote:
There are several ways, depending on what you want to do.

One is to quote the * when you invoke your script,

$ foo '*'

Another is to change the positional parameter from $1 to $@.
Then your script command would be,

cp "$@" /srv/www/htdocs/

And there are other ways to do this. What you need to do is
take the time to carefully read the entire man page for bash.
Maybe twice. Not so much that you'll understand everything or
remember it all, but so that you have an idea what there is, and
when needed you'll know to go and read specifically about the
parts you need.

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com

Posted by Remi Villatel on January 24th, 2004


Michael Zedeler wrote:

$1 was supposed to be a filename which may contain jockers.

There's not much more in the script. A test which produces a little help if
the script is call with --help as parameter or no parameter at all. A test
to prevent anybody but root to use it, then the cp command.

Anyway, thanks but it's fixed now. See below in the thread.

See ya,

=====================
Remi Villatel
maxilys@normandnet.fr
=====================


Posted by Remi Villatel on January 24th, 2004


Chris F.A. Johnson wrote:

Thanks, $@ was what I was looking for. I changed the line like this:

for foo in $@ ; do
cp $foo /srv/www/htdocs/ ;
done

That is more like the behavior I expected.

On the contrary, I'd like cp to see the asterisk but without quotes.

$ cp * /tmp

Anyway, my little script works just the way I want but I'm going to see how
"getops" works for more ambitious scripts.

See ya,

=====================
Remi Villatel
maxilys@normandnet.fr
=====================



Posted by Remi Villatel on January 24th, 2004


Floyd Davidson wrote:

Thanks for not saying RTFM! ;-) I read and read but I don't write much
scripts and I think that bash manpage misses some examples. Even translated
in my natlang, or maybe because of the translation, it's not always very
clear. And it's so loooooong!

Maybe should I consider extending my bookcase with a good bash scripting book?

See ya,

=====================
Remi Villatel
maxilys@normandnet.fr
=====================



Posted by Chris F.A. Johnson on January 24th, 2004


On Sat, 24 Jan 2004 at 11:52 GMT, Remi Villatel wrote:
The example I gave is what happens if cp _does_ see the asterisk;
that's _not_ what you want.

When you do:

cp * /tmp

cp does _not_ see the asterisk; the shell converts it to a list of
files _before_ cp sees it; cp receives the list.

Good luck!

--
Chris F.A. Johnson http://cfaj.freeshell.org
================================================== =================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License


Similar Posts