Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm writing a CLI script that asks a series of questions before doing a few things. How can I pass them as arguments so that I don't have to keep entering them every time I want to test my script?

Basically, it should pass 4 items to STDIN, like "text1[ENTER]text2[ENTER]text3[ENTER]text4[ENTER]" automatically.

Yes, I could modify my script to actually read the shell arguments, but I don't want to do it that way, since it's supposed to run more like a wizard.

Looking for something like

SOMEPROGRAM myscript arg1 arg2 arg3 arg4

or

SOMEPROGRAM arg1 arg2 arg3 arg4 | myscript

Or something like that. Is there such a program?

share|improve this question

7 Answers 7

I understand you do not want to modify myscript.

Regarding the second solution you ask for, you can use printf:

printf '%s\n' text1 text2 text3 text4 | myscript

so that, defining an alias for SOMEPROGRAM as:

alias SOMEPROGRAM="printf '%s\n'" 

you could effectively call

SOMEPROGRAM text1 text2 text3 text4 | myscript

The first form is ambiguous (from the point of view of SOMEPROGRAM), because it don't know where myscript options end and text parameters start, unless myscript is effectively invoked without any options. In this case you could use a function:

SOMEPROGRAM() {
  myscript="$1"
  shift
  printf '%s\n' "$@" | "$myscript"
}

so that you could effectively call

SOMEPROGRAM myscript text1 text2 text3 text4
share|improve this answer
    
Haha...you didn't have to make it verbatim :p printf solution looks good, I'll have to try this on monday! –  Mark Sep 10 '11 at 0:52
    
for the printf solution I get stty: stdin isn't a terminal –  Mark Sep 13 '11 at 20:17
    
@Mark: ok, not all programs accept indifferently stdin or interactive input. What is your myscript? –  enzotib Sep 13 '11 at 21:19
    
It's a PHP script that takes input in via $fp=fopen("php://stdin","r") and fgets($fp) –  Mark Sep 13 '11 at 21:56
    
@Mark: I have no php knowledge, but I wonder why the stty string in the error message. It could be useful to see a stripped down, but complete, version of myscript that still presents the problem. –  enzotib Sep 14 '11 at 4:16

So basically, you want to pass each argument as a line to a child program. Below is a script that loops through all arguments passed ti SOMEPROGRAM and prints them as a line. You can pass an empty line ("enter without entering something before") by passing an empty argument as in SOMEPROGRAM yes '' | myscript.

#!/bin/bash
# This is "SOMEPROGRAM", it prints each argument as "answer" to  a program that
# must be piped. Usage: SOMEPROGRAM yes 1 no 2 | myscript
for arg; do
    echo "$arg"
done

If you always need to answer "yes" to your script, use the coreutil yes:

yes | myscript

If you need to pass some other value, say "no":

yes 'no' | myscript
share|improve this answer

You can pass the parameters, but it won't work, you'll also have to modify the bash script to accept the params from the command-line as well.

share|improve this answer
    
Why's that? It's just reading in 4 things from STDIN one at a time, and you can pass things into STDIN...so I don't see the problem. –  Mark Sep 10 '11 at 0:52

If you are writing a script then just use getopts so the arguments are passed to the scripts and then you can use those args as you want? Is this something you are looking for?

http://wiki.bash-hackers.org/howto/getopts_tutorial

share|improve this answer
    
I'm just going to quote myself here... "Yes, I could modify my script to actually read the shell arguments, but I don't want to do it that way, since it's supposed to run more like a wizard." –  Mark Sep 10 '11 at 0:53

For simple cases, the way enzotib describes it with piping stdin from a file seems reasonable. Consider this script:

#!/bin/bash
read -p "What's your name?" name 
echo $name 
read -p "Age?" age 
echo $age
echo "Your name is " $name " and your age is "$age

invoke it with:

echo -e "Foobar\n14\n" | ./namenage.sh 

The -e is to enable escape sequences like \newline.

Foobar
14
Your name is  Foobar  and your age is 14

Note how magically, bash doesn't print the prompt for reading.

However, for more complicated cases, there is a program, called expect to handle interactive CLI programs, where you have to wait for the next prompt, before entering the second value, and I guess you can even branch under conditions.

share|improve this answer
    
echo solution looks nice too. It's actually a PHP CLI script, but I follows roughly the same format as your example, so I think it should work. –  Mark Sep 10 '11 at 0:57

With bash, you can do this:

myscript << END
arg1 
arg2
arg3
arg4
END

or

myscript <<< $'arg1\narg2\narg3\narg4\
share|improve this answer

I'm pretty sure you're looking for expect.

edit: example added

  
#!/bin/bash  
USER="root"  
HOST="192.168.1.111"  
PASS="secret"  
expect -c "  
spawn ssh $USER@$HOST  
expect \"password:\"  
send \"$PASS\r\"  
interact  
"  

--from this blog

share|improve this answer
    
But then you have to write a whole expect script? I was hoping I could just stuff in some command-line args, or pipe it through some program without writing a whole secondary script. Maybe I was just dreaming though. –  Mark Feb 21 '12 at 17:54
    
check the edit...it's not a whole different script exactly as it's still inside the same bash script –  hbdgaf Feb 21 '12 at 20:59
    
Except I was writing a PHP CLI script, not a CLI script. Although I could probably shell_exec it...not sure if it would work like that or not though. Oh well. –  Mark Feb 21 '12 at 22:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.