Solved

How to get a prompt from a shell script?

Posted on 2011-09-21
5
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 …
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…
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…

636 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