Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get a prompt from a shell script?

Posted on 2011-09-21
5
Medium Priority
?
239 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 2000 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

886 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