Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Using a variable in the exho of another variable

  1. #1
    Join Date
    Dec 2012
    Beans
    74

    Using a variable in the exho of another variable

    Hi, i know how to do this in DOS, and i'm hoping its possible in linux.


    file1=bob
    file2=joe
    file3=ken

    var=1

    Output needs to be echo $file1

    echo $file$var (unfortunately would give me 1)

    var=2

    echo $file$var

    Apologies if that makes no sense at all!!

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Using a variable in the exho of another variable

    I don't know exactly what you want to do. Maybe you can use a for-loop. If the names are files in the current directory, you can get them into the variable 'name' and get an associated counter 'i' like this. (Try it in a directory with rather few files!)

    Code:
     i=0;for name in *;do i=$((i=$i+1));echo -n "$i: "; echo $name;done
    You can find the following description of the for command in
    Code:
    man bash
    Code:
           for name [ in word ] ; do list ; done
                  The list of words following in is expanded, generating a list of
                  items.  The variable name is set to each element of this list in
                  turn,  and  list is executed each time.  If the in word is omit‐
                  ted, the for command executes  list  once  for  each  positional
                  parameter that is set (see PARAMETERS below).  The return status
                  is the exit status of the last command that  executes.   If  the
                  expansion of the items following in results in an empty list, no
                  commands are executed, and the return status is 0.
    
           for (( expr1 ; expr2 ; expr3 )) ; do list ; done
                  First, the arithmetic expression expr1 is evaluated according to
                  the  rules  described  below  under  ARITHMETIC EVALUATION.  The
                  arithmetic expression expr2 is then evaluated  repeatedly  until
                  it  evaluates  to zero.  Each time expr2 evaluates to a non-zero
                  value, list is executed and the arithmetic expression  expr3  is
                  evaluated.   If  any  expression is omitted, it behaves as if it
                  evaluates to 1.  The return value is the exit status of the last
                  command in list that is executed, or false if any of the expres‐
                  sions is invalid.
    Some more examples
    Code:
    for i in *.txt;do echo textfiles: $i;done
    
    for (( i=1; i<=20 ; i++ )) ; do echo $i;done
    
    for i in *.TXT;do mv $i ${i/\.TXT/}.txt; done  # "move *.TXT *.txt"

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,163
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Using a variable in the exho of another variable

    See you other question... this is a hack when you can't use arrays, but shell scripts can do arrays..

    And in fact in this specific case, you don't even need an array, a for loop on a list of files will work (and surprisingly, it would also be the right solution in a .BAT... see "HELP FOR")

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,340
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Using a variable in the exho of another variable

    Hi cbillson.

    There are two ways that I can think of:

    Using arrays (ofnuts's suggestion):
    Code:
    file[1]=bob
    file[2]=joe
    file[3]=ken
    
    var=1
    
    echo "${file[$var]}"
    Or using eval:
    Code:
    file1=bob
    file2=joe
    file3=ken
    
    var=1
    
    echo $(eval echo \$"file$var")
    Let us know how it goes.
    Regards.

  5. #5
    Join Date
    Dec 2012
    Beans
    74

    Re: Using a variable in the exho of another variable

    Quote Originally Posted by papibe View Post
    Hi cbillson.

    There are two ways that I can think of:

    Using arrays (ofnuts's suggestion):
    Code:
    file[1]=bob
    file[2]=joe
    file[3]=ken
    
    var=1
    
    echo "${file[$var]}"
    Or using eval:
    Code:
    file1=bob
    file2=joe
    file3=ken
    
    var=1
    
    echo $(eval echo \$"file$var")
    Let us know how it goes.
    Regards.
    you sir are a star..

    thank-you to everyone who advised, much appreciated

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,494
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Using a variable in the exho of another variable

    my advice regarding eval is forget you've seen it. If you find yourself using it, in 99.9 percent of cases you are doing it wrong.

    just stick to arrays.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,163
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Using a variable in the exho of another variable

    Quote Originally Posted by Vaphell View Post
    my advice regarding eval is forget you've seen it. If you find yourself using it, in 99.9 percent of cases you are doing it wrong.

    just stick to arrays.
    +2

  8. #8
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,340
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Using a variable in the exho of another variable

    Quote Originally Posted by Vaphell View Post
    If you find yourself using it, in 99.9 percent of cases you are doing it wrong. just stick to arrays.
    Quote Originally Posted by ofnuts View Post
    +2
    Oopsy

  9. #9
    Join Date
    Dec 2012
    Beans
    74

    Re: Using a variable in the exho of another variable

    ok, another question.. using the array with a for loop.

    normally a for loop will repeat until it runs out of things to repeat on, like if you're using a file (lines) or a directory (files).

    how does it work with an array, will it just stop when there is nothing more to increment?

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,494
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Using a variable in the exho of another variable

    it's works the same because you provide the for loop with a finite set of elements, once they are processed the loop exits.

    there are few ways you can iterate arrays

    Code:
    $ array=( a b c )  # standard integer indexed array, starting from 0
    $ declare -A asoc_arr # associative array indexed with strings
    $ asoc_arr=( [abc]=lol [def]=wut )
    
    $ for elem in "${array[@]}"; do echo "$elem"; done
    a
    b
    c
    
    $ for elem in "${asoc_arr[@]}"; do echo "$elem"; done
    wut
    lol
    works both with integer indexed and associative arrays, note that associative arrays don't guarantee order

    Code:
    $ echo "${!array[@]}"
    0 1 2
    $ for idx in "${!array[@]}"; do echo "array[$idx]: ${array[$idx]}"; done 
    array[0]: a
    array[1]: b
    array[2]: c
    
    $ echo "${!asoc_arr[@]}"
    def abc
    $ for idx in "${!asoc_arr[@]}"; do echo "asoc_arr[$idx]: ${asoc_arr[$idx]}"; done 
    asoc_arr[def]: wut
    asoc_arr[abc]: lol
    works both integer indexed and associative arrays, ${!array[@]} generates set of indices/keys found in the array

    Code:
    $ for (( i=0; i<${#array[@]}; i++ )); do echo "array[$i]: ${array[$i]}"; done
    array[0]: a
    array[1]: b
    array[2]: c
    C-like for loop, works only with integer indexed arrays, ${#array[@]} returns array size so the loop spans range of 0..ARRAY_SIZE-1
    Last edited by Vaphell; July 19th, 2013 at 04:54 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •