1,105,625 Community Members

Pipe alternative

Member Avatar
liran
Newbie Poster
24 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey,

I have some command, I'll call it "command".

I need to do this:
yes | command
but without a pipe, there is a reason for this, because for the specific use, pipe before the command
is not allowed, that's not the point.

Is there an alternative I can use which will output do the same thing without the pipe ?
Maybe something like this:
command < yes
(which will not work because it expects a file, and not a command)
?

Or some other simple way ?

Thanks

Member Avatar
Nutster
Junior Poster
146 posts since Oct 2006
Reputation Points: 52 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 3 [?]
 
0
 

You could try:

#/bin/sh
yes > /tmp/tmpout
command < /tmp/tmpout
rm /tmp/tmpout

You could even put it in a shell script, but then you could go back to using the pipe then as well.

Member Avatar
L7Sqr
Veteran Poster
1,006 posts since Feb 2011
Reputation Points: 179 [?]
Q&As Helped to Solve: 168 [?]
Skill Endorsements: 11 [?]
 
0
 

The problem with redirecting to a file or using a subshell or similar technique is that the yes command is infinite. There is no terminating condition other than killing the process. There are ways to do this with other commands but something like yes is going to be problematic.
Is yes a requirement or did you serve it up as an example of what you are trying to do?

Member Avatar
Nutster
Junior Poster
146 posts since Oct 2006
Reputation Points: 52 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 3 [?]
 
0
 

Do you know how many "y"s you need? Filter the output with head to restrict the number of lines.
yes | head -n 5 | command
When head closes its input after 5 lines, yes gets told its output has closed and it will stop. In the meantime, command is processing the output from head.

Member Avatar
L7Sqr
Veteran Poster
1,006 posts since Feb 2011
Reputation Points: 179 [?]
Q&As Helped to Solve: 168 [?]
Skill Endorsements: 11 [?]
 
0
 

The problem, I believe, is not how to limit the output of yes; you've clearly demonstrated how to do that. I think the more difficult problem is doing that without using a pipe (thus preventing a 'broken pipe' from terminating yes).

Member Avatar
rubberman
Senior Poster
3,999 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 501 [?]
Skill Endorsements: 87 [?]
 
0
 

You can easily embed the output from a shell command into the argument list of another command using backquotes. Example, if you want 'y' to be passed as the option "--accept=", try this:

command --accept=`yes | head -1`

To get that into the standard input stream you can do this (scripting is best since it requires at least 2 lines):

command <<EOF y
EOF

# or

command <<EOF `yes | head -1`
EOF
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: