27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 34. Miscellany<br />

Nobody really knows what the Bourne shell's<br />

grammar is. Even examination of the source code<br />

is little help.<br />

Tom Duff<br />

34.1. Interactive and non−interactive shells and scripts<br />

An interactive shell reads commands from user input on a tty. Among other things, such a shell reads startup<br />

files on activation, displays a prompt, and enables job control by default. The user can interact with the shell.<br />

A shell running a script is always a non−interactive shell. All the same, the script can still access its tty. It is<br />

even possible to emulate an interactive shell in a script.<br />

#!/bin/bash<br />

MY_PROMPT='$ '<br />

while :<br />

do<br />

echo −n "$MY_PROMPT"<br />

read line<br />

eval "$line"<br />

done<br />

exit 0<br />

# This example script, and much of the above explanation supplied by<br />

# Stephane Chazelas (thanks again).<br />

Let us consider an interactive script to be one that requires input from the user, usually with read statements<br />

(see Example 11−2). "Real life" is actually a bit messier than that. For now, assume an interactive script is<br />

bound to a tty, a script that a user has invoked from the console or an xterm.<br />

Init and startup scripts are necessarily non−interactive, since they must run without human intervention. Many<br />

administrative and system maintenance scripts are likewise non−interactive. Unvarying repetitive tasks cry<br />

out for automation by non−interactive scripts.<br />

Non−interactive scripts can run in the background, but interactive ones hang, waiting for input that never<br />

comes. Handle that difficulty by having an expect script or embedded here document feed input to an<br />

interactive script running as a background job. In the simplest case, redirect a file to supply input to a read<br />

statement (read variable

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!