intruder
intruder

Reputation: 417

How to redirect a file to other file

I've following two scripts:

A.sh 
B.sh

A.sh is as follows:

#!/bin/bash
some/path/ 2>/dev/null -jar some/path/java.jar "$1" 

Let's assume that A.sh takes input as:

$ A.sh "some script"

And we'd redirect it to some output as:

$ A.sh "some script" > output.txt

And let's assume that B.sh takes a file (file.txt) as input and process it like:

$ B.sh "file.txt"

Now, I need a script which can pipeline output.txt to B.sh. Something which can perform below operations in a single script? (Is it possible to do so? If not, any solution?)

$ A.sh "some script" > output.txt
$ B.sh "output.txt"

Upvotes: 1

Views: 85

Answers (2)

user5076313
user5076313

Reputation:

You can use the tee command in between the running of two commands to capture the output of the first command.

Example:-

   A.sh "some script" | tee output.txt | B.sh

The output for the A.sh script output is stored in the output.txt file and as well as it is passed to the input of the B.sh script.

Upvotes: 1

John1024
John1024

Reputation: 113864

Try process substitution:

B.sh <(A.sh "some script")

<(...) is process substitution. It makes the output of A.sh available to B.sh as a file-like object. This works as long as B.sh does simple sequential reads.

This requires bash or other advanced shell. Process substitution is not required by POSIX and, consequently, simple shells like dash do not support.

Documentation

From man bash:

Process Substitution
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of <(list) or >(list). The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list.

When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.

Upvotes: 3

Related Questions