0

I've spent way to long trying to figure this out, so I hope someone can shed some light on this.

#!/bin/bash

HOSTNAME="`hostname`"
JSONFILE="${HOSTNAME}.json"

#####################
#     FUNCTIONS     #
#####################
function getfilesystems() {
  count=0;
  FILESYSTEMS=()
  SAVEIFS=$IFS
  IFS=$(echo -en "\n\b")
  for fs in `df -P | awk 'NR!=1'`; do
    FILESYSTEMS+=("fs$count=${fs}")
    (( count++ ))
  done
  echo "${FILESYSTEMS[@]}"
  IFS=$SAVEIFS
}

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

jo -p serverdata="$(jo hostname=${HOSTNAME} reportdata="$(date)" storage="$(jo -p "$(getfilesystems)")")"

IFS=$SAVEIFS

I'm trying to focus on this part FILESYSTEMS+=("fs$count=${fs}")

Right now, the output is this:

'fs0=/dev/mapper/vg_rpidalappnfs-LogVol00                                           10190136      5486908      4178940      57% /' 'fs1=tmpfs                                                                           1962684            4      1962680       1% /dev/shm' 'fs2=/dev/sda1                                                                        194241       104145        79856      57% /boot'

It's almost what I want. What I need is this (see the quotes difference?):

fs0="/dev/mapper/vg_rpidalappnfs-LogVol00                                           10190136      5486908      4178940      57% /" fs1="tmpfs                                                                           1962684            4      1962680       1% /dev/shm" fs2="/dev/sda1                                                                        194241       104145        79856      57% /boot"

I've literally been trying to get this to work for about an hour and I just can't seem to get past this one part.

The help is appreciated.

4
  • 1
    I don't think you want any quotes in the output; don't confuse syntactic quotes with literal quotes. You also don't really need an array in getfilesystems if all you are going to do is output the entire contents at the end; you can just write each filesystem out as you discover it. Commented Oct 2, 2018 at 17:05
  • 2
    Bash already sets the shell variable HOSTNAME for you, by the way. Commented Oct 2, 2018 at 17:14
  • 1
    You can probably define the function simply as getfilesystems () { df -P | awk 'NR != 1 {print "fs"(NR-2)"="$0;}'; }. Commented Oct 2, 2018 at 17:35
  • 1
    @BenjaminW. - learned something new today; did not know that. Commented Oct 2, 2018 at 18:35

1 Answer 1

1

Escape the doublequote by a backslash:

FILESYSTEMS+=("fs$count=\"${fs}")

But I fear you won't need the doublequotes in the output in the end, but I'm not familiar with jo.

Sign up to request clarification or add additional context in comments.

2 Comments

Already tried this before posting the questions; it did not work as expected.
I take that back, I think this technically does what I need, but I think the jo application is messing up my output and causing it to add escapes in the shell, so I might have looked at this the wrong way when I first tried this before the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.