bash - Bash 脚本 [macOS Sierra 终端] 为给定的每个子目录创建一个 .dmg
问题描述
我想从终端创建一个脚本,以便像这样使用:
ddmg StartingDirectory
这将为存在的每个子目录在 StartingDirectory 中创建一个 dmg 文件。
例子:
\StartingDirectory
\MDT1D01
\MDT1D02
\MDT1D03
\MDT1DN
命令应该为每个子目录 MDT1D0 (1..N) 运行并为每个子目录创建一个 .dmg,VolumeName和FileName作为同一子目录的名称(即 MDT1D01 fi)。(VolumeName是打开 dmg 时出现在 Finder 左侧的名称)。
我已经知道创建 dmg 的命令是:
hdiutil create -volname VolumeName -srcfolder /path/to/the/folder/you/want/to/create -ov -format UDZO FileName.dmg
这是有效的,因为我对其进行了测试。
我已经尝试过以这种方式创建一个名为 dmg 的个人命令:
dmg(){
hdiutil create -volname “$1” -srcfolder “$2” -ov -format UDZO “$3.dmg”
}
应该以这种方式使用:
dmg VolumeName source/directory/path FileName
但它似乎不起作用,我不明白为什么。
此外,我找到了一个创建脚本的模板(这是有效的,但它说我需要安装 xtools 才能正常工作,我想是因为我现在不需要 git 命令):
#!/bin/bash
#Use set -x if you want to echo each command while getting executed
#set -x
#Save current directory so we can restore it later
cur=$PWD
#Save command line arguments so functions can access it
args=("$@")
#Put your code in this function
#To access command line arguments use syntax ${args[1]} etc
function dir_command {
#This example command implements doing git status for folder
cd $1
echo "$(tput setaf 2)$1$(tput sgr 0)"
git tag -a ${args[0]} -m "${args[1]}"
git push --tags
cd ..
}
#This loop will go to each immediate child and execute dir_command
find . -maxdepth 1 -type d \( ! -name . \) | while read dir; do
dir_command "$dir/"
done
#This example loop only loops through give set of folders
declare -a dirs=("dir1" "dir2" "dir3")
for dir in "${dirs[@]}"; do
dir_command "$dir/"
done
#Restore the folder
cd "$cur"
有了这些信息,你能帮我创建我需要的脚本吗?我是新手,所以请非常具体:) 提前非常感谢!
解决方案
我找到了一种方法来做到这一点。这是代码:
#!/bin/bash
#Use set -x if you want to echo each command while getting executed
#set -x
#Save current directory so we can restore it later
cur=$PWD
#Save command line arguments so functions can access it
args=("$@")
#Put your code in this function
#This loop will go to each immediate child and execute hdiutil
find . -depth 1 -type d | cut -c 3- | while read dir; do
hdiutil create -volname $dir -srcfolder $dir -format UDZO $dir.dmg
done
#Restore the folder
cd "$cur"
请注意,我已将“-ov”选项剪切为 hdiutil,因此它不会覆盖现有的 dmg。
它非常有用,因为如果您有很多子目录,您可能会在某些时候遇到错误(例如我收到“资源繁忙”错误),这样您就可以简单地重新启动命令而不必担心目录有错误。刚刚经过测试,它可以完美运行。5 小时它现在正在创建 dmgs :)
推荐阅读
- snowflake-cloud-data-platform - 从我们仓库中其他 Snowflake DB 派生的数据中使自定义表保持最新的最佳实践
- scala - Spark 中的 XGBoost 模型 --> 缺失值处理
- forms - 如何执行两个输入只需按下一个
- powershell - Powershell启动进程找不到文件
- java - Webflux,如何拦截请求并添加新标头
- sql - 查找具有特定列名 + 值小于 X 的所有表
- powerbi - Power BI - 为日期 1、日期 2..date n 添加列/筛选器,其中 n 是两个日期之间的天数
- javascript - 从axios请求中获取数据
- ios - 反应原生 ios 处理推送通知而不点击通知
- c - Fortran/C 互操作性将结构从 Fortran 传递到 C 与可分配对象