Shell scripts that respond to console output

shellshell-scriptsubversion

Disclaimer:: I am not an active shell scripter

How does one create a script that responds to console output? For instance, I'm trying to setup a cron that periodically svnsync 's changes to my repository to a backup server every midnight. The problem is, in order to sync, it prompts for authorization credentials.

svnsync sync accepts no options to specify a login thus a simple cron command is out. I believe I can launch a script but I'm trying to figure out how to make it respond to console output.

For instance:

ken@server:~$ svnsync sync https://svn3.backup.net/svn/mirror
Authentication realm: <https://svn3.backup.net/svn/mirror:443> backup.net Subversion Backup
Password for 'ken':
***<send return key>***
Username:***<specify backup.net username>***
Password for '<the name>':***<specify password for username>***
<It proceeds to check out>

where ***< message >*** is the action I want the script to respond to.

Best Answer

I was in doubt when you said "svnsync sync accepts no options to specify a login" so I checked the documentation and guess what, it does:

--source-password
--source-username
--sync-password
--sync-username

Those options should be enough for you to go back to a simple cron script.

Back into the case where you really can't specify such options, it is still easy to write a wrapper script that sends data to the program's stdin. For example the following may work (where program is the program you wan to run, and text is a file where you store text to be sent to the program):

program < text

However, for authentication, programs are often written to ready from tty and not from stdin (for security reasons). I'm not familiar with it, but you can still create a fake terminal in that case. This is where expect comes into use.

Related Question