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 09-27-2005, 01:57 PM   #1
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
processing command args in bash


#!/bin/bash noob here.

i'm comfortable with C but i think some simpler tasks on my system will be more easily achived with bash.
I'd also like to getter a better grasp of all my config files and initialisation scripts so here i go..
Code:
if [ "$#" -eq 0 ];then
	echo "pls state the name of the file "
	else
	echo "on we go" 
	torrent=$#
	echo $torrent # i wanted this to print out the name of the parsed cmd line arg 0:(
	echo $1          #this obviously printed the name of the file i parsed at cmd line
	fi
	if [ -f $torrent ];then 
	echo "the file is here"
	else
	echo "no such file"
	fi
pls see the comments in the middle of the code.
the program will process args of an indeterminate number but it will stipulate what
the last arg will be and thus i need acces to it,i.e test that it is there etc

i cant code [ -f $n .... where n is some integer value
because i will not know what the integer value will be ahead of time.
how do i achieve this pls?
 
Old 09-27-2005, 04:21 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
i cant code [ -f $n .... where n is some integer value
because i will not know what the integer value will be ahead of time.
how do i achieve this pls?
Code:
#!/bin/bash

# Processing of command line arguments in bash
# "shift" shifts all arguments to the left ($2=$1, $3=$2 etc.)
# "shift" returns non-zero exit-code (indicating "false")
# when no more arguments available.
# (note that $0 will not get "shifted").


ARGV0=$0 # First argument is shell command (as in C)
echo "Command: $ARGV0"

ARGC=$#  # Number of args, not counting $0
echo "Number of args: $ARGC"

i=1  # Used as argument index
while true; do
	if [ $1 ]; then
		echo "Argv[$i] = $1" # Always print $1, shifting $2 into $1 later
		shift
	else
		break # Stop loop when no more args.
	fi
	i=$((i+1))
done
echo "Done."
 
Old 09-27-2005, 04:30 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Or, without "shift":

("${!i}" is actually the answer to your question "i cant code [ -f $n .... where n is some integer value")

Code:
#!/bin/bash

# Or, without "shift": use loop counter until $# ( = number of args)

ARGV0=$0 # First argument is shell command (as in C)
echo "Command: $ARGV0"

ARGC=$#  # Number of args, not counting $0
echo "Number of args: $ARGC"

i=1  # Used as argument index
while [ $i -le $ARGC ]; do # "-le" means "less or equal", see "man test".
	# "${!i} "expands" (resolves) to $1, $2,.. by first expanding i, and
	# using the result (1,2,3..) as a variable which is then expanded.
	echo "Argv[$i] = ${!i}"
	i=$((i+1))
done
echo "Done."

Last edited by Hko; 09-27-2005 at 04:39 PM.
 
Old 09-27-2005, 05:41 PM   #4
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Hey Slzckboy,

Didn't quite understand what you were trying to do. The first comment seemed to be confusing. But I guess your actual problem was parsing the last argument. Yes, the solution to that is using 'shift' or "${!i}" as Hko suggested. I guess you wanted this :
a) Invoke the script with n no. of arguments,
b) If n = 0 ask the user to input the name of the file, else
c) if n != 0 assume that the last argument is the name of a file, and check to see if that file exists or not.

Here's how the above script would look with shift :
Code:
#!/bin/bash
        if [ "$#" -eq 0 ];then
	echo "pls state the name of the file "
	
	
	
	else
		echo "on we go" 
				
			while [ "$2" != "" ]; do
			shift
			done
				
		if [ -f $1 ];then 
		echo "Searching for file $1 : the file is here"
		else
		echo "Searching for file $1 : no such file"
		fi

	fi

exit 0
If there's something more you wanted please state that.
Hope this helps !
 
Old 09-28-2005, 03:20 AM   #5
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
no..thats fine . That is exactly what i meant.


thnks
 
Old 09-28-2005, 04:36 AM   #6
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
i think i will go the non "shift" way or else I will loose the data passed in on the previous args.!!?!
 
Old 09-28-2005, 08:00 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
for parsing of command lines with flags,
man getopts

make life easy for yourself!

e.g parsing -d and -f flags, where -f requires an argument:

Code:
    while getopts df: flag
    do
        case $flag in

            d)
                  echo debugging on
                  ;;
            f)
                 file=$OPTARG
                 echo filename is $file
                 ;;
            ?)
                exit
                ;;
        esac
   done
shift $(( OPTIND - 1 ))  # shift past the last flag or argument

echo parameters are $*



Result
Code:
$ bash opt.sh -df  blah one two three
debugging on
filename is blah
parameters are one two three

$ bash opt.sh -f 
opt.sh: option requires an argument -- f

$ bash opt.sh -r 
opt.sh: illegal option -- r

Last edited by bigearsbilly; 09-28-2005 at 08:01 AM.
 
Old 09-28-2005, 10:51 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by slzckboy
i think i will go the non "shift" way or else I will loose the data passed in on the previous args.!!?!
...unless you store them in shell-variables of course.
 
Old 09-28-2005, 11:04 AM   #9
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
hmm...

ok..


well i'd value an opinion on what I have done thus far

i'm getting
"parameter passed in at end with no value" spat back at me.

Code:
#!/bin/sh

	argv[0]=$0
	bittorrent="btdownloadheadless.py"
	#   set default values
	up_speed=2
	min_port=49152
	max_port=65535
	
	if [ "$#" -eq 0 ];then
	echo "pls state the name of the file"
	exit 0
	else
	echo "on we go"
	torrent=$#
	fi

	i=1	
	if  [ -f "${!torrent}" ]; then
	echo "${!torrent} file found"
	while [ $i -lt $#  ];do
	case "${!i}" in
	"--max_upload_rate")
	i=$((i+1))
	up_speed=${!i};;
	esac
	i=$((i+1))
	done

	exec /usr/local/bin/$bittorrent --max_upload_rate=$up_speed		
	
	else
	echo "${!torrent} not found"
	fi

basically wots wrong with doing.....
exec /usr/local/bin/$bittorrent --max_upload_rate=$up_speed ????

i tried "man getopts" but strangely the man page is not on my system.
will have to google 4 it.
 
Old 09-28-2005, 02:06 PM   #10
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 32
Quote:
Originally posted by slzckboy

i tried "man getopts" but strangely the man page is not on my system.
will have to google 4 it.
Try "man getopt"
 
Old 09-28-2005, 04:49 PM   #11
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Original Poster
Rep: Reputation: 30
hey ..

thnks 4 everyones help.

i have a working base now that I can expand on.
I have worked out the bugs in the above posted code
No need for getopts for now but I will keep it in mind for later use.
 
  


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
processing a ksh shell in bash environment ntoughe Programming 9 09-12-2005 05:09 AM
newbe bash question ( grep processing) therealbxp Programming 2 11-20-2004 07:40 AM
command line args in a countdown Squeak2704 Programming 1 04-26-2004 12:27 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM
>>> control mplayer with command args??? nibjb Linux - Software 0 07-16-2003 11:09 PM


All times are GMT -5. The time now is 05:51 AM.

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