Solved

How to get a prompt from a shell script?

Posted on 2011-09-21
5
230 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

26 Experts available now in Live!

Get 1:1 Help Now