'How to takes bash script input from both pipe

I want to take my script input from both conditions. And also all conditions support to loop.

How can i do that in bash :)

Condition : 1

cat text.txt | ./myscript.sh

Hello
Hello1
Hello2

Condition :2

./myscript.sh single-word

single-word


Solution 1:[1]

Use "${@:--}" within the script as the input source - that'll use arguments if present or stdin otherwise:

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

awk '{ print FILENAME, $0 }' "${@:--}"

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ed Morton