LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem in assigning value to a variable in shell script. (http://www.linuxquestions.org/questions/linux-newbie-8/problem-in-assigning-value-to-a-variable-in-shell-script-845815/)

mshforever 11-22-2010 10:01 AM

Problem in assigning value to a variable in shell script.
 
Hi,

Why doesn't "var1=`echo $var2 | grep pattern`" work ?
Please help me.

i92guboj 11-22-2010 10:19 AM

It should. What does the command in between tickers (`) return? What ***exact*** line are you using in your script? I need to see it exactly as-is, to the last quote and punctuation character, please, copy/paste directly. Also, explain ***how*** it doesn't work. Is there an error? Is it just empty? How are you retrieving or using $var1? What's the contents of $var2?

catkin 11-22-2010 10:21 AM

It does normally work. Can you copy and paste testing it at the command prompt, including an echo "'$var2'" before the problem variable assignment so we can see exactly what you are doing?

mshforever 11-24-2010 12:09 PM

Quote:

Originally Posted by i92guboj (Post 4167155)
It should. What does the command in between tickers (`) return? What ***exact*** line are you using in your script? I need to see it exactly as-is, to the last quote and punctuation character, please, copy/paste directly. Also, explain ***how*** it doesn't work. Is there an error? Is it just empty? How are you retrieving or using $var1? What's the contents of $var2?

Thnks for the reply and sorry for the delay. The exact code is:

procinfo=`awk -F\\\t '/model name/{print $2}' /proc/cpuinfo`
varDual=`echo $procinfo | grep Duo`

mshforever 11-24-2010 12:10 PM

Quote:

Originally Posted by catkin (Post 4167156)
It does normally work. Can you copy and paste testing it at the command prompt, including an echo "'$var2'" before the problem variable assignment so we can see exactly what you are doing?

Thnks for the reply and sorry for the delay. The exact code is:

procinfo=`awk -F\\\t '/model name/{print $2}' /proc/cpuinfo`
varDual=`echo $procinfo | grep Duo`

catkin 11-24-2010 01:53 PM

What is the output of awk -F\\\t '/model name/{print $2}' /proc/cpuinfo on your system?

mshforever 11-29-2010 04:50 AM

Quote:

Originally Posted by catkin (Post 4169885)
What is the output of awk -F\\\t '/model name/{print $2}' /proc/cpuinfo on your system?

Thanks for the replies. I get ": Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz".

grail 11-29-2010 05:38 AM

So then what do you get after running:
Code:

echo "$varDual"
My guess is it should be the same as:
Quote:

: Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz

mshforever 11-30-2010 07:05 AM

Quote:

Originally Posted by grail (Post 4174484)
So then what do you get after running:
Code:

echo "$varDual"
My guess is it should be the same as:

Looks like the issue earlier I was getting is not appearing now. Thanks for all the replies it really helped.


All times are GMT -5. The time now is 08:44 AM.