LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-17-2006, 06:48 PM   #1
microsoft/linux
Senior Member
 
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Blog Entries: 9

Rep: Reputation: 48
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?
 
Old 04-17-2006, 07:33 PM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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.
 
Old 04-17-2006, 07:35 PM   #3
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
"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?
 
Old 04-17-2006, 07:45 PM   #4
microsoft/linux
Senior Member
 
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445

Original Poster
Blog Entries: 9

Rep: Reputation: 48
no, read the input into a bash script, like cin in c++ or scanf in c
 
Old 04-17-2006, 08:16 PM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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"
 
Old 04-17-2006, 08:23 PM   #6
microsoft/linux
Senior Member
 
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445

Original Poster
Blog Entries: 9

Rep: Reputation: 48
ok, thanks one and all
 
Old 04-17-2006, 08:35 PM   #7
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
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
 
Old 12-30-2011, 10:53 AM   #8
roybal
LQ Newbie
 
Registered: Sep 2009
Posts: 8
Blog Entries: 8

Rep: Reputation: 0
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/"
 
Old 12-30-2011, 04:21 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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/"
 
  


Reply

Tags
bash, input, stdin



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting stdin in bash script artur Programming 3 12-09-2011 06:07 AM
mounting from stdin slinky2004 Linux - General 2 03-10-2006 02:33 PM
unbuffered stdin Mike Davies Linux - Software 1 03-03-2006 12:13 AM
stdout stdin Furlinastis Linux - Newbie 3 08-11-2005 11:00 PM
Statistics from stdin? edeca Linux - Software 0 05-23-2005 10:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 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
Open Source Consulting | Domain Registration