LinuxQuestions.org
Review your favorite Linux distribution.
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-23-2009, 11:36 AM   #1
bashuser
LQ Newbie
 
Registered: Apr 2009
Posts: 4

Rep: Reputation: 0
How can i work with optional parameters separated by '-'


Okay the title is not very good so i will explain(sorry for my english i am from a galaxy far far away )
Let's suppose i have a dir named MainBla that contains four dirs named bla1 bla2 bla3 bla4 and each one of these dirs has a file(i believe it's the same procedure if each one of the bla dirs contains other dirs that contain files etc).
I am writing a script which takes optional parameters:
$1: the path that MainBla is found
$2: one of the bla folders in order to do whatever my script does with the file that bla contains.

So one call might be ./script.sh /..../MainBla bla1.
Let's suppose now that i want to change that into
./script /.../MainBla bla1-bla3...in that case $2 argument is the range of dirs i want to work with(bla1,bla2,bla3 but not bla4).how can i make the script change from bla1 to bla2 and then to bla3?
(this can be expanded for random number of dirs)
again excuse me for my poor english.if you have any questions please ask me.
Thank you
 
Old 04-23-2009, 12:11 PM   #2
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
I would do it in a different way, an easier way.

The shell can expand parameters such as bla{1..3} to 3 parameter: bla1 bla2 bla3. If you call your script with bla{1..3} instead of bla1-bla3, then your work is already done for you. In your script you can access the command line parameters as $2 $3 $4 etc... You can loop over all of them with a code like this:

shift
for bla in $@
do
echo $bla
done

This is how I run it, and the output I get:
./bla.sh blaMain bla{1..3}
bla1
bla2
bla3


Note that '$@' is equivalent to '$1 $2 $3'... which is why I call 'shift' in the beginning to shift the parameters 1 place left ($2 will become $1, $3 will become $2 and so on...). You can also know how many parameters your script got on the command line with $#.
 
Old 04-23-2009, 12:18 PM   #3
bashuser
LQ Newbie
 
Registered: Apr 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Udi View Post
I would do it in a different way, an easier way.

The shell can expand parameters such as bla{1..3} to 3 parameter: bla1 bla2 bla3. If you call your script with bla{1..3} instead of bla1-bla3, then your work is already done for you. In your script you can access the command line parameters as $2 $3 $4 etc... You can loop over all of them with a code like this:

shift
for bla in $@
do
echo $bla
done

This is how I run it, and the output I get:
./bla.sh blaMain bla{1..3}
bla1
bla2
bla3


Note that '$@' is equivalent to '$1 $2 $3'... which is why I call 'shift' in the beginning to shift the parameters 1 place left ($2 will become $1, $3 will become $2 and so on...). You can also know how many parameters your script got on the command line with $#.
thank you very much for your answer.the truth is i already know that procedure,so i am not interested in the easiest way but in the particular one i am describing above,which i think needs string manipulation...if you have any suggestion on that,pls tell me.thnks again!


//i think i have some progress here.when i finish i will write my results

Last edited by bashuser; 04-23-2009 at 12:54 PM. Reason: extra explanation
 
Old 04-23-2009, 02:17 PM   #4
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137

Rep: Reputation: 22
Post

What about google?

http://tldp.org/LDP/abs/html/string-manipulation.html

(Or in other words, what have you tried, already?)
 
Old 04-23-2009, 02:21 PM   #5
bashuser
LQ Newbie
 
Registered: Apr 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by synss View Post
What about google?

http://tldp.org/LDP/abs/html/string-manipulation.html

(Or in other words, what have you tried, already?)
you are not gonna believe this.i was just checking that page!i managed to separate the parameter bla1-bla3 in bla1 and bla3.then i create a path with each one of these parameters...now i am thinking how i am going to make the script change from bla1 to bla2 and then to the upper bound which is bla3...
 
Old 04-23-2009, 04:55 PM   #6
bashuser
LQ Newbie
 
Registered: Apr 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Problem solved:

First i define which will be the lower bound and the upper bound from the dirs included in MainBla

MainBla=$1
bla_general=$MainBla"/"$2
string=$2
bla_lower_bound=$MainBla"/"${string%-**}
bla_upper_bound=$MainBla"/"${string#**'-'}


for lalala in $( \ls $MainBla)
do
bla_lower_bound=$MainBla"/"$lalala
if [[ "$bla_lower_bound" < "$bla_upper_bound" ]]||[[ $bla_lower_bound == "$bla_upper_bound" ]]
then
cd $bla_lower_bound

And after this i begin a second "for" that gets into the specific dir that i want to work every time...

Last edited by bashuser; 04-23-2009 at 04:56 PM.
 
  


Reply


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
bash script - parsing optional parameters yitzle Programming 5 02-17-2008 11:16 AM
2 NICs - 2 Separated Network -- Help! pacini Linux - Networking 3 12-07-2007 02:44 PM
MPlayer won't work with certain parameters Xplosive Linux - Software 1 09-24-2005 09:36 PM
Optional parameters in C++ Templates? enemorales Programming 5 04-22-2005 04:40 PM
separated at birth The Bad Penny General 1 11-10-2004 09:12 AM


All times are GMT -5. The time now is 06:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration