Solved

Bash code too ugly to use

Posted on 2014-11-13
8
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Redhat upgrade 1 66
Install XRDP on Ubuntu Server 16.10 x64 3 111
CentOs root password/fsck issue 7 58
Exchange Server Send connector and DNS Round Robin ? 6 73
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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…
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.:
Suggested Courses

738 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