ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
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."
("${!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."
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 !
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.