LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack 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: 45
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 10, Linux Mint 11
Posts: 148

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: 52
"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
Blog Entries: 9

Original Poster
Rep: Reputation: 45
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: 52
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
Blog Entries: 9

Original Poster
Rep: Reputation: 45
ok, thanks one and all
 
Old 04-17-2006, 08:35 PM   #7
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,275

Rep: Reputation: 61
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
Posts: 2,308

Rep: Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768
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


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 08:11 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration