Solved

Bash code too ugly to use

Posted on 2014-11-13
8
202 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 37

Expert Comment

by:Gerwin Jansen
ID: 40442441
Do you want a csv file of the 'mount' output?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 37

Expert Comment

by:Gerwin Jansen
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 37

Accepted Solution

by:
Gerwin Jansen 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

786 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