LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-23-2015, 08:57 PM   #1
gdizzle
Member
 
Registered: Jul 2012
Posts: 234

Rep: Reputation: Disabled
Postional parameter with e.g: /etc/*


Hi the script works below if you run:

Code:
./symbol.sh /etc
However it does not run if you use:


Code:
./symbol.sh /etc/*
I think it's because posparam $1 does not know how to interpret
Code:
"/*"
Or /* is intrpretted as $2?

Any ideas?

Thanks


Code:
# Check for symbol

# At least 1 posparam

if [[ $# != 1 ]]
then
        echo "Usage: $0 <path>"
        exit 190
fi

#Check if the string contains a *

set -x

#Write out to a file for ease

echo "$1" > /tmp/file.$$ 2>/tmp/file.0.$$
grep '*' /tmp/file.$$ >/tmp/file0.$$

if [[ "$?" -eq 1 ]]
then
        echo "Required i.e. $1/*"
else
        echo "Symbol is NOT required"
fi


# Remove the files

rm -f /tmp/file.$$ /tmp/file0.$$
 
Old 05-23-2015, 09:21 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,345

Rep: Reputation: Disabled
If you run:
Code:
./symbol.sh /etc/*
...the shell will expand "/etc/*" before passing the results as parameters to symbol.sh.

For instance, if the /etc directory contains the files foo, bar and baz, the actual command line will be:
Code:
./symbol.sh /etc/foo /etc/bar /etc/baz
So while $1 will contain /etc/foo, you'll find /etc/bar in $2, /etc/baz in $3 and so on.
 
Old 05-24-2015, 03:28 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
if you really want to get hold of the "*", you have to use something like
Code:
'/etc/*'
# or
/etc/\*
but why would you want that?
 
Old 05-24-2015, 07:52 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Try to debug your script...
for example command echo "$@" prints the arguments, but you won't see the boundaries.
Another method:
Code:
for arg in "$@"; do printf '\t"%s"\n' "$arg"; done
 
  


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
in with a parameter? jncunliffe Linux - Newbie 6 01-06-2011 04:23 PM
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
Change postional parameter mamtasahai1 Linux - General 2 06-10-2008 07:28 PM
learning sed,awk,postional parameter and functions yawe_frek Programming 3 09-08-2007 08:49 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM

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

All times are GMT -5. The time now is 12:54 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