[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::digital_unix

Title:DIGITAL UNIX(FORMERLY KNOWN AS DEC OSF/1)
Notice:Welcome to the Digital UNIX Conference
Moderator:SMURF::DENHAM
Created:Thu Mar 16 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:10068
Total number of notes:35879

8675.0. "Trap CR in ksh" by STOSS1::HORVATH () Fri Jan 31 1997 21:07

    Hello,
    
    How can I trap a carriage return in a ksh script?
    
    The ASCII equivalent to the CR is ^M but this doesn't work in 
    a comparison statement i.e 
    
    if [[ $answer = "^M" ]]; then
    
    
    Thanks,
T.RTitleUserPersonal
Name
DateLines
8675.1VAXCPU::michaudJeff Michaud - ObjectBrokerFri Jan 31 1997 22:2614
>     The ASCII equivalent to the CR is ^M but this doesn't work in 
>     a comparison statement i.e 
>     
>     if [[ $answer = "^M" ]]; then

	Sounds like you want to determine if the user simply pressed
	return w/out entering anything.  The way to test for that is
	to get for the empty string, ie:

		if [[ $answer = "" ]]; then

	because unless you've told the terminal driver to not map carriage
	returns to newlines, you'll never see a carriage return unless the
	user escaped it (ie. ^V^M) and then hit return.
8675.2NOTES collision - but be sure to quote $answerCFSCTC::SMITHTom Smith MRO1-3/D12 dtn 297-4751Fri Jan 31 1997 22:3015
    If you're doing something like testing for a null response (<CR> only)
    to a "read", you can use something like the following:
    
    read answer
    if [ -z "$answer" ]
    then
        answer="default answer"
    fi
    
    This is also Bourne shell-compatible. The string-compare version of the
    test would be `[ "$answer" = "" ]'. The double brackets can be used but
    are only necessary for ksh-specific tests. See test(1) for the "plain
    vanilla" versions.
    
    -Tom
8675.3Works greatSTOSS1::HORVATHMon Feb 03 1997 14:162
    Thanks menu script  works fine.