Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash pipes not working with my scripts [duplicate]

Tags:

linux

bash

shell

I have written two scripts. One is myscipt1.sh which reads a sequence of integer numbers (provided as arguments) and reports a final number:

[user@pc user] ./myscript1.sh 34 45 67 234
[user@pc user] 1200

In the above example, the script returns 1200. Another script myscript2.sh takes a string as input and returns a sequence of integer numbers:

[user@pc user] ./myscript2.sh a string to provide
[user@pc user] 364 465 786 34 22 1

I want to call myscript1.sh by passing the result of myscript2.sh, so I tried:

[user@pc user] ./myscript2.sh my_string | ./myscript1.sh

But I have no luck as myscript1.sh (wich performs a check on the number of arguments passed, exiting of no arguments are passed) reports that no arguments were passed.

Looks like Bash have problems when I use pipes with scripts I write. How to do?

like image 625
Andry Avatar asked Mar 20 '26 17:03

Andry


2 Answers

You can run it as:

./myscript1.sh $(./myscript2.sh my_string)
like image 98
anubhava Avatar answered Mar 22 '26 07:03

anubhava


Use xargs

[user@pc user] ./myscript2.sh my_string | xargs ./myscript1.sh
like image 36
Joucks Avatar answered Mar 22 '26 07:03

Joucks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!