Solved

How to get a prompt from a shell script?

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

710 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