简体   繁体   中英

Mounted volumes & bash in OSX

I'm working on a disk space monitor script in OSX and am struggling to first generate a list of volumes. I need this list to be generated dynamically as it changes over time; having this work properly would also make the script portable.

I'm using the following script snippet:

#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH

FS=$(df -l | grep -v Mounted| awk ' { print $6 } ')

while IFS= read -r line
do
echo $line
done < "$FS"

Which generates:

test.sh: line 9: /
/Volumes/One-TB
/Volumes/pfile-archive-offsite-three-CLONE
/Volumes/ERDF-Files-Offsite-Backup
/Volumes/ESXF-Files-Offsite-Backup
/Volumes/ACON-Files-Offsite-Backup
/Volumes/LRDF-Files-Offsite-Backup
/Volumes/EPLK-Files-Offsite-Backup: No such file or directory

I need the script to generate output like this:

/
/Volumes/One-TB
/Volumes/pfile-archive-offsite-three-CLONE
/Volumes/ERDF-Files-Offsite-Backup
/Volumes/ESXF-Files-Offsite-Backup
/Volumes/ACON-Files-Offsite-Backup
/Volumes/LRDF-Files-Offsite-Backup
/Volumes/EPLK-Files-Offsite-Backup

Ideas, suggestions? Alternate or better methods of generating a list of mounted volumes are also welcome.

Thanks!

Dan

< is for reading from a file. You are not reading from a file but from a bash variable. So try using <<< instead of < on the last line.

Alternatively, you don't need to store the results in a variable, then read from the variable; you can directly read from the output of the pipeline, like this (I have created a function for neatness):

get_data() {
  df -l | grep -v Mounted| awk ' { print $6 } '
}

get_data | while IFS= read -r line
do
  echo $line
done

Finally, the loop doesn't do anything useful, so you can just get rid of it:

 df -l | grep -v Mounted| awk ' { print $6 } '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM