LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-13-2008, 10:12 AM   #1
Barefootpanda
LQ Newbie
 
Registered: Oct 2008
Posts: 10

Rep: Reputation: 0
Shell Script format question - output command results in quotes


Hey guys, first post here!

I am working on a script and my web guy wants it to output with a very specific format. I'm very close and have my echos printing most of my stuff, but I am needing quotes around the output value from a command.

I'm try to get quotes around the temp (040 here)
Code:
"drive_a": 040
My script reads like...
Code:
echo -n "\"drive_a\": "
/usr/sbin/smartctl --all -d ata /dev/sda | grep -i temperature | awk '{ print $4 }'
echo skips the new line (-n) and everything is good there with displaying its quotes, I just need to get the smartctl output to be in quotes.

Thanks guys!
 
Old 10-13-2008, 10:24 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can try command substitution in a echo statement:
Code:
echo -n "\"drive_a\": "
echo "\"$(/usr/sbin/smartctl --all -d ata /dev/sda | grep -i temperature | awk '{ print $4 }')\""
If you prefer, you can merge the two lines in a single echo statement.
 
Old 10-13-2008, 11:22 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You can use the octal code for `"' in the printf statement. Using grep is silly:
Code:
 sudo /usr/sbin/smartctl --all -d ata /dev/sda | awk '/Temperature/{ printf "\042Drive:\042 \042%d\042\n",$4/10}'
"Drive:" "10"
You might want to select by the code instead of the temperature.
Code:
194 Temperature_Celsius     0x0022   100   100   ...
According to man smartctl, code 194 is for the raw Celcius temperature x 10. Check your manpage. I performed the division in the print statement.
 
Old 10-13-2008, 01:55 PM   #4
rlhartmann
Member
 
Registered: Mar 2008
Posts: 73

Rep: Reputation: 16
You could also use sed to insert the quotes,
sed 's/.*/"&"/' .* says to match anything and & is
what every it matched. This is not as efficient as the
awk statement above, but it is easier to read.

Quote:
echo -n "\"drive_a\": "
/usr/sbin/smartctl --all -d ata /dev/sda | grep -i temperature | awk '{ print $4 }' | sed 's/.*/"&"/'
 
Old 10-13-2008, 11:28 PM   #5
Barefootpanda
LQ Newbie
 
Registered: Oct 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Talking

Thank you very much fellas!

Lots of great tips in there for a newb scripter! Thanks again!
 
Old 10-13-2008, 11:40 PM   #6
Barefootpanda
LQ Newbie
 
Registered: Oct 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
You can use the octal code for `"' in the printf statement. Using grep is silly:
Code:
 sudo /usr/sbin/smartctl --all -d ata /dev/sda | awk '/Temperature/{ printf "\042Drive:\042 \042%d\042\n",$4/10}'
"Drive:" "10"
Great tip. Though I get different results....

Code:
/usr/sbin/smartctl --all -d ata /dev/sda | awk '/Temperature/{ printf "\042drive_a:\042 \042%d\042\n",$4/10}'
prints....


Code:
"drive_a:" "4"
....for me.

I'm guessing that means '40' but I would like "40" printed.


EDIT: /usr/sbin/smartctl --all -d ata /dev/sda | awk '/Temperature/{ printf "\042drive_a:\042 \042%d\042\n",$4/1}' << worked for me

Last edited by Barefootpanda; 10-13-2008 at 11:47 PM. Reason: FIXED!
 
  


Reply

Tags
scripting, smartctl


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-01-2008 11:37 PM
shell command script question.... Basslord1124 Linux - Software 2 04-11-2007 04:36 PM
Shell script help with function and output format altrob Programming 4 09-27-2006 02:51 PM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM
passing quotes in shell script Tenor Trombone Programming 1 04-18-2001 09:26 PM


All times are GMT -5. The time now is 02:54 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration