Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to get a prompt from a shell script?

Hi,
Let's say I have very basic tcsh file.

and it has something like

echo This is for file X

Open in new window


Now every time I run this file, I want it to ask me X then print it as the following:

>> ./example.csh
>> Which file: 
>> MyFile
>> This is for file MyFile

Open in new window


How can I do that?

Thanks,





Avatar of sakman
sakman
Flag of United States of America image

Here's one way, without any error checking:

#!/usr/bin/tcsh 

echo -n "Which file? "
set file_name = $<
echo "This is for file $file_name"

exit 0

Open in new window


The "echo -n" sends the prompt without a newline and the "$<" reads from standard input.
Avatar of Tolgar
Tolgar

ASKER

Hi,
Thank you for your reply.

What kind of error checking can be added to this code?

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of sakman
sakman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar

ASKER

Thanks for prompt reply.