编写 Bash 脚本以根据命令中的参数数量下载可变数量的 .gitignore 文件?
Write Bash script to download a variable number of .gitignore files based on number of arguments in command?
如何在命令行中写入 gitignore Python
并使其成为 运行
curl https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore > ./.gitignore
并gitignore Python macOS
做
curl https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore https://raw.githubusercontent.com/github/gitignore/master/macOS.gitignore > ./.gitignore
gitignore
之后的参数数量是可变的。
tl;dr: 到底部
听起来您想编写一个 bash 函数来遍历参数并将输出写入 .gitignore。这里要经过三个概念:循环迭代、变量扩展和标准输出重定向。
首先,您需要一个函数体,它是用 funcname() { ...; }
创建的(如果您将结束括号分成单独的行,;
是可选的)。
其次,您需要迭代传递给该函数的参数。 $@
是 bash 中的一个特殊变量,它设置为给定 script/function 中所有未移位的参数。要对此进行循环,您可以使用 for 循环结构:for i in "${@}"
。 @
周围的 Curly 括号是可选的,但引号是必需的,因为您不想扩展 funcname "a" "b c"
以评估三个循环迭代。要了解更多信息,请查看 IFS 拆分。
第三,您想在 url 中间扩展新变量 i
。确保它是双引号:
for i in "${@}"; do
curl "https://raw.githubusercontent.com/github/gitignore/master/${i}.gitignore"
done
最后,您想将所有这些写到一个文件中 .gitignore
。在 curl 命令的末尾添加 >.gitignore
听起来很直观,但实际上,您想将它放在 done
之后。这是因为像 funcname Python macOS
这样的调用将 运行:
curl "https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore" >.gitignore
curl "https://raw.githubusercontent.com/github/gitignore/master/macOS.gitignore" >.gitignore
>
重定向运算符将用 python 的内容覆盖 gitignore,然后用 macOS 的内容再次覆盖它。您真的想捕获这两个命令的标准输出并用它们的内容覆盖 .gitignore(或者您可以使用 >>
执行上述操作,但是 运行 多次执行您的命令会导致 .gitignore 中出现重复行).
所以你的最终函数将是:
gitignore() {
for i in "${@}"; do
curl "https://raw.githubusercontent.com/github/gitignore/master/${i}.gitignore"
done >.gitignore
}
如何在命令行中写入 gitignore Python
并使其成为 运行
curl https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore > ./.gitignore
并gitignore Python macOS
做
curl https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore https://raw.githubusercontent.com/github/gitignore/master/macOS.gitignore > ./.gitignore
gitignore
之后的参数数量是可变的。
tl;dr: 到底部
听起来您想编写一个 bash 函数来遍历参数并将输出写入 .gitignore。这里要经过三个概念:循环迭代、变量扩展和标准输出重定向。
首先,您需要一个函数体,它是用 funcname() { ...; }
创建的(如果您将结束括号分成单独的行,;
是可选的)。
其次,您需要迭代传递给该函数的参数。 $@
是 bash 中的一个特殊变量,它设置为给定 script/function 中所有未移位的参数。要对此进行循环,您可以使用 for 循环结构:for i in "${@}"
。 @
周围的 Curly 括号是可选的,但引号是必需的,因为您不想扩展 funcname "a" "b c"
以评估三个循环迭代。要了解更多信息,请查看 IFS 拆分。
第三,您想在 url 中间扩展新变量 i
。确保它是双引号:
for i in "${@}"; do
curl "https://raw.githubusercontent.com/github/gitignore/master/${i}.gitignore"
done
最后,您想将所有这些写到一个文件中 .gitignore
。在 curl 命令的末尾添加 >.gitignore
听起来很直观,但实际上,您想将它放在 done
之后。这是因为像 funcname Python macOS
这样的调用将 运行:
curl "https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore" >.gitignore
curl "https://raw.githubusercontent.com/github/gitignore/master/macOS.gitignore" >.gitignore
>
重定向运算符将用 python 的内容覆盖 gitignore,然后用 macOS 的内容再次覆盖它。您真的想捕获这两个命令的标准输出并用它们的内容覆盖 .gitignore(或者您可以使用 >>
执行上述操作,但是 运行 多次执行您的命令会导致 .gitignore 中出现重复行).
所以你的最终函数将是:
gitignore() {
for i in "${@}"; do
curl "https://raw.githubusercontent.com/github/gitignore/master/${i}.gitignore"
done >.gitignore
}