Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 07-20-2007, 10:55 AM   #1
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Bash Scripting: Pipe input to script vs. $1

I have a script - let's callit MyCoolShellScript, which takes $1 as a list of files. It would be more convenient in this instance, and in the future if instead of saying

(method to create filelist) > .filelist
MyCoolShellScript .filelist
I could just say

(method to create filelist) | MycoolShellScript
However, it would also be convenient to maintain the $1 functionality.
How do you handle piped stdin within a shell script?

Last edited by jhwilliams; 07-20-2007 at 10:56 AM.
Old 07-20-2007, 11:06 AM   #2
Registered: May 2001
Posts: 29,390
Blog Entries: 55

Rep: Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562Reputation: 3562
case "$#" in
0) while read line; do
        line=(${line}); echo -en "Input contains ${#line[@]} elements"
        [ ${#line[@]} -ge 3 ] && echo ", the 3rd element is ${line[2]}." \
        || echo .
-*|--*) echo help; exit 1
*) args=($@); echo -en "Input comprises of ${#args[@]} arguments"
        [ $# -ge 3 ] && echo ", the 3rd element is ${args[2]}." \
        || echo .

exit 0
]$ echo 0 1 nonexistent 3  | sh
Input contains 4 elements, the 3rd element is nonexistent.

]$ sh 0 1 garbage 3
Input comprises of 4 arguments, the 3rd element is garbage.
Not foolproof but it'll do as an example I hope.

Last edited by unSpawn; 07-20-2007 at 11:08 AM.
Old 07-20-2007, 11:11 AM   #3
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Rep: Reputation: 15
May be
(method to create filelist) | xargs MycoolShellScript
Old 12-21-2007, 10:54 PM   #4
LQ Newbie
Registered: Dec 2007
Posts: 1

Rep: Reputation: 0
detecting pipe input from a script

On my system (probably on yours too) you can test for pipe input by looking into /proc/$$/fd/0 - which is the standard input (stdin) file descriptor symlink.
Here's an example script and three examples of it in use:
# file:
readlink /proc/$$/fd/0
bash$ ./

bash$ ls | ./

bash$ ./ < input.txt
Also, if you want to read the input that was piped into the script, you can simply use cat to read it from the pipe.

if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
  echo "Pipe input:"
elif file $( readlink /proc/$$/fd/0 ) | grep -q "character special"; then
  echo "Standard input:"
  echo "File input:"

cat # read from stdin and write to stdout


bash, pipe, stdin

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
separte pipe for stderr and stout in a bash script ask4info Programming 7 09-07-2012 01:39 AM
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 05:14 PM
Pipe a bash script into less in the script? keysorsoze Linux - Software 4 06-05-2007 10:41 AM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration