1

My shell is printing the output as shown below.

$ echo ${mount_check[*]} |tr "." "\n"
File system /abc is NOT mounted
 File system /xyz is NOT mounted

$

I had tried using echo "${mount_check[*]}" however that didn't print the output the way I wanted.

I would like the shell print every new line with no space and remove the empty line space as well as shown below.

File system /abc is NOT mounted
File system /xyz is NOT mounted

As requested by David and tripleee adding the full code.

fstable=( $(awk '!/bind|swap|shm/ && $1 !~/#|^$/ && !/^ +$/ { print $2 }' /etc/fstab))
mount_check=($(for mount in "${fstable[@]}"; do
if [[ -z $(findmnt -m "$mount") ]]
then
echo "File system $mount is NOT mounted."
fi
done))
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
rony thomas
  • 215
  • 1
  • 12
  • What is in `mount_check` and how are you populating the array? Show us `declare -p mount_check` – David C. Rankin Jan 19 '20 at 08:17
  • How did you populate `mount_check` in the first place? What does it contain, exactly? You have multiple quirks in your `echo` statement and we can't tell if those are beginner errors or something you do on purpose. `echo "${mount_check[@]}"` might do exactly what you want, or at least provide some insights. See also [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Jan 19 '20 at 08:19
  • I had in fact tried echo "${mount_check[@]}" .This is how the array is made. ......mount_check=($(for mount in "${fstable[@]}"; do if [[ -z $(findmnt -m "$mount") ]] then echo "File system $mount is NOT mounted." fi done)) – rony thomas Jan 19 '20 at 08:20
  • Not to be too dense, but where does `fstable` come from? `lsblk`? Your `/etc/fstab`? – David C. Rankin Jan 19 '20 at 08:23

3 Answers3

1

Assuming your goal is to create an array mount_check[] with the status of all mounts, this loop:

mount_check=($(for mount in "${fstable[@]}"; do
if [[ -z $(findmnt -m "$mount") ]]
then
echo "File system $mount is NOT mounted."
fi
done))

should be this instead:

mount_check=()
for mount in "${fstable[@]}"; do
    mount_status=$(findmnt -m "$mount")
    if [[ -z "$mount_status" ]]
    then
        mount_status="File system $mount is NOT mounted."
    fi
    mount_check+=( "$mount_status" )
done
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

I am not sure about your complete requirement, so going by your question specific to fix your problem.

awk solution:

echo "${mount_check[*]}" | awk -F'mounted.' '{gsub(/ File/,"File");print $1 FS ORS $2 FS}'

OR non-one liner for of above solution:

echo "${mount_check[*]}" |\ 
awk -F'mounted.' '
{
  gsub(/ File/,"File")
  print $1 FS ORS $2 FS
}'

I am considering that your output will always going to have File string in output.

What this solution will take care of is:

  • This will NOT print space which is coming in your 2nd line when you used tr command.
  • 2nd thing it will take care of is, the empty line, which will NOT be printed.


If you want to stick with your code tr one then try following.

echo "${mount_check[*]}" |tr '.' '\n' | sed -E 's/^ +//g;/^$/d'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

Assuming you never used those variables for anything else, you can simply add those tweaks to the Awk script itself.

awk '!/bind|swap|shm/ && $1 !~/#|^$/ && !/^ +$/ {
    p = $2 ; sub(/[.]/, "", p); print p}' /etc/fstab
tripleee
  • 175,061
  • 34
  • 275
  • 318