MrP*_*les 14 unix linux bash shell
我data=$(cat)在bash脚本中看到了这一行(只是声明一个空变量)并且对于可能做的事情感到困惑.
我阅读了手册页,但它没有这方面的例子或解释.这会捕获stdin或其他东西吗?关于这个的任何文件?
编辑:特别是如何做data=$(cat)它允许它运行这个钩子脚本?
#!/bin/bash
# Runs all executable pre-commit-* hooks and exits after,
# if any of them was not successful.
#
# Based on
# http://osdir.com/ml/git/2009-01/msg00308.html
data=$(cat)
exitcodes=()
hookname=`basename $0`
# Run each hook, passing through STDIN and storing the exit code.
# We don't want to bail at the first failure, as the user might
# then bypass the hooks without knowing about additional issues.
for hook in $GIT_DIR/hooks/$hookname-*; do
test -x "$hook" || continue
echo "$data" | "$hook"
exitcodes+=($?)
done
Run Code Online (Sandbox Code Playgroud)
https://github.com/henrik/dotfiles/blob/master/git_template/hooks/pre-commit
tri*_*eee 13
cat 将其输入连接到其输出.
在您发布的变量捕获的上下文中,效果是将语句(或包含脚本)的标准输入分配给变量.
命令替换$(command)将返回命令的输出; 赋值将替换的字符串赋值给变量; 并且在没有文件名参数的情况下,cat将读取并打印标准输入.
您在此处找到的Git钩子脚本捕获来自标准输入的提交数据,以便可以分别重复管道到每个钩子脚本.您只能获得一份标准输入,因此如果您需要多次,您需要以某种方式捕获它.(我会使用一个临时文件,并正确引用所有文件名变量;但将数据保存在变量中肯定是可以的,特别是如果你只想要相当少量的输入.)
这样做:
t@t:~# temp=$(cat)
hello how
are you?
t@t:~# echo $temp
hello how are you?
Run Code Online (Sandbox Code Playgroud)
(单身Controld就行了"你呢?"终止输入.)
正如手册所说
cat - 连接文件并在标准输出上打印
也
cat将标准输入复制到标准输出.
在这里,cat将您连接STDIN成一个字符串并将其分配给变量temp.