如何从两个管道获取 bash 脚本输入

How to takes bash script input from both pipe

我想从这两种情况下获取我的脚本输入。而且所有条件都支持循环。

我如何在 bash 中做到这一点 :)

条件:1

cat text.txt | ./myscript.sh

Hello
Hello1
Hello2

条件:2

./myscript.sh single-word

single-word

在脚本中使用 "${@:--}" 作为输入源 - 如果存在则使用参数,否则使用标准输入:

$ cat tst.sh
#!/usr/bin/env bash

awk '{ print FILENAME, [=10=] }' "${@:--}"

$ cat text.txt | ./tst.sh
- Hello
- Hello1
- Hello2

$ ./tst.sh text.txt
text.txt Hello
text.txt Hello1
text.txt Hello2

如果你只有1个参数:

#!/bin/bash
#
printf "$(cat )\n"