• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

shell script

i have two varaibales in a string, the variables are seperated by space as below .. i need a for loop to extract each string into a variable called temp and print temp each time
str = "hum-tum bay"
0
damodar4u
Asked:
damodar4u
  • 6
  • 5
  • 2
  • +1
5 Solutions
 
farzanjCommented:
First: NO SPACES IN ASSIGNMENT
SO
str = "hum-tum bay"

Should be
str="hum-tum bay"


Second: You need a for loop

for var in $str
do
    echo $var
done
0
 
farzanjCommented:
Sorry, you said temp then

for temp in $str
do
    echo $temp
done
0
 
damodar4uAuthor Commented:
hum-tum_tit_for_tat_SIZE = 40


INST = hum-tum

 eval TEMP_SIZE="$"${INST}_tit_for_tat_SIZE

it is not evaluating to 40 rather it is evaluating to -tum_tit_for_tat_SIZE
can you pls fix it



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
damodar4uAuthor Commented:
i need the temp size value to be 40
0
 
farzanjCommented:
Try this:

TEMP_SIZE=$(eval echo \$${INST}_tit_for_tat_SIZE)

Sorry don't have shell at this time to test.
0
 
damodar4uAuthor Commented:
getting same value ...
0
 
damodar4uAuthor Commented:
taht did not fix the issue
0
 
farzanjCommented:
That is because the variable name is faulty.  It cannot be hum-tum.  It will be interpreted as the math operator minus.  Could you please use underscore and test.
0
 
damodar4uAuthor Commented:
i cannot use _ as i'm geeting the value from some file ..
0
 
farzanjCommented:
Well, you would need to either substitute using either sed or something.

Try doing

hum\-tum=10

echo ${hum\-tum}

Probably would not work.  Then the only solution would be to substitute or get rid of dash
0
 
damodar4uAuthor Commented:
can you pls show me the possible solutions for the above example

 substitute or get rid of dash
0
 
simon3270Commented:
Why are you generating the variable name from the contents of a data file?  There are many data formats which would not be valid variable names (mostly punctuation as you have seen, but also any string starting with a digit).
0
 
ozoCommented:
how are you getting the value from some file, and how are you turning the value into a variable name?
0
 
ozoCommented:
in perl, ${"hum-tum"} would work, but what purpose would it serve to do that?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now