LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices

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

Rep: Reputation: 207Reputation: 207Reputation: 207
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

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

Code:
(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
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,285
Blog Entries: 54

Rep: Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854
Code:
#!/bin/sh
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 .
   done
   ;;
-*|--*) echo help; exit 1
   ;;
*) args=($@); echo -en "Input comprises of ${#args[@]} arguments"
        [ $# -ge 3 ] && echo ", the 3rd element is ${args[2]}." \
        || echo .
   ;;
esac

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

]$ sh example.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
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Rep: Reputation: 15
May be
Code:
(method to create filelist) | xargs MycoolShellScript
 
Old 12-21-2007, 10:54 PM   #4
carlanderson
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:
Code:
#!/bin/sh
# file: example.sh
readlink /proc/$$/fd/0
Code:
bash$ ./example.sh
/dev/pts/0

bash$ ls | ./example.sh
pipe:[27112838]

bash$ ./example.sh < input.txt
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.
Code:
#!/bin/sh

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:"
else
  echo "File input:"
fi

cat # read from stdin and write to stdout
 
  


Reply

Tags
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


All times are GMT -5. The time now is 04:58 AM.

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