LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How can I pipe output of a command into arguments of a script (https://www.linuxquestions.org/questions/linux-general-1/how-can-i-pipe-output-of-a-command-into-arguments-of-a-script-609178/)

HGeneAnthony 12-26-2007 06:39 AM

How can I pipe output of a command into arguments of a script
 
I've seen examples on appending/overwriting the output of a command to a file but I haven't seen how you can catch the output of a command through a pipe. This is how I wrote my very basic script

##############################################
#!/bin/bash

for i in $@; do
echo You entered $i
done

##############################################

Where I run the following command it works:
./test.sh dhdhd dhdhd

However, if I enter this command nothing is passed to the program.
ls /home/gene | ./test.sh

Is there a way to either modify my script to accept piped input or a shorthand version I can pass to the script to send it the output. Note: I've tried the pipe with other commands like mkdir it tells me it's missing arguments so I was wondering what's wrong?

Dinithion 12-26-2007 06:50 AM

Use he 'read' command.
I.E. like this:

Code:

#!/bin/bash
read tmp
echo $tmp

Now you can run your script like this:
echo Test | ./test.sh
or more usefull ways
ls | ./test.sh
(Not that the script itself is usefull, but you get the point ;P)

HGeneAnthony 12-26-2007 07:19 AM

Reply
 
I ran the script and all I get is the first item. Is there a way I can pass it the list in one shot, like in a buffer? Also is there a variable that stores the temp data like how find has the {} option?

HGeneAnthony 12-26-2007 07:24 AM

Never mind I got how to do this. I am still wondering though if there's a variable that stores this data I can reference though.


All times are GMT -5. The time now is 06:28 AM.