Results 1 to 4 of 4

Thread: [SOLVED] Bash Variable from Data Question

  1. #1
    Join Date
    Mar 2008
    Location
    Somerset, NJ
    Beans
    147
    Distro
    Ubuntu 11.04 Natty Narwhal

    [SOLVED] Bash Variable from Data Question

    Hi-

    I'm creating a shell script that will be taking in data and I want to create separate variables that contain the name of the data taken in.

    for instance, if the data obtained is in a variable called $var1 = "txt", I want to create a new variable named "txt_count" somehow using $var1. I am also wondering if I can later test these variables in an if statement, using the same kind of concept.

    Something along the lines of $var1_count.

    I can't just use an if statement, as I am dealing with a lot of data that I want to do this for.

    For instance, in the vain of filenames, if I had txt, jpg, ogg, cpp, I'd want to create corresponding variables txt_count, jpg_count, ogg_count, and cpp_count [except that I am doing this with over 100 pieces of data]. And then later, I want to be able to say, based on having only this $var1 [which could be equal to txt, jpg, ogg, or cpp], and say does $var1_count exist in an if statement or something along those lines.

    I'm not sure if this is possible in bash.

  2. #2
    Join Date
    Mar 2008
    Location
    Somerset, NJ
    Beans
    147
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bash Variable from Data Question

    note, something like this:

    Code:
    var="0"
    "$var"_count="2"
    echo $var_count
    yields

    Code:
     0_count=2: command not found
    I suppose an easier problem to tackle would be can I create variables like this?

    If I have a file containing:
    Code:
    txt
    cpp
    ogg
    And I read those [individually] off of the file into a variable called $ftype, could I then make a new variable which is the basically equal to the value of $ftype. Basically, it's like psuedo-dynamically creating variables. Is that possible?
    Last edited by melrom; December 4th, 2008 at 11:52 PM.

  3. #3
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash Variable from Data Question

    Possible, yes, but you really should avoid such programming. Instead, you could use arrays for example. Here's a small bash script checking all files in the current directory, counting the number of files with the same extensions:

    Code:
    #!/bin/bash
    
    # Declare $extensions and $counts to be arrays. Not really necessary, but it
    # shows that you intend these to be arrays.
    declare -a extensions
    declare -a counts
    
    for file in *.*; do
        # Set $ext to $file with everything up to the last '.' removed.
        ext="${file##*.}"
    
        # Check if $extensions has this extension already and increment the count
        # for that extension if that is the case
        for ((i=0; i < ${#extensions[*]}; i++)); do
            if [ "$ext" = "${extensions[i]}" ]; then
                ((counts[i]++))
                break
            fi
        done
        # If $i equals the size of the array, there wasn't an entry for that
        # extension already, so we add a new element to the arrays.
        if [ $i -eq ${#extensions[*]} ]; then
            extensions[i]="$ext"
            counts[i]=1
        fi
    done
    
    # Print the results
    for ((i=0; i < ${#extensions[*]}; i++)); do
        echo "There are ${counts[i]} files with '${extensions[i]}' extension"
    done
    To do what you originally wanted, you'd need to use eval
    Code:
    $ var=txt
    $ eval ${var}_count=2
    $ echo $txt_count
    2

  4. #4
    Join Date
    Mar 2008
    Location
    Somerset, NJ
    Beans
    147
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bash Variable from Data Question

    Yes, I was thinking about using arrays. Thanks for both tips! =)

Tags for this Thread

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
  •