0

So I want to validate a response of a user in tcsh script and that's what I have so far

set var1="temp"
    while($var1 != [yY] && $var1 != [Yy][Ee][Ss] && $var1 != [Nn] && $var1 != [Nn][Oo])
            echo "Do you want to create a backup directory ? :"
        set response=&< 
        if ($response == [yY] || $respose == [Yy][Ee][Ss]) then
            mkdir backup
            echo "Backupd directory created \n"
        else if($response == [nN] || $response == [Nn][Oo]) then
            echo "No backup directory is created \n"
        else
            echo "Please enter a valid respose ! \n"
            var1=$response
        endif 
    end

I basically want the user to either input Y,y,Yes,N,n,No and of course I also allow for a combination of small case and upper case in yes and now.

But when I run the script, it gives me an error saying [yY]: No match.

So I am guessing something is wrong when comparing $var1 to [Yy]. Basically I am validating the user input.

And cai reassign var1 like I did, var1=$response, or do I have to use set again >

1 Answer 1

2

You probably meant !~, "does not match regular expression", rather than !=, "is not equal to".

For $var =~ regexp and $var !~ regexp, the pattern on the right should not be quoted. For == and !=, you may quote the part on the right (and in fact I'd encourage this).

Since != expects a string, "[yY] was not parsed as a regular expression. Rather, the shell expanded it to "any file whose name is y or Y". And since you didn't have one, the shell complained.

This sort of thing, by the way, is why I prefer not to write scripts in tcsh, even though I use it as my interactive shell.

2
  • I got that part thanks alot. But I have one question. Can I reassign $var1 using var1=$response or do I have to use set again !
    – alkabary
    Jan 23, 2016 at 0:22
  • I assume you've tried both. What's the value of $var1 after each one?
    – arensb
    Jan 23, 2016 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.