Solved

Bash code too ugly to use

Posted on 2014-11-13
8
204 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

829 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