Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

shell script

Posted on 2011-09-29
14
Medium Priority
?
287 Views
Last Modified: 2012-05-12
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
Comment
Question by:damodar4u
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 800 total points
ID: 36823801
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
 
LVL 31

Expert Comment

by:farzanj
ID: 36825768
Sorry, you said temp then

for temp in $str
do
    echo $temp
done
0
 

Author Comment

by:damodar4u
ID: 36833731
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
Technology Partners: 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!

 

Author Comment

by:damodar4u
ID: 36834515
i need the temp size value to be 40
0
 
LVL 31

Expert Comment

by:farzanj
ID: 36839208
Try this:

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

Sorry don't have shell at this time to test.
0
 

Author Comment

by:damodar4u
ID: 36843096
getting same value ...
0
 

Author Comment

by:damodar4u
ID: 36843237
taht did not fix the issue
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 800 total points
ID: 36843935
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
 

Author Comment

by:damodar4u
ID: 36844570
i cannot use _ as i'm geeting the value from some file ..
0
 
LVL 31

Expert Comment

by:farzanj
ID: 36846495
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
 

Author Comment

by:damodar4u
ID: 36857248
can you pls show me the possible solutions for the above example

 substitute or get rid of dash
0
 
LVL 20

Assisted Solution

by:simon3270
simon3270 earned 400 total points
ID: 36890997
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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 800 total points
ID: 36891180
how are you getting the value from some file, and how are you turning the value into a variable name?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 800 total points
ID: 36891195
in perl, ${"hum-tum"} would work, but what purpose would it serve to do that?
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question