LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2010, 10:01 AM   #1
mshforever
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 1
Question Problem in assigning value to a variable in shell script.


Hi,

Why doesn't "var1=`echo $var2 | grep pattern`" work ?
Please help me.
 
Old 11-22-2010, 10:19 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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?
 
Old 11-22-2010, 10:21 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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?
 
Old 11-24-2010, 12:09 PM   #4
mshforever
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 1
Question

Quote:
Originally Posted by i92guboj View Post
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`
 
Old 11-24-2010, 12:10 PM   #5
mshforever
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 1
Question

Quote:
Originally Posted by catkin View Post
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`
 
1 members found this post helpful.
Old 11-24-2010, 01:53 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What is the output of awk -F\\\t '/model name/{print $2}' /proc/cpuinfo on your system?
 
Old 11-29-2010, 04:50 AM   #7
mshforever
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 1
Question

Quote:
Originally Posted by catkin View Post
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".
 
Old 11-29-2010, 05:38 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 11-30-2010, 07:05 AM   #9
mshforever
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 1
Thumbs up

Quote:
Originally Posted by grail View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Problem With Shell Script Variable Usage Crowey Linux - General 2 02-09-2010 01:03 AM
Bash/Shell: Problem using a variable in a script redvivi Linux - General 8 08-21-2008 02:02 PM
Help with variable assigning in shell scripts fultron Linux - Newbie 2 02-16-2007 04:32 PM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration