LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-02-2006, 09:53 AM   #1
bujecas
Member
 
Registered: Oct 2004
Location: Portugal
Distribution: Debian, Slackware
Posts: 78

Rep: Reputation: 20
read from stdin in a shell script


Hello,
I want to write a shell script that can be used in a pipe. For example,

cat test.txt | script.sh

The script.sh should be feeded by the output of cat.

I couldn't find out how to do it. Any ideias? Thanks.
 
Old 02-02-2006, 10:28 AM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,272

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
In the example below, "ttt" is a two line text file and "qqq" is the shell script that accepts input from that file. "qqq" illustrates reading an entire line from stdin and also parsing a line into individual parameters.
Code:
$ cat ttt
The entire line
1 2 3


$ cat qqq
echo
x=`line`
echo "I read the first line as:"
echo "<$x>"
echo
read a b c
echo "I parsed the second line as:"
echo "1st param: <$a>"
echo "2nd param: <$b>"
echo "3rd param: <$c>"
echo


$ cat ttt | qqq

I read the first line as:
<The entire line>

I parsed the second line as:
1st param: <1>
2nd param: <2>
3rd param: <3>

$
 
Old 02-02-2006, 12:51 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
A few general comments:
A script is not fed by stdin---rather the COMMANDS work off of stdin.
If you run a script from a terminal, then the first command that needs input will expect it from the terminal unless you use redirection to tell it other wise. If you do not run from a terminal, then I think stdin is ambiguous and you need explicit redirection.

More detailed answer might be easier with an example of what you want to do.
 
Old 07-03-2009, 04:46 PM   #4
disem
LQ Newbie
 
Registered: May 2008
Location: Asunción, Paraguay
Distribution: Debian, Slackware
Posts: 1

Rep: Reputation: 0
keep it simple people..


Code:
#!/bin/sh
# Converts all lowercase text from
# stdin to uppercase
#
tr '[:lower:]' '[:upper:]' < /dev/stdin

# edward
# ebaddouh@gmail.com


you should call your script as follow:

Code:
$ echo "keep it simple" | myscript.sh
KEEP IT SIMPLE

cheers
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
shell script that read each line separatly xpucto Programming 6 09-20-2005 08:06 AM
How to create a shell script that reads something from stdin? ricky_ds Programming 2 05-17-2005 05:06 AM
Serial Port read in shell script tjt Linux - Newbie 1 08-12-2004 09:18 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration