LinuxQuestions.org
Have you listened to LQ Radio?
Go Back   LinuxQuestions.org > Forums > Linux > 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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools Search this Thread
Old 07-20-2007, 11:55 AM   #1
jhwilliams
Member
 
Registered: Apr 2007
Location: Portland, Oregon
Distribution: Mostly Debian or some (exceptionally popular?) derivative thereof.
Posts: 685
Thanked: 58
Bash Scripting: Pipe input to script vs. $1


[Log in to get rid of this advertisement]
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 11:56 AM..
jhwilliams is offline  
Tag This Post , ,
Reply With Quote
Old 07-20-2007, 12:06 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 16,986
Blog Entries: 33
Thanked: 323
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 12:08 PM..
unSpawn is offline     Reply With Quote
Old 07-20-2007, 12:11 PM   #3
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 49
Thanked: 0
May be
Code:
(method to create filelist) | xargs MycoolShellScript
SeRGeiSarov is offline     Reply With Quote
Old 12-21-2007, 11:54 PM   #4
carlanderson
LQ Newbie
 
Registered: Dec 2007
Posts: 1
Thanked: 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
carlanderson is offline     Reply With Quote

Reply


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
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 06:14 PM
Pipe a bash script into less in the script? keysorsoze Linux - Software 4 06-05-2007 11:41 AM
separte pipe for stderr and stout in a bash script ask4info Programming 5 03-24-2007 09:09 PM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 03:20 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM


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

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.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration