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

huangapple go评论68阅读模式
英文:

shell tee not redirecting all command output to file

问题

Typical command output

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

file content


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
英文:

I'm running a command that output some lines of code during execution and some final ones when finished, but piping it to tee to have that output duplicated to file write just the final lines.

The command is sam build, and typical ouput is as follows, but only the last section (the result recap) is written to file.

Typical command output

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

file content


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

答案1

得分: 1

You can pass both stderr and stdout from sam build to tee like this:

sam build 2>&1 | tee build.txt
英文:

You can pass both stderr and stdout from sam build to tee like this:

sam build 2>&1 | tee build.txt

答案2

得分: 0

If you want stderr to be piped to tee then you need to redirect stderr to stdout before the pipe.... command foo 2>&1 | tee foo.txt. 2> represents directing stderr to &1 stdout.

英文:

If you want stderr to be piped to tee then you need to redirect stderr to stdout before the pipe.... command foo 2>&1 | tee foo.txt. 2> represents directing stderr to &1 stdout

huangapple
  • 本文由 发表于 2023年3月9日 17:32:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定