Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-28-2005, 11:38 AM   #1
LQ Newbie
Registered: Jul 2005
Location: Chicago Suburbs, USA
Distribution: Slackware 11
Posts: 7

Rep: Reputation: 0
Globbing Problem in BASH

In one of my scripts, I want the user to have the ability to input a parameter with a directory path and a file name with a glob pattern so that the script can expand it there. The problem is that the shell is expanding it in the parameter list already. I can demonstrate with the sample script, Here is the script:

set -o noglob
echo "all parameters are:" $@
echo "The first parameter is:" $1

When I run it as shown:

./ /export/nfshome/pggah/*.txt

I get the following output:

all parameters are: /export/nfshome/pggah/jackie2.txt
/export/nfshome/pggah/logfile.txt /export/nfshome/pggah/newfile.txt
/export/nfshome/pggah/pggah.txt /export/nfshome/pggah/small.txt
The first parameter is: /export/nfshome/pggah/jackie2.txt

It is only if I escape the asterisk as shown:

./ /export/nfshome/pggah/\*.txt

Do I get what I want:

all parameters are: /export/nfshome/pggah/*.txt
The first parameter is: /export/nfshome/pggah/*.txt

I have tried setting the noglob option in the script with +o, -o and without it and it makes no difference. I would prefer not having to escape the asterisk. Does anyone know how I can take in the input parameter without it globbing?

Thanks in advance.
Old 07-28-2005, 12:43 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Don't fight with yout shell, on the contrary work with it...

For example the convert command from ImageMagik has
a -profile='*' option (to blank the profile infos of an image)

If you don't quote the * in your shell, -profile option is filled
with all files in current directory and convert outputs an error...

I haven't see a program that take care of that by itself yet

Last edited by keefaz; 07-28-2005 at 12:45 PM.
Old 07-28-2005, 01:19 PM   #3
LQ Newbie
Registered: Jul 2005
Location: Chicago Suburbs, USA
Distribution: Slackware 11
Posts: 7

Original Poster
Rep: Reputation: 0
Great suggestion. I tried quoting the parameter as shown

./ '/export/nfshome/pggah/*.txt'

and the shell did not expand it out. Thanks for the help. It is not exactly how I was trying to acomplish it, but it works and better yet without any coding changes. Thanks again.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with bash adityavpratap Slackware 9 06-13-2006 10:44 AM
a problem about bash naihe2010 Linux - Enterprise 1 10-15-2005 12:06 PM
Problem with BASH Pheidon Linux From Scratch 5 06-24-2005 03:34 AM
bash problem cL4YmAN Linux - Newbie 1 06-07-2004 02:32 PM
bash problem Vincent Slackware 2 10-30-2003 06:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:17 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration