You are not logged in.
I simply want to use the output of the $# number of arguments var as the name of another var like this:
while [ $# -gt 0 ]; do
if [ ${$#} = k ]; then
do stuff
else if [ ${$#} = l ]; then
do stuff
else if [ ${$#} = m ]; then
do stuff
fi
shift
done
so but i'm not sure how to do it. obviously the ${$#} must not be the right way.
cheers
Offline
Are you sure simply '$#' won't work?
Assuming 'while [ $# -gt 0 ]; do' works, I see no reason why can't you use '$#' later on.
Lemme see ...
[BRB, it's 7 A.M., me needs coffee]
Edit:
case $# in
"0") echo "0" ;;
"1") echo "1" ;;
"2") echo "2" ;;
*) echo "Fail"
esac
Seems to work.
http://tldp.org/LDP/Bash-Beginners-Guid … 07_03.html
Last edited by karol (2009-11-28 06:08:34)
Offline
Case statement works a lot better than an if/else tree in Bash. Use something like this:
#!/bin/bash
while [[ $# -gt 0 ]]; do
case $# in
3) echo "3 args" ;;
2) echo "2 args" ;;
1) echo "1 arg" ;;
esac
shift
done
edit: beaten to it, oy.
Last edited by falconindy (2009-11-28 06:09:31)
Offline
But i want to use the output of $# to select the argument to test such as $1 $2 $3 $4 etc. So if $# equals 4, I want to test argument 4 i.e. $4 to see if it matches certain strings. For example if 'myscript' is invoked with arguments 'cat' 'dog' 'log' like so:
$ myscript cat dog log
I want to use $# to run through each argument cat, dog, and log and test if they matches certain strings.
Offline
> So if $# equals 4, I want to test argument 4
case $# in
4) echo "$4" ;;
You can use any command instead of 'echo':
case "$dst" in
*.tbz2|*.tar.bz2) tar cvpjf "$dst" "${srcs[@]}" ;;
*.tgz|*.tar.gz) tar cvpzf "$dst" "${srcs[@]}" ;;
*.gz) cat "${srcs[@]}" | gzip > "$dst" ;;
*.bz2) cat "${srcs[@]}" | bzip2 > "$dst" ;;
*.zip) zip "$dst" "${srcs[@]}" ;;
*.7z) 7z -t7z "$dst" "${srcs[@]}" ;;
*) errorout "invalid archive: $dst" ;;
esac
[stolen from http://pbrisbin.com:8080/bin/archive ]
If you prefer multiple 'ifs' rather than 'case', you can also use it.
As for the question, how to refer to the number of your variables, it's '$#'.
Edit: And there's also 'getopts':
while getopts "t:c:s:b:a:" options; do
case $options in
t ) TO=$OPTARG;;
c ) CC=$OPTARG;;
s ) SUBJECT=$OPTARG;;
b ) BODY=$OPTARG;;
a ) ATTACHMENT=$OPTARG;;
\? ) echo $USAGE
exit 1;;
* ) echo $USAGE
exit 1;;
esac
[stolen from http://bbs.archlinux.org/viewtopic.php? … 00#p431800 ]
Last edited by karol (2009-11-28 07:07:48)
Offline
You can use eval but that's really not the best way to do what you seem to want to do. When you use the shift statement, you're renumbering the vars. For example, if you have 0 1 2 3 4, shift changes it to 0 1 2 3. That way you can just $1 and walk through each of your arguments. But what you are doing is backwards and completely pointless. So here's what you ought to try:
while [ $# -gt 0 ]; do
if [ $1 = k ]; then
do stuff
else if [ $1 = l ]; then
do stuff
else if [ $1 = m ]; then
do stuff
fi
shift
done
aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies
Offline
fsckd is right. Here is an example that also uses the indirect reference:
--> testir() { echo LAST ARG IS: ${!#}; shift; echo LAST ARG IS: ${!#}; }
--> testir a b c d e f
LAST ARG IS: f
LAST ARG IS: f
Offline
Thanks for the help. How do these look:
#!/bin/bash
for ((i=$#; i>0; i--)); do
if [ ${!i} = f ]; then prog1 &
elif [ ${!i} = m ]; then prog2 &
elif [ ${!i} = t ]; then prog3 &
fi
done
testop()
{
if [ $1 = f ]; then prog1 &
elif [ $1 = m ]; then prog2 &
elif [ $1 = t ]; then prog3 &
fi
}
i=$#
while [ $i -gt 0 ]; do
case $i in
5) testop $5 ;;
4) testop $4 ;;
3) testop $3 ;;
2) testop $2 ;;
1) testop $1 ;;
*) echo "too many args" ;;
esac
i=$(($i-1))
done
They both work, but is there any obvious noob mistakes/sillyness?
cheers
Offline
> They both work, but is there any obvious noob mistakes/sillyness?
I have no idea what are you trying to do with this code ...
for i in $@
do
case $i in
"0") echo "0" ;;
"1") echo "1" ;;
"2") echo "2" ;;
*) echo "Fail"
esac
done
This will check if there's a 0, 1 or 2 in the args list and act accordingly.
Last edited by karol (2009-11-29 04:50:15)
Offline
Ah that is much better. I wasn't aware of what the $@ variable was until I just had a look now. Cheers.
Offline
http://tldp.org/LDP/Bash-Beginners-Guid … 03_02.html
Go to "3.2.5. Special parameters" and pay attention to "$* vs. $@" note.
Offline