LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-05-2009, 09:16 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
getting command line arguments in bash


I have a bash script as follows:

#!/bin/bash

ARG_DIR='-dir'
BASBUILD_DIR='BUILD'
BUILD_DIR=$BASBUILD_DIR
ARGCOUNT=1


if [ $# -ne 0 ]; then

for ARG in "$@"; do

case $ARG in
$ARG_DIR)
BUILD_DIR=$BASBUILD_DIR/$($ARGCOUNT)
echo "$BUILD_DIR"
;;
esac

ARGCOUNT=$(($ARGCOUNT+1))

done
fi

It is suppose to print the command-line argument following the command-line argument "-dir-". For example:

sh myscript.sh -dir meow

Should print meow. However it just complain "bash: 1: command not found" which is the value of 'ARGCOUNT'.

How do I get the next argument from the for loop?
 
Old 05-05-2009, 09:38 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The error is due to this expression which is a non-sense:
Code:
$($ARGCOUNT)
this is command substitution where the command is $ARGCOUNT, that is 1. The shell tries to execute the command 1 which obviously is not found. You want indirect variable reference instead, to retrieve the value of $1:
Code:
${!ARGCOUNT}
Furthermore a better result can be achieved using the shift built-in, which shifts the positional parameters by one every time it is executed (the second becomes the first, the third the second and so on...). Useful in a loop where you have to process the arguments one at a time.
 
Old 05-05-2009, 10:24 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Here's an alternative, which employs getopt.

Code:
#!/bin/sh

set -e

OPTS=`getopt -n $0 -o d: --long dir: -- $@`

eval set -- "$OPTS"

while true
do
        case "$1" in
                -d|--dir)  dir=$2; shift 2 ;;
                --)        shift ; break ;;
        esac
done

echo $dir    # for debug
Usage:
Code:
sh myscript.sh -d meow
sh myscript.sh --dir meow
 
Old 05-05-2009, 01:39 PM   #4
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Great! Thanks for your answers.
 
Old 05-06-2009, 12:50 PM   #5
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
I just love the fact that people here help and get help ... Rock On !!!


Debian

Last edited by amysaraantony; 05-15-2009 at 09:13 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 command line arguments msreddy999 Programming 1 03-20-2009 03:56 PM
command line arguments nickraj Programming 6 09-11-2006 02:01 PM
Command line arguments?? almagerenia Linux - Newbie 1 09-08-2006 05:05 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 09:43 AM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 03:25 AM


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