LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-28-2005, 10:38 AM   #1
Hink
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, globproblem.sh. Here is the script:

#!/bin/bash
set -o noglob
echo "all parameters are:" $@
echo "The first parameter is:" $1

When I run it as shown:

./globproblem.sh /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:

./globproblem.sh /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, 11:43 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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 11:45 AM.
 
Old 07-28-2005, 12:19 PM   #3
Hink
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

./globproblem.sh '/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.
 
  


Reply



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 09:44 AM
a problem about bash naihe2010 Linux - Enterprise 1 10-15-2005 11:06 AM
Problem with BASH Pheidon Linux From Scratch 5 06-24-2005 02:34 AM
bash problem cL4YmAN Linux - Newbie 1 06-07-2004 01:32 PM
bash problem Vincent Slackware 2 10-30-2003 05:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 PM.

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