LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 12-12-2013, 12:17 PM   #1
mojofender
LQ Newbie
 
Registered: Feb 2013
Posts: 4

Rep: Reputation: Disabled
Cannot assign variable in shell one liner


Hello,

I am remoted in with putty and am trying some one liners.
I am trying stuff in online article "one liners explained".
for example:
var = $(smartctl -a /dev/sdc | grep “Model:” | awk '{print $3}') (Trying to put command result to a variable)

or

echo smartctl -a /dev/sdc | grep "Model:" | awk '{print $3}' > var (Trying to put command to a file)

Thanks,
 
Old 12-12-2013, 12:29 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Both commands are syntactically incorrect.

The first one has spaces around the =, which shouldn't be there:
Code:
var=$( smartctl -a /dev/sdc | grep “Model:” | awk '{print $3}' )
The second has an unneeded echo:
Code:
smartctl -a /dev/sdc | grep "Model:" | awk '{print $3}' > var
BTW: The smartctl -a /dev/sdc | grep “Model:” | awk '{print $3}' uses an unneeded grep, this would do the same:
Code:
smartctl -a /dev/sdc | awk'/Model:/ { print $3 }'
 
1 members found this post helpful.
Old 12-12-2013, 12:44 PM   #3
mojofender
LQ Newbie
 
Registered: Feb 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Awesome! It works great. Thanks.
 
Old 12-12-2013, 06:17 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
I am curious about the second example as I think it depends on how the potion in brackets is interpreted as to whether or not the echo is required.
Code:
echo smartctl -a /dev/sdc | grep "Model:" | awk '{print $3}' > var (Trying to put command to a file)
I see the portion in red can be read one of 2 ways:

1. Trying to put the output of the command to a file - then druuna's correction and example is correct

2. Trying to put THE command to a file - here you would need to use the echo as you wish the entire command to be placed inside the file. The command would still need to be corrected for this to occur as the pipes are raw and will therefore be used, so for this option the below would work:
Code:
echo "smartctl -a /dev/sdc | grep \"Model:\" | awk '{print $3}'" > var
 
Old 12-12-2013, 08:32 PM   #5
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
optinally addition to @grail's input: grep could be replaced by awk as in #2 by @druuna.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 05:43 PM
[SOLVED] assign full name to variable then grep a file for the variable socalheel Programming 3 09-16-2013 12:04 PM
[SOLVED] bash script problem -- pass complex *one-liner* to a variable RandyTech Linux - Newbie 12 01-27-2013 06:01 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 05:56 PM


All times are GMT -5. The time now is 12:24 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration