shell tee未将所有命令输出重定向到文件

cidc1ykv  于 2023-03-13  发布在  Shell
关注(0)|答案(2)|浏览(187)

我正在运行一个命令,它在执行期间输出一些代码行,在完成时输出最后一些代码行,但是将它通过管道传输到tee,以便将该输出复制到文件中,只写入最后几行。
命令为sam build,典型输出如下所示,但只有最后一部分(结果摘要)写入文件。

典型命令输出
sh-4.2# sam build | tee build.txt
Building codeuri: /var/task/lambda-functions/scaffold-site-codebase runtime: nodejs18.x metadata: {} architecture: x86_64 functions: ScaffoldSiteCodebaseFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/confirm-new-purchase runtime: nodejs18.x metadata: {} architecture: x86_64 functions: ConfirmNewPurchaseFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/register-new-customer runtime: nodejs18.x metadata: {} architecture: x86_64 functions: RegisterNewCustomerFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/send-new-purchase-emails runtime: nodejs18.x metadata: {} architecture: x86_64 functions: SendNewPurchaseEmailsFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/send-failed-subscription-renewal-email runtime: nodejs18.x metadata: {} architecture: x86_64 functions: SendFailedSubscriptionRenewalEmailFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp
Building codeuri: /var/task/lambda-functions/create-stripe-customer runtime: nodejs18.x metadata: {} architecture: x86_64 functions: CreateStripeCustomerFunction
Running NodejsNpmBuilder:NpmPack
Running NodejsNpmBuilder:CopyNpmrcAndLockfile
Running NodejsNpmBuilder:CopySource
Running NodejsNpmBuilder:NpmInstall
Running NodejsNpmBuilder:CleanUpNpmrc
Running NodejsNpmBuilder:LockfileCleanUp

Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided
文件内容
Build Succeeded

Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided
pftdvrlh

pftdvrlh1#

您可以将stderrstdoutsam build传递到tee,如下所示:

sam build 2>&1 | tee build.txt
2izufjch

2izufjch2#

如果要将stderr通过管道传输到tee,则需要在管道.... command foo 2>&1 | tee foo.txt. 2>表示将stderr定向到&1stdout之前将stderr重定向到stdout

相关问题