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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-02-2006, 09:53 AM
|
#1
|
Member
Registered: Oct 2004
Location: Portugal
Distribution: Debian, Slackware
Posts: 78
Rep:
|
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.
|
|
|
02-02-2006, 10:28 AM
|
#2
|
Senior Member
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331
|
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>
$
|
|
|
02-02-2006, 12:51 PM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
|
|
07-03-2009, 04:46 PM
|
#4
|
LQ Newbie
Registered: May 2008
Location: Asunción, Paraguay
Distribution: Debian, Slackware
Posts: 1
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 02:40 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|