There is one problem here. If you run the script without first checking to make sure that there is an entrance on the stdin, it will hang until something is typed.
So, to get around this, you can check that stdin is first, and if not, use the command line argument instead.
Create a script, called "testPipe.sh"
#!/bin/bash # Check to see if a pipe exists on stdin. if [ -p /dev/stdin ]; then echo "Data was piped to this script!" # If we want to read the input line by line while IFS= read line; do echo "Line: ${line}" done # Or if we want to simply grab all the data, we can simply use cat instead # cat else echo "No input was found on stdin, skipping!" # Checking to ensure a filename was specified and that it exists if [ -f "$1" ]; then echo "Filename specified: ${1}" echo "Doing things now.." else echo "No input given!" fi fi on stdin. #!/bin/bash # Check to see if a pipe exists on stdin. if [ -p /dev/stdin ]; then echo "Data was piped to this script!" # If we want to read the input line by line while IFS= read line; do echo "Line: ${line}" done # Or if we want to simply grab all the data, we can simply use cat instead # cat else echo "No input was found on stdin, skipping!" # Checking to ensure a filename was specified and that it exists if [ -f "$1" ]; then echo "Filename specified: ${1}" echo "Doing things now.." else echo "No input given!" fi fi ; #!/bin/bash # Check to see if a pipe exists on stdin. if [ -p /dev/stdin ]; then echo "Data was piped to this script!" # If we want to read the input line by line while IFS= read line; do echo "Line: ${line}" done # Or if we want to simply grab all the data, we can simply use cat instead # cat else echo "No input was found on stdin, skipping!" # Checking to ensure a filename was specified and that it exists if [ -f "$1" ]; then echo "Filename specified: ${1}" echo "Doing things now.." else echo "No input given!" fi fi all the data, we can simply use cat instead #!/bin/bash # Check to see if a pipe exists on stdin. if [ -p /dev/stdin ]; then echo "Data was piped to this script!" # If we want to read the input line by line while IFS= read line; do echo "Line: ${line}" done # Or if we want to simply grab all the data, we can simply use cat instead # cat else echo "No input was found on stdin, skipping!" # Checking to ensure a filename was specified and that it exists if [ -f "$1" ]; then echo "Filename specified: ${1}" echo "Doing things now.." else echo "No input given!" fi fi
Then to check:
Add some things to the test.txt file and then pass the result to our script.
printf "stuff\nmore stuff\n" > test.txt cat test.txt | ./testPipe.sh
Conclusion: Data was piped to this script! Line: stuff Line: more stuff Data was piped to this script! Line: stuff Line: more stuff
Now let's test if you donβt specify any input:
./testPipe.sh
Conclusion: No input was found on stdin, skipping! No input given! No input was found on stdin, skipping! No input given!
Now let's test if you provided a valid file name:
./testPipe.sh test.txt
Conclusion: No input was found on stdin, skipping! Filename specified: test.txt Doing things now.. No input was found on stdin, skipping! Filename specified: test.txt Doing things now..
And finally, let the test using an invalid file name:
./testPipe.sh invalidFile.txt
Conclusion: No input was found on stdin, skipping! No input given! No input was found on stdin, skipping! No input given!
Explanation: Programs such as read and cat will use stdin if they are available in the shell, otherwise they will wait for input.
Credit goes to Mike from this page in his answer showing how to check the login for stdin: https://unix.stackexchange.com/questions/33049/check-if-pipe-is-empty-and-run-a-command -on-the-data-if-it-isnt? newreg = fb5b291531dd4100837b12bc1836456f