LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-26-2007, 06:39 AM   #1
HGeneAnthony
Member
 
Registered: Mar 2003
Posts: 178

Rep: Reputation: 30
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?
 
Old 12-26-2007, 06:50 AM   #2
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
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)
 
Old 12-26-2007, 07:19 AM   #3
HGeneAnthony
Member
 
Registered: Mar 2003
Posts: 178

Original Poster
Rep: Reputation: 30
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?
 
Old 12-26-2007, 07:24 AM   #4
HGeneAnthony
Member
 
Registered: Mar 2003
Posts: 178

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script: put the output of a command into a variable. poincare999 Programming 4 11-04-2007 12:53 PM
A command or script to rm files based on df output? Anithen Slackware 7 06-21-2007 11:18 PM
Pipe output of command to php !!! ALInux Programming 7 12-30-2006 01:36 PM
how to pipe and parse output of a command learnfast Linux - Newbie 2 06-15-2005 05:55 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 10:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration