Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
04-17-2006, 07:48 PM
#1
Senior Member
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Rep:
BASH: Is it possible to take STDIN?
Is it possible to take input from STDIN in bash? I know how to do it in C, C++, and Perl, but is there a way to do it in bash? Or if not, is there a way to execute a command(with variables as parameters) in perl?
04-17-2006, 08:33 PM
#2
Member
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 146
Rep:
Basically, to read information into a variable (let's say temp_var), you
If you just use read, information is automatically stored in $REPLY.
04-17-2006, 08:35 PM
#3
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware
Posts: 1,782
Rep:
"Is it possible to take input from STDIN in bash?"
What do you mean? the stdin is the keyboard. You always get stdin.
Do you mean to read the input from a bash script?
04-17-2006, 08:45 PM
#4
Senior Member
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Original Poster
Rep:
no, read the input into a bash script, like cin in c++ or scanf in c
04-17-2006, 09:16 PM
#5
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware
Posts: 1,782
Rep:
Quote:
Originally Posted by microsoft/linux
no, read the input into a bash script, like cin in c++ or scanf in c
then as Hobbletoe already posted "read" will do the job:
Code:
#!/bin/bash
echo -n "What's your name? "
read var1
echo "hello $var1"
04-17-2006, 09:23 PM
#6
Senior Member
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Original Poster
Rep:
ok, thanks one and all
04-17-2006, 09:35 PM
#7
Senior Member
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,205
Rep:
Or if you want to allow a user to choose different options try a case function:
#!/bin/sh
echo "Enter your command (who, list, or cal)"
read command
case "$command" in
who)
echo "Running who..."
who
;;
list)
echo "Running ls..."
ls
;;
cal)
echo "Running cal..."
cal
;;
*)
echo "Bad command, your choices are: who, list, or cal"
;;
esac
exit 0
12-30-2011, 11:53 AM
#8
LQ Newbie
Registered: Sep 2009
Posts: 8
Rep:
i know this is long dead, but i see this ranks high in the results
Use cat to capture stdin from the command line pipe:
cat myfile.txt | myscript.sh
in myscript.sh:
#!/bin/bash
cat - | sed "s/tranform this line/into this line/"
12-30-2011, 05:21 PM
#9
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 1,332
You resurrected a five year old thread to post a
Useless Use of Cat
Code:
cat myfile.txt | myscript.sh
myscript.sh < myfile.txt
Code:
#!/bin/bash
cat - | sed "s/tranform this line/into this line/"
sed "s/tranform this line/into this line/"
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 12:31 PM .
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