LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-21-2011, 11:08 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
get n-variable parameters in bash


Hi,

I've a script that it's invoked with n-variable parameters. Here's an examples:

Code:
./myprogram.sh inputdir FIELD1 FIELD2 ... FIELDN outputfile
In the script I would like to get the FIELD names that were passed.

How can I do that in bash?
 
Old 03-21-2011, 11:17 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can try the shift statement inside a loop to cycle through all the positional parameters (arguments) independently from their number, e.g.
Code:
until [[ -z $1 ]]
do
  echo $1
  shift
done
the loop is executed until $1 is null. Please, see the Bash Reference Manual for more details.
 
Old 03-21-2011, 11:20 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
I've found it.
The command getopt worked for me.
 
Old 03-21-2011, 11:25 AM   #4
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-17.3 with Cinnamon & KDE
Posts: 1,530
Blog Entries: 3

Rep: Reputation: 103Reputation: 103
Given a command line:
Code:
prompt$ verb  option1  option2  ...  optionN
where is token is a group of characters separated by whitespace(blanks, tabs, etc)

You access them within your bash script as:
  • $0 -- the name of the "verb" (see more below)
  • $1 $2 ... $9 -- each option in order from left to right
  • $* -- all of the options as a single value
You may learn more at a terminal with
Code:
prompt$ man bash
and read about "Positional Parameters". NOTE -- 'man' launches the 'less' page reader.
Within less, type "/Positional" to search for that string.

If you have more than nine space-separated options, there is a built-in command, 'shift' to drag more options into place. Again 'man bash' is your friend.

The value of $0 is actually the full file path and name to the running script. So if you are running a script named "framis" that is stored in "/usr/local/bin", the verb will appear as "/usr/local/bin/framis".

Bonne chance,
~~~ 0;-Dan
 
Old 03-21-2011, 08:26 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Quote:
Originally Posted by SaintDanBert
If you have more than nine space-separated options, there is a built-in command, 'shift' to drag more options into place.
Actually this is not required, but of course still works. You can however simply use the following for greater than 9:
Code:
echo ${10}
 
  


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: How do I read data from a variable while that variable is being populated? theaceoffire Programming 4 04-23-2010 02:29 PM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
BASH: run command with parameters from variable Roger Krowiak Programming 3 10-30-2007 03:27 AM
C programming: Variable number of parameters to function kenneho Programming 8 03-21-2006 06:21 AM
Question about function with variable number of parameters xailer Programming 4 01-13-2004 11:37 AM


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