LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: Is it possible to take STDIN? (http://www.linuxquestions.org/questions/programming-9/bash-is-it-possible-to-take-stdin-436181/)

microsoft/linux 04-17-2006 06:48 PM

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?

Hobbletoe 04-17-2006 07:33 PM

Code:

man read
Basically, to read information into a variable (let's say temp_var), you

Code:

read temp_var
If you just use read, information is automatically stored in $REPLY.

perfect_circle 04-17-2006 07:35 PM

"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?

microsoft/linux 04-17-2006 07:45 PM

no, read the input into a bash script, like cin in c++ or scanf in c

perfect_circle 04-17-2006 08:16 PM

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"


microsoft/linux 04-17-2006 08:23 PM

ok, thanks one and all

fotoguy 04-17-2006 08:35 PM

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

roybal 12-30-2011 10:53 AM

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/"

ntubski 12-30-2011 04:21 PM

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/"



All times are GMT -5. The time now is 12:00 PM.