gpt4 book ai didi

linux - Unix:cat 本身做什么?

转载 作者:IT王子 更新时间:2023-10-29 00:25:13 27 4
gpt4 key购买 nike

我在 bash 脚本中看到了 data=$(cat) 行(只是声明了一个空变量),我对它能做什么感到困惑。

我阅读了手册页,但没有对此的示例或解释。这是否捕获标准输入或其他东西?有这方面的文档吗?

编辑:具体来说,data=$(cat) 到底是如何让它运行这个钩子(Hook)脚本的?

#!/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

https://github.com/henrik/dotfiles/blob/master/git_template/hooks/pre-commit

最佳答案

cat 会将其输入连接到输出。

在您发布的变量捕获的上下文中,效果是将语句的(或包含脚本的)标准输入分配给变量。

命令替换 $(command) 将返回命令的输出;赋值会将替换后的字符串分配给变量;如果没有文件名参数,cat 将读取并打印标准输入。

您在其中找到的 Git Hook 脚本从标准输入中捕获提交数据,以便可以将其重复分别传输到每个 Hook 脚本。您只能获得标准输入的一份副本,因此如果您多次需要它,则需要以某种方式捕获它。 (我会使用一个临时文件,并正确地引用所有文件名变量;但是将数据保存在一个变量中当然没问题,特别是如果您只期望相当少量的输入。)

关于linux - Unix:cat 本身做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31142363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com