Redirect string from bash stderr to user stdin


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirect string from bash stderr to user stdin
# 1  
Old 05-27-2017
Redirect string from bash stderr to user stdin

Hi there,

I need to execute a command in the bash. The program prints some standard (output and) error and then wants the user to choose one of several options and type the according input. I am trying to solve this issue in a bash script but also running into some circular dependency. How can I combine these two things to a single bash call with only one execution of command? For the user input, i would usually write something like
Code:
echo $input | command

But $input must be read from stderr of command in advance which I would usually read as
Code:
input=$(command 2>&1 > /dev/null | awk '/some.*regexp$/ {print $2}')

I particularly don't want to execute command twice (once for extracting/saving the content of $input and once for feeding command with that input). Is there a way to combine the two calls in one? (BTW: both single commands work as I need them since I want to read from the stderr and don't need stdout at all.)

Many thanks in advance!
# 2  
Old 05-27-2017
Welcome to the forum.

Your request is not clear. Be more specific and give more context.
# 3  
Old 05-27-2017
Code:
command $(command 2>&1 > /dev/null | awk '/some.*regexp$/ {print $2}')

# 4  
Old 05-27-2017
but that one-liner (which wouldn't work like that anyways) contains two calls of command. i would like to know, for several reasons, whether this can be done with a single call of command.

Last edited by fredestet; 05-27-2017 at 07:42 PM..
# 5  
Old 05-27-2017
As has been said before, your request is not at all clear. If you would show us the output produced by command to its standard output and to its standard error and show us the output you are trying to produce, we would have a much better chance of helping you.

If you're saying that you want a bash script that you have named command is to read its own standard error output as part of its input, it might be impossible or it might be trivial depending on what your script is trying to do. If this is the case, we need to see the code in your script; not the way you are currently trying to invoke it. (And, the comments in your script had better be extremely clear about where the feedback loop is and what it is trying to do.)

Note that there is also a standard utility named command. Are you sure that you are invoking your script instead of the standard command utility??? The command utility is a shell built-in in bash, ksh, and many other shells based on Bourne shell syntax.
# 6  
Old 05-29-2017
OK, I'm convinced and will try to explain in more detail. The command in question is called gmx genion from the Gromacs package for molecular dynamics simulations. When executing it in a bash terminal, it prints (using stderr) something like
Code:
Select a continuous group of solvent molecules
Group     0 (         System) has 51133 elements
Group     1 (        Protein) has  3961 elements
Group     2 (      Protein-H) has  1951 elements
Group     3 (        C-alpha) has   244 elements
Group     4 (       Backbone) has   732 elements
Group     5 (      MainChain) has   977 elements
Group     6 (   MainChain+Cb) has  1211 elements
Group     7 (    MainChain+H) has  1215 elements
Group     8 (      SideChain) has  2746 elements
Group     9 (    SideChain-H) has   974 elements
Group    10 (    Prot-Masses) has  3961 elements
Group    11 (    non-Protein) has 47172 elements
Group    12 (          Other) has    44 elements
Group    13 (            UNK) has    44 elements
Group    14 (          Water) has 47128 elements
Group    15 (            SOL) has 47128 elements
Group    16 (      non-Water) has  4005 elements
Select a group:

expecting me to type one of the numbers and hit ENTER. I would always choose the number associated with the group SOL, hence 15 in this particular case. However, this number can vary and I want to automate this step in a bash script where, currently, I execute gmx genion twice:
Code:
groupselect=$(echo "" | gmx genion -s $manyArgs 2>&1 > /dev/null | awk '/ SOL\) has.*elements$/ {print $2}')
echo $groupselect | gmx genion -s $manyArgs 2>&1

The first line stores the string "15" in the variable called $groupselect whereas the second call executes the tool as desired with the content of $groupselect as input. Now, I would like to avoid one of the two calls to gmx genion. Is there a way to get this job done with a single call of gmx genion?

Thanks so far for your efforts!
# 7  
Old 05-29-2017
If you want to script interaction with a program expecting human input, bash and most other shells are very bad at that. Pipes are poor substitutes for the interactive terminal the program is obviously expecting, they tend to deadlock with each process waiting for the other and no way to notify each other of said fact.

If your interaction is any more complicated than typing one number and hitting enter, you might look into the expect language, which is designed for these sort of problems. It will allocate a PTY which allows it to control the program it's communicating with asynchronously without all the piping and redirection.

Last edited by Corona688; 05-29-2017 at 03:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect stdout/stderr, except e.g. "STRING"

Hi, I'm running a program (Python) whose output I would like to redirect to a log. But the program calls a library (that I cannot change), which outputs all sorts of useless information. I would like to redirect all output from my Python program into this log, except output that matches the... (7 Replies)
Discussion started by: rswindle
7 Replies

2. UNIX for Advanced & Expert Users

How to set font color for STDIN,STDOUT and STDERR?

I want to differentiate the STDOUT and STDERR messages in my terminal . If a script or command is printing a message in terminal I want to differentiate by colors, Is it possible ? Example: $date Wed Jul 27 12:36:50 IST 2011 $datee bash: datee: command not found $alias ls alias... (2 Replies)
Discussion started by: ungalnanban
2 Replies

3. Shell Programming and Scripting

Redirect String to STDIN

Looking for the proper way to bring a string into the stdin. I have a string that I would like to grep and awk. Each have to be run separately, not piped together. So far, the only way I could figure out how is to echo the string and pipe it: echo 'This is my string' | grep my (3 Replies)
Discussion started by: Panman82
3 Replies

4. Shell Programming and Scripting

Cannot redirect to STDIN in a shell script

I am unable to use STDIn redirection with < (commands) When I do the following, both approaches work and give the same results: 1. $ printf "aaa\nbbb\n" > file1 $ printf "111\n222\n" > file2 $ cat file1 file2 aaa bbb 111 2222. $ cat <(printf "aaa\nbbb\n") <(printf "111\n222\n") aaa... (8 Replies)
Discussion started by: metaltree
8 Replies

5. UNIX for Advanced & Expert Users

inline redirect stdin

Hi: I have the next script on ksh #!/usr/bin/ksh cd $FUENTES qdesign <<-! \$/opt/cognos/ph843e/bin/qtp <<-! \$/opt/cognos/ph843e/bin/quiz <<-! ! ! ! This script is very simple, i want to nest three process quiz into qtp, and this into qdesign. When I run it , i receive the next... (2 Replies)
Discussion started by: ct2marer
2 Replies

6. Shell Programming and Scripting

inline redirect stdin

Hi: I have the next script on ksh #!/usr/bin/ksh cd $FUENTES qdesign <<-! \$/opt/cognos/ph843e/bin/qtp <<-! \$/opt/cognos/ph843e/bin/quiz <<-! ! ! ! This script is very simple, i want to nest three process quiz into qtp, and this into qdesign. When I run it , i receive the... (5 Replies)
Discussion started by: ct2marer
5 Replies

7. Programming

Redirect stdin and out to sockets

For windows was pretty simple to redirect the std in a and out of a child process for "cmd.exe " command prompt terminal to a socket using connected pipes passed to a new process in the STARTUPINFO structure. BOOL b = ::CreatePipe((LPHANDLE)h_stdInRead,(LPHANDLE)hsdtInWriteTmp, &SecAttrib,... (1 Reply)
Discussion started by: gyula
1 Replies

8. Shell Programming and Scripting

can't redirect stderr in bash

Consider: #!/bin/sh #this is a shell script in sh (bourne) grep missingfile 2>errout.txt It works from the command line, but keeps producing errors from the script. So how do I redirect in a bash shell...or bourne? (3 Replies)
Discussion started by: lumix
3 Replies

9. Shell Programming and Scripting

redirect STDIN

can you redirect STDIN with command arguments? I have tried this approach: # ./script -option <argument1> <argument2> 0<$2 # $2: ambiguous redirect Is this possible? (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. Shell Programming and Scripting

redirect stderr to dev/null in bash env.

Working in a bash environment, in the following example, how do I direct the error message that putting in an invalid flag (-j for example) would normally produce to dev/null? while getopts "abcd" opt do case "$opt" in i) a etc ;; r) b etc ;; f) c etc ;; v) d... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question