Solved

How to get a prompt from a shell script?

Posted on 2011-09-21
5
229 Views
Last Modified: 2012-05-12
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,





0
Comment
Question by:Tolgar
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:sakman
ID: 36574318
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.
0
 

Author Comment

by:Tolgar
ID: 36580485
Hi,
Thank you for your reply.

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

Thanks,
0
 
LVL 4

Accepted Solution

by:
sakman earned 500 total points
ID: 36580918
Anytime you accept user input, what was entered should be tested.

Two tests you can do are 1) was a file name entered and, 2) does the file name entered exist.

Checking if the file name exists is optional if you don't care that the file exists or not...

#!/usr/bin/tcsh 

# Set file_name to blank.
#
set file_name 

# Get user input.  Continue if a blank was entered.
#
while ($file_name == "")

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

end

# Does the file exist?
#
if (-f $file_name) then
	# Yes.  
	#
	echo "This is for file $file_name"
else
	# No. 
	#
	echo "Cannot find file: $file_name"
endif

exit 0

Open in new window

0
 
LVL 4

Assisted Solution

by:sakman
sakman earned 500 total points
ID: 36580943
Actually, the exit code should depend on whether the file was found or not.  So "exit 1" if not found and "exit 0" if found...

# Does the file exist?
#
if (-f $file_name) then
	# Yes.  
	#
	echo "This is for file $file_name"
	exit 0
else
	# No. 
	#
	echo "Cannot find file: $file_name"
	exit 1
endif

Open in new window

0
 

Author Closing Comment

by:Tolgar
ID: 36644136
Thanks for prompt reply.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video discusses moving either the default database or any database to a new volume.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now