Solved

Bash code too ugly to use

Posted on 2014-11-13
8
205 Views
Last Modified: 2014-11-18
I want to take commands that give multline output, add lines to them and bracket them with quotation marks as the commas.

if you look at the output of this code in excel you will see the number of lines  then the output of the mount command in each cell of an excel csv spreadsheet.

I would like to be able to make the contents of cell and then printf "%s" them

Here is my ugly and confusing attempt

#!/bin/bash


function yada {
echo "MountCount=`mount | wc -l`"
mount
}


printf "%s" "\"`yada`\"",
printf "%s" "\"`yada`\"",
0
Comment
Question by:TIMFOX123
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 40441662
Can you please give an example of your desired output?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40441730
printf '"%s",' "`yada`"
?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40442441
Do you want a csv file of the 'mount' output?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TIMFOX123
ID: 40443737
Sorry for being so slow to respond, you folks are great

Here is the output and a screen shot of what I want it to look like in excel

The reason I want pretty code ( easy to follow) is I have to maintain it as well as I tend to use the same code many times

acceptance of confusing code by the team is not going to happen,
mud.csv
sshot.png
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40444212
Replace your function with this to get the required output:
echo \"Mountcount=$(mount | wc -l)\"
mount | sed 's/^/"/;s/$/"/'

Open in new window

0
 

Author Comment

by:TIMFOX123
ID: 40444686
I have to say that is  much more elegant solution however that is quoting every line.

I was hoping to have quotes only at the beginning and end.  

If you open this in excel you would likely see the difference.
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 40444785
I see, try last line like this:

echo \"  $(mount) \"

(mobile, not tested)

<edit>
This works:
echo \"Mountcount=$(mount | wc -l)\"
echo \"$(mount)\"

Open in new window


Or one one line:
echo \"Mountcount=$(mount | wc -l)\";echo \"$(mount)\"

Open in new window

0
 

Author Closing Comment

by:TIMFOX123
ID: 40450093
Great job,  

I had to do a little mod on this one so I figure I would post it

yada="\"$(echo -n "MountCount=";mount | wc -l; mount  )\","


it was still your solution, I just had to clean some up so excel was happy
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question