19

Suppose I have a bash shell script called Myscript.sh that need one argument as input.

But I want the content of the text file called text.txt to be that argument.

I have tried this but it does not work:

cat text.txt | ./Myscript.sh

Is there a way to do this?

25

Command substitution.

./Myscript.sh "$(cat text.txt)"
18

You can use pipe output as a shell script argument.

Try this method:

cat text.txt | xargs -I {} ./Myscript.sh {}
1

If you have more than one command in the file, consider using xargs or parallel, e.g.

xargs -d '\n' Myscript.sh < text.txt
parallel -j4 Myscript.sh < text.txt
0

Try,

 $ cat comli.txt
 date
 who
 screen
 wget

 $ cat comli.sh
 #!/bin/bash
 which $1

 $ for i in `cat comli.txt` ; do ./comli.sh $i ; done

so you can enter the values one by one to comli.sh from comli.txt.

0

Process substitutuion

./Myscript.sh <(cat text.txt)

See e.g. https://www.gnu.org/software/bash/manual/bash.html#Process-Substitution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.