views:

2236

answers:

3

This is a crude korn shell script that someone else wrote. I don't know much about using shell syntax and I'm not even sure if this is possible.

Is there any way for me to run this file and be prompted for the date so that I don't have to manually go into the script and change it each time?

For example, I wan to replace the "1/12/09" with a variable that is taken from a user prompt.

#!/bin/ksh
./clear_old
./rooms_xls.pl 1/12/09
cd doors
./doors_xls.pl 1/12/09
+3  A: 

$1 is the first command line argument. This goes up to $9. Check this tutorial for more basic ksh syntax.

Scottie T
True, but it doesn't deal with the prompt as the question asked. And I'd be happy to see you encouraging a split into the interactive part and a non-interactive part (rudimentary MVC programming), but ...
Jonathan Leffler
+4  A: 

In general from a shell script command line arguments can be accessed like:

$0, $1, ... $N

So you could replace the hardcoded date like:

./room_xls.pl $1

And run it like

./myscript 1/12/09
Ali A
+9  A: 

If you want to be prompted (as opposed to passing the date in as a parameter), use the following logic (or something similar):

date=
while [ -z $date ]
do
    echo -n 'Date? '
    read date
done

That loop will continue to prompt for the date until the user enters something (anything) other than a simple RETURN.

If you want to add some simple validation, and you're using a version of KSH that's KSH93 or better, do something like this:

date=
while [ -z $date ]
do
    echo -n 'Date? '
    read date
    if [[ $date =~ ^[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,4}$ ]]
    then
        break
    fi
    date=
done

See http://docs.sun.com/app/docs/doc/819-2239/ksh93-1?a=view for more info.

Brian Clapper
On Unix systems (probably, as opposed to Linux, meaning it may not exist on Linux), check out the 'ckdate' command and its relatives.
Jonathan Leffler