LinuxQuestions.org
Review your favorite Linux distribution.
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 12-12-2005, 03:36 PM   #1
merchtemeagle
Member
 
Registered: Oct 2004
Location: Belgium
Distribution: Slackware 13.37
Posts: 512

Rep: Reputation: 31
flags vs normal arguments


I'm writing a bash script that takes optional user input, a pathname. If it is not specified "pwd" will be used. But what is the best way to do this?

Code:
$command path
or

Code:
$command -d path
 
Old 12-12-2005, 03:48 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
If a path is necessary but simply has a default, it is best not for it to be an option e.g. find command.

Last edited by tuxdev; 12-12-2005 at 03:49 PM.
 
Old 12-12-2005, 04:56 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You might consider using the bash built-in "getopts". It works like the C language function of the same name. If an option requires an argument, a semicolon will follow the option letter in the option string. Then you can simply read the $OPTARG variable for the value of the argument. Typing in "help getopts" in the bash shell will give you some information. Also detailed information should be in the bashref manual.

Your question reminds me of the optional destination directory option of the "mv" command, i.e. the '-t' option, which allows its use with the "xargs" command.

Last edited by jschiwal; 12-12-2005 at 05:04 PM.
 
Old 12-12-2005, 04:58 PM   #4
merchtemeagle
Member
 
Registered: Oct 2004
Location: Belgium
Distribution: Slackware 13.37
Posts: 512

Original Poster
Rep: Reputation: 31
Yes, I already looked at 'getopts'. But it only takes care of arguments with a trailing '-' (flags), I thought?
 
Old 12-12-2005, 05:16 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If you have regular arguments, rather than option arguments, you can use the value of $OPTIND to determine where to start regular processing. If you will only have one optional command argument, then you don't need to use options. Instead check for the existence of an argument, if one doesn't exist, i.e. $# is 0, then use $PWD; otherwise if $# is 1, use the supplied argument.
 
Old 12-12-2005, 05:26 PM   #6
merchtemeagle
Member
 
Registered: Oct 2004
Location: Belgium
Distribution: Slackware 13.37
Posts: 512

Original Poster
Rep: Reputation: 31
This is what I've done:

Code:
#! /bin/bash

TOCHECK=$(pwd)

if [ $# -le 1 ]; then
    if [ -n "$1" ]; then
        TOCHECK=$1
    fi
else
    printf "Usage: %s [path]\n"  $0
    exit 1
fi
Good?
 
Old 12-12-2005, 09:38 PM   #7
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
Cool

if you want to use flags and arguments all you have to do if find out how many arguments were sent into the script and use a case block to execute what you want

Code:
case "$number_of_args" in
	1)echo "this executes if 0 flags"
	;;
	1)echo "this executes if 1 flags"
	;;
	2)echo "in here you could put like another case, lets say you have"
	echo "2 flags and now you know that the first has to be the flag"
	echo "and the other has to be the pathname"
	echo "then you do another case, instead of all this text"
	case $1 in
		-d)case $2 in
		echo "do whatever you want here"
		;;
		*) echo "invalid input"
		exit 1
		;;
	;;
	...
	...
	...
	etc) you get it
esac
exit 0
hope i helped somehow

peace out, later
 
Old 12-14-2005, 10:20 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
The code you posted works. If the path must exist ahead of time, consider using the "-d" test:

if [ -d "$1" ]; then

This will test whether the path name in the arguments exists, and whether it is a directory.
 
  


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
Recommended USE flags? ssimontis Linux - Newbie 5 02-14-2005 11:14 PM
CC flags and the like Garp Linux - Software 2 05-17-2004 03:48 AM
rpmbuild flags vejnovic Linux - Software 0 05-03-2004 07:22 AM
lilo flags german Linux - General 3 04-15-2004 08:07 AM
some route flags Cubiq Linux - Networking 5 08-16-2002 11:37 AM


All times are GMT -5. The time now is 03:36 PM.

Main Menu
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