Find the answer to your Linux question:
Results 1 to 3 of 3
I don't really know much about programming/scripting that much. I had one programming class once. I am trying to get the following to work, but it only works with numerical ...
  1. #1
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55

    Double variable -- I think

    I don't really know much about programming/scripting that much. I had one programming class once. I am trying to get the following to work, but it only works with numerical numbers, and fails with text. How can I get the text portion to work?:


    PHP Code:
    #!/bin/bash

    LARGE_FILE_MAX_SIZE=5
    LARGE_FILE_TYPE
    =GB

    convert_data 
    (){
            
    CONVERT_TYPE=$((${1}_FILE_TYPE))
            
    CONVERT_TOTAL=$((${1}_FILE_MAX_SIZE))
            echo 
    CONVERT_TYPE:  $CONVERT_TYPE
            
    echo CONVERT_TOTAL$CONVERT_TOTAL
    }

    convert_data LARGE 
    The above returns the following:
    # ./data_gen.sh
    CONVERT_TYPE: 0 <--- I need this to read GB (or whatever string is passed in)
    CONVERT_TOTAL: 5

    Thanks in advance for your help

    BTW: I am not a student and this isn't homework. It is a script I am trying to refine that will generate large amounts of custom data for some of the testing I have to do. My current script generates the data, but I am refining it to give me more control of the size of data it will generate (this is only a small portion of the overall script)

  2. #2
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    #!/bin/bash 
     
    LARGE_FILE_MAX_SIZE=2
    LARGE_FILE_TYPE=GB 
     
    convert_data (){ 
            CONVERT_TYPE=${1}_FILE_TYPE
            CONVERT_TOTAL=$((${1}_FILE_MAX_SIZE))
            echo CONVERT_TYPE:  ${!CONVERT_TYPE}
            echo CONVERT_TOTAL: $CONVERT_TOTAL
    } 
     
    convert_data LARGE
    CONVERT_TYPE=$((${1}_FILE_TYPE))
    will always returm 0 as the resulting variable contains a non numeric value.
    ${1}_FILE_TYPE
    becomes the the value "LARGE_FILE_TYPE" contained in "CONVERT_TYPE"

    ${!CONVERT_TYPE}
    is bash's variable indirection. "LARGE_FILE_TYPE" is used as a variable in itself and points to it's content : GB
    Last edited by nmset; 05-06-2010 at 09:22 AM.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  3. #3
    Just Joined! jdh239's Avatar
    Join Date
    Sep 2006
    Posts
    55
    NICE! Thank you very much. I don't quite understand what you said, but I see that it works. I work with a guy that can explain what you did to me.

    Thank you.

Posting Permissions

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