Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


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

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

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

cat test.txt |

The should be feeded by the output of cat.

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

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.
$ cat ttt
The entire line
1 2 3

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

$ 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, 01:51 PM   #3
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, 05:46 PM   #4
LQ Newbie
Registered: May 2008
Location: Asunción, Paraguay
Distribution: Debian, Slackware
Posts: 1

Rep: Reputation: 0
keep it simple people..

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

# edward

you should call your script as follow:

$ echo "keep it simple" |



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 05:26 AM
shell script that read each line separatly xpucto Programming 6 09-20-2005 09:06 AM
How to create a shell script that reads something from stdin? ricky_ds Programming 2 05-17-2005 06:06 AM
Serial Port read in shell script tjt Linux - Newbie 1 08-12-2004 10:18 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 09:23 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:03 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