Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-02-2012
Registered User
 
Join Date: Feb 2012
Posts: 48
Thanks: 14
Thanked 0 Times in 0 Posts
Question Bash Positional Parameters Question

In a Bash script I used getopts command to let a user does something regards to the selected options. The question is: How do you find out what is the name of the file that user inserted in the command line like the following:
Quote:
$ ./myscriptname.bash -u username -t filename
The good part is this file is always the last argument in the command line and there won't be any switches after the file. Also, the name of the file may change and it is not fixed!

Last edited by bashily; 04-02-2012 at 09:12 PM.. Reason: Better explanation
Sponsored Links
    #2  
Old 04-02-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,661
Thanks: 166
Thanked 647 Times in 624 Posts
This is from tldp.org - based on your question history I would strongly advise you to read:

Advanced Bash-Scripting Guide

You can download a pdf from there. It is all free.

This is from the getopts page. Among lots of other things it explains getopts use of the : character


Code:
while getopts ":abcde:fg" Option
# Initial declaration.
# a, b, c, d, e, f, and g are the options (flags) expected.
# The : after option 'e' shows it will have an argument passed with it.
do
case $Option in
a ) # Do something with variable 'a'.
b ) # Do something with variable 'b'.
...
e) # Do something with 'e', and also with $OPTARG,
# which is the associated argument passed with option 'e'.
...
g ) # Do something with variable 'g'.
esac
done

$OPTARG would be the value of -t filename where "t:" exists in the the format string.
There is also an example of how to handle trailing arguments that are not options.
The Following User Says Thank You to jim mcnamara For This Useful Post:
bashily (04-02-2012)
Sponsored Links
    #3  
Old 04-02-2012
Registered User
 
Join Date: Feb 2012
Posts: 48
Thanks: 14
Thanked 0 Times in 0 Posts
Thanks so much. it was the greatest help ever! I got what I wanted. The source that you introduced was a fantastic bash scripting source.
Cheers
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Positional Parameters Straitsfan UNIX for Dummies Questions & Answers 1 08-24-2009 09:00 PM
Setting "default" positional parameters (in bash) pvdb Shell Programming and Scripting 2 06-18-2009 02:19 PM
Positional parameters shalu@ibm UNIX for Dummies Questions & Answers 2 11-22-2007 06:58 AM
Positional Parameters ericelysia UNIX for Dummies Questions & Answers 15 10-23-2005 03:11 PM
Positional Parameters google Shell Programming and Scripting 2 09-26-2003 01:51 PM



All times are GMT -4. The time now is 10:12 PM.