Entries Comments



Providing Input to Commands Within a Script

4 December, 2007 (15:13) | commands, UNIX | By: passion@linux

Scripts should run with as little user intervention as possible. There are several reasons for this. Firstly, the whole point of having a script is to reduce the amount of typing needed to run a job. Secondly, scripts should be able to be run by users with little or no technical knowledge. Thirdly, they are designed to make sure that nothing is left out or mistyped, thus reducing operator errors.

Many Unix commands and programs require input from the user. In an automated script, you would often want to provide this information from the script itself. We have seen in an earlier article that the standard input and output can be diverted to a file instead of the terminal by using output redirection (>), input redirection (<) and error redirection (2>).

Input can be redirected to come from the script itself by using the symbols <<. The text to be taken as input must be enclosed in exclamation marks (!). The following example illustrates how this is done.

cat > textfile <<!
This is some sample text
!

Again, this is a feature which can be very useful in installation scripts, which may need to set up initial files, customized scripts for running jobs etc.

Let’s look at an installation script that needs to schedule a day end job to be run at 6 PM daily. The cron utility is used for job scheduling. cron can take details of the job from a file, or they can be entered from the terminal. In the example below, the information is supplied within the installation script.

cron <<!
00 18 * * * /usr/progs/dayend

Write a comment