Joe
Joe

Reputation: 133

Hold content in stdin for another script to read in

If I want to write a script(A) such that it will call another script(B) that will read input from stdin, how can I store the content of file to stdin in A, and also call B in A and then B will read in input from stdin?

Upvotes: 1

Views: 146

Answers (2)

imzhangliang
imzhangliang

Reputation: 31

You can use the pipeline. For example: filecontent:

Michael

scriptA.sh

#!/bin/bash

cat filecontent | ./scriptB.sh

scriptB.sh

#!/bin/bash

read -p "Name:" name
echo "Hello, $name"

When you run the script scriptA.sh. The output will be:

[root@localhost ~]# ./scriptA.sh
Hello, Michael

Upvotes: 2

user8680478
user8680478

Reputation:

Not sure about your real need but logically it can look like this:

# A.sh
cat > /a/tmp/file
/script/to/B < /a/tmp/file

If you don't have special requirements then it can simply be:

# A.sh
/script/to/B

The point is a child process would inherit opened files from its parent process.

Upvotes: 1

Related Questions