Help parsing command line arguments in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help parsing command line arguments in bash
# 1  
Old 07-14-2009
Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it.

At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the current directory, and that there is only one argument.

Here is the code I am using for this task now, but it does not produce the result I need:

Code:
if [ -f $1 -a $# -eq 1 ]

Thanks!
# 2  
Old 07-14-2009
Quote:
Originally Posted by Breakology
Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it.

At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the current directory, and that there is only one argument.

Here is the code I am using for this task now, but it does not produce the result I need:

Code:
if [ -f $1 -a $# -eq 1 ]

Thanks!

what is the message your are getting?
Have you echo'ed the values before the "if" ?
# 3  
Old 07-14-2009
Posix-sh, ksh, bash, ...
Code:
[  $#  -lt 1 ]  &&  echo "need arg" && exit 1
[ ! -f  "$1" ]   &&  echo "file $1 must exist"  && exit 1

Your version [ -f $1 -a $# -eq 1 ] give error if you don't give any arguments, because $1 is empty, but if you put -f "$1" then value is some string, maybe very short (len 0).
Why ? Read full story
# 4  
Old 07-14-2009
!/bin/bash -x

returns:

+ '[' -f filename -a 1 -eq 1 ']'
++ cut -d: -f3
++ sort -r
++ head -n1

---------- Post updated at 06:00 PM ---------- Previous update was at 05:08 PM ----------

ahhhh, thanks guys, I figured it out, problem was elsewhere.

Last edited by Breakology; 07-14-2009 at 06:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing Command Line Arguments In C shell script

]I have a string like "/abc/cmind/def/pq/IC.2.4.6_main.64b/lnx86" and this string is given by user. But in this string instead of 64b user may passed 32 b an i need to parse this string and check wether its is 32b or 64 b and according to it i want to set appropriate flags. How will i do this... (11 Replies)
Discussion started by: codecatcher
11 Replies

2. Shell Programming and Scripting

Wanted: Help with escaping bash command line arguments

Please forgive me if this is the wrong forum. I want to execute some one liners with the groovy programming language and I'm having trouble escaping the special characters to accommodate bash. Here is one of the lines that is giving me trouble: groovy -e "(new... (1 Reply)
Discussion started by: siegfried
1 Replies

3. UNIX for Dummies Questions & Answers

Accepting command line arguments in bash

I have this tcsh code that I want to convert to a bash script. Basically it accepts command line arguments supplied by the user and stores them, so they can be used to run a C++ program. set arg_browseDir_inFileLst = "" set allArgsUpCase = `echo "$*" | tr '' ''` set opt_browseDir_flag =... (17 Replies)
Discussion started by: kristinu
17 Replies

4. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

5. Programming

Parsing command line arguments in Python

Hi, I've a python script called aaa.py and passing an command line option " -a" to the script like, ./aaa.py -a & Inside the script if the -a option is given I do some operation if not something else. code looks like ./aaa.py -a . . if options.a ---some operation--- if not options.a... (1 Reply)
Discussion started by: testin
1 Replies

6. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

7. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

8. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

9. Shell Programming and Scripting

Bash: Reading 2 arguments from a command line

If no arguments are entered I wanna be able to read 2 arguments, i have done like this but it doesnt work: x=0 until #loop starts do if ; then echo No arguments were entered, please enter 2 arguments. read $1 $2 elif || ; then echo $#... (0 Replies)
Discussion started by: Vozx
0 Replies

10. UNIX for Advanced & Expert Users

Parsing the command line arguments

Is there a way to get the command line arguments. I am using getopt(3) but if the arguments are more than one for a particular option than it just ignores the second argument. For eg ./a.out -x abc def now abd will be got with -x using getopt "( x : )" and string abc\0def will get stored... (7 Replies)
Discussion started by: jayakhanna
7 Replies
Login or Register to Ask a Question