简体   繁体   English

在OSX中挂载的卷和bash

[英]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. 我正在OSX中处理磁盘空间监视器脚本,并且正在努力首先生成卷列表。 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. 您不是从文件中读取,而是从bash变量中读取。 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 } '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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