Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect stdin from a shell script to a command in the shell script?

Tags:

bash

stdin

How do I redirect stdin from a shell script to a command in the shell script? I am trying to pass the stdin to a java System.in stream.

I want to replace

find . -type f | $JAVA_HOME/bin/java com.domain.BatchProcess

with

 find . -type f | ./batch.sh
like image 779
jon077 Avatar asked Feb 12 '09 21:02

jon077


People also ask

What is Stdin redirection?

Redirection is a feature in Linux such that when executing a command, you can change the standard input/output devices. The basic workflow of any Linux command is that it takes an input and give an output. The standard input (stdin) device is the keyboard. The standard output (stdout) device is the screen.

What is Stdin in shell script?

stdin − It stands for standard input, and is used for taking text as an input. stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error.

Which symbol connects the stdin to the shell?

The pipe (" | ") symbol in the middle tells the shell to connect commandA 's standard output to commandB 's standard input before launching them.


2 Answers

If you exec the command within the script, it will replace the shell and inherit it's file descriptors, including stdin,stdout and stderr.

like image 166
Dave Stenglein Avatar answered Sep 18 '22 02:09

Dave Stenglein


If the command:

$JAVA_HOME/bin/java com.domain.BatchProcess

is expecting input from stdin, then putting it in a script and running your second command will work.

like image 40
Jeremy Cantrell Avatar answered Sep 20 '22 02:09

Jeremy Cantrell