LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell Script format question - output command results in quotes (https://www.linuxquestions.org/questions/linux-general-1/shell-script-format-question-output-command-results-in-quotes-676062/)

Barefootpanda 10-13-2008 10:12 AM

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!

colucix 10-13-2008 10:24 AM

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.

jschiwal 10-13-2008 11:22 AM

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.

rlhartmann 10-13-2008 01:55 PM

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/.*/"&"/'

Barefootpanda 10-13-2008 11:28 PM

Thank you very much fellas!

Lots of great tips in there for a newb scripter! Thanks again!

Barefootpanda 10-13-2008 11:40 PM

Quote:

Originally Posted by jschiwal (Post 3308794)
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


All times are GMT -5. The time now is 10:01 AM.