我有 2 个 shell 脚本。 script_1.sh
和 script_2.sh
都有可执行权限。
我在 script_1.sh
中有一个方法可以将 echo 语句记录到日志文件中。
以下是script_1.sh
#!/bin/sh
log() {
/bin/echo $1 >> /path/to/theLogFile
}
TEST_VALUE=10
log "Logging a test value which says $TEST_VALUE"
# Calling script_2
/path/to/script_2
以下是script_2.sh
#!/bin/sh
echo "This is logging from script 2"
问题:
从 script_1.sh
echo
编辑的所有内容都会进入 theLogFile
。但显然 script_2.sh
没有任何内容。我的问题是如何从 script_2.sh
调用 script_2 以便将 script_2.sh
中的所有 echo
语句添加到 中theLogFile
以及使用方法 log
from script_1。
我应该做这样的事情吗?
log(/path/to/script_2)
或者有更好的方法吗?
最佳答案
如果您希望 script2 中的所有标准输出都由日志函数处理,那么在 bash
中使用 script1.sh
:
#!/usr/bin/env bash
logit() {
command echo "$@" >> theLogFile
}
# export function definition
declare -fx logit
# Calling script_2 and redirect all stdout to logit function
(exec > >(logit "$(cat)"; exit); ./script2.sh)
# this will still be on stdout
echo "foo bar"
关于linux - 如何将调用的 shell 脚本中的 echo 语句传递到调用方脚本中方法的参数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55901571/