【发布时间】:2019-11-23 18:02:12
【问题描述】:
我想从终端创建一个脚本,以便像这样使用:
ddmg StartingDirectory
这将为存在的每个子目录在 StartingDirectory 中创建一个 dmg 文件。
例子:
\StartingDirectory
\MDT1D01
\MDT1D02
\MDT1D03
\MDT1DN
命令应该为每个子目录运行 MDT1D0 (1..N) 并为每个创建一个 .dmg,其中 VolumeName 和 FileName 作为同一子目录的名称(即 MDT1D01 f.i.)。 (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"
有了这些信息,你能帮我创建我需要的脚本吗? 我是新手,所以请说的很具体:) 提前非常感谢!
【问题讨论】:
-
当您说
dmg函数“似乎不起作用”时,它究竟是做什么的?有任何错误信息吗?一个可能的问题是引号:至少在您发布的版本中,它有花哨的 unicode 引号(“和”),而不是普通的 ASCII 引号(")。 shell 将无法识别 unicode 引号。 -
它什么都不做。没有错误或其他。顺便说一句,你这是一个好点。 :)