You are not logged in.

#1 2009-11-28 05:34:35

archnemesis
Member
Registered: 2009-11-27
Posts: 53

shell - how to use the output of one variable as name of another var?

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

#2 2009-11-28 06:00:43

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: shell - how to use the output of one variable as name of another var?

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

#3 2009-11-28 06:09:08

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: shell - how to use the output of one variable as name of another var?

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

#4 2009-11-28 06:45:22

archnemesis
Member
Registered: 2009-11-27
Posts: 53

Re: shell - how to use the output of one variable as name of another var?

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

#5 2009-11-28 06:58:28

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: shell - how to use the output of one variable as name of another var?

> 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

#6 2009-11-28 07:01:56

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: shell - how to use the output of one variable as name of another var?

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

#7 2009-11-28 08:41:51

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: shell - how to use the output of one variable as name of another var?

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

#8 2009-11-29 04:25:42

archnemesis
Member
Registered: 2009-11-27
Posts: 53

Re: shell - how to use the output of one variable as name of another var?

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

#9 2009-11-29 04:37:33

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: shell - how to use the output of one variable as name of another var?

> 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

#10 2009-11-30 06:50:00

archnemesis
Member
Registered: 2009-11-27
Posts: 53

Re: shell - how to use the output of one variable as name of another var?

Ah that is much better. I wasn't aware of what the $@ variable was until I just had a look now. Cheers.

Offline

#11 2009-11-30 15:48:31

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: shell - how to use the output of one variable as name of another var?

http://tldp.org/LDP/Bash-Beginners-Guid … 03_02.html
Go to "3.2.5. Special parameters" and pay attention to "$* vs. $@" note.

Offline

Board footer

Powered by FluxBB