LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-10-2014, 09:57 AM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
Bash wildcard in argument


Ive been having trouble getting my bash script to accept wildcards when there are multiple arguments, and am just trying to use this with an ls as described below:

Say I have these 4 files in the directory
Code:
abc123.txt
abc456.txt
xyz789.txt
123xyz.txt
I essentially have a script like this called test_script
Code:
arg1=$1
arg2=$2 
ls $arg1 > chk1.txt
ls $arg2 > chk2.txt
I can get the results I want to obtain by running the script as:
Code:
test_script "*abc*" "*xyz*"
However I need others to use this scripts and normally we dont need to quote things in order to get the wildcards to work

I have tried using $@ (although I really dont know what this does), but can only seem to get that to work properly if there is only one argument given.


Any advice?

Thanks
 
Old 04-10-2014, 11:29 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The wildcards, unless quoted, are being expanded by the user's shell. Your script never sees them, just the multiple file names that result from the expansion. In your case, that can't work since there is no way for the script to know where the first group ends and the next begins, so there's really no good alternative to quoting.
 
Old 04-10-2014, 11:36 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
What you're trying to do is prevent bash from doing what it normally does. I think there is no way prevent that.

Preventing Wildcard Expansion / Globbing in Shell Scripts seems to have identified one version of the least objectionable workaround.
 
Old 04-10-2014, 02:22 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
However I need others to use this scripts and normally we dont need to quote things in order to get the wildcards to work
Actually that is false, try using find with an asterisk that is unquoted and see if you get the results you want.

Also, it may serve as a good lesson to the 'others' that quoting is in fact necessary for some command / scripts to work correctly.
 
  


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
How do I pass wildcard as an argument to a shell script? Jykke Programming 8 03-09-2013 10:33 AM
bash string comparisons wildcard casperdaghost Programming 5 08-10-2012 03:30 AM
[SOLVED] BASH Wildcard * and ? cwilliams6419 Linux - Newbie 4 02-24-2012 12:19 PM
bash wildcard expansion length issue scazz Linux - Newbie 2 11-01-2011 12:32 PM
Wildcard/regex in bash command logicalfuzz Linux - General 2 10-27-2011 02:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:22 AM.

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