# checking user input in unix script



## cgjoker (Aug 13, 2003)

Okay, got another.

Im trying to write a unix script where a user provides a filename and I ask the user if it is correct. If it is the script continues, if not it will reprompt the user to enter it again, or they can chose to quit the program completely.

Im having trouble getting this thing to loop, and work for that matter. Here is what I threw together.

#Get the directory path
echo "Enter the directory path: \c" ; read dir 
#Append to the file
echo $dir > /affinium/Campaign/Ad_Hoc_Chris/Snapshots/tempfile.dat

foreach directory ($dir)
echo $dir
echo "Is this the directory path you entered? (y/n/q)"
set ans = $<
switch ($ans)
case n: 
continue
case q: 
exit
case y: 
exit
end


----------



## cgjoker (Aug 13, 2003)

I tried something a little different but its still not working.

echo "Is this the directory path you entered? (y/n/q)"
set ans = $< 
while ( $ans == q )
switch ($<) 
case y: 
echo "You answered yes"
set ans = y
breaksw
case n: 
echo "You answered no"
set ans = n
breaksw
case q:
echo "Quiting program"
set ans = q 
breaksw
default:
echo "Invalid reponse, try again"
breaksw

endsw

end


----------



## cgjoker (Aug 13, 2003)

Here is my latest

#!/bin/sh

#Get the directory path
echo "Enter the directory path: \c" ; read dir 
#Append to the file
echo $dir > /directory/tempfile.dat

typeset -u answer="A"

while [ "${answer}" != "Y" ] && [ "${answer}" != "N" ]
do
echo "The directory path you entered is $dir"
echo "Is the directory path correct (Y/N/Q)?"
read answer
done
if [ "${answer}" = "Y" ]
echo "Continuing process"
exit 0

elsif [ "${answer}" = "N " ]
echo "Enter the directory path: \c" ; read dir 
echo $dir > /directory/tempfile.dat

elsif [ "${answer}" = "Q" ]
echo "Quiting process"
exit 0

fi


----------



## Squashman (Apr 4, 2003)

You may not want to use *dir* as a variable. On some unix and linux systems, they have the *dir* command aliased to the *ls* command.

You might want to have the moderators move this to the Linux/Unix forum. CodeJockey is pretty good at this stuff. Just hit the little Triangle to the right of your post. And ask for it to be moved.

Could you tell us what version of Unix you are using and what Shell you are using?


----------



## Squashman (Apr 4, 2003)

If you are trying to append a file you need to use two *>>* greater than symbols. A single one will overwrite a file.


----------



## codejockey (Feb 11, 2002)

I think you've almost got it. You want to give the user the opportunity to validate the supplied directory within the loop; if the user decides the choice is incorrect, the logic should return to the top of the loop, and give the user another chance to enter the correct value. You might consider something like this:

```
QUIT=0
VALID=0

while [ $VALID -eq 0 -a $QUIT -eq 0 ]
do
    echo please select directory
    read DIRECTORY
    echo you entered $DIRECTORY -- is this correct?

    read ANSWER
    case "$ANSWER" in
        y|Y) VALID=1 ;;
        n|N) ;;
        q|Q) QUIT=1 ;;
        *)   echo response not understood -- assuming 'N'
    esac
done

if [ $VALID -eq 1 ]
then
    # add your processing here ...
fi
```
Hope this helps.


----------



## Squashman (Apr 4, 2003)

Should probably check to make sure the Directory and File exists.


----------



## cgjoker (Aug 13, 2003)

thats great, I will try your suggestions.

Here is what I came up with to this point but it doesn't loop back if the answer is incorrect. Still was working on that part.

#!/bin/sh

typeset -u answer="A"

while [ "${answer}" != "Y" ] && [ "${answer}" != "N" ] && [ "${answer}" != "Q" ]
do
echo "Enter the directory path: \c" ; read dir 
echo $dir > /directory/tempfile.dat
echo "The directory path you entered is $dir"
echo "Is the directory path correct (Y/N/Q)?"
read answer

if [ "${answer}" = "Y" ]
then
echo "Continuing process"
fi

if [ "${answer}" = "N" ]
then
echo "Enter the directory path: \c" ; read dir 
echo $dir > /directory/tempfile.dat
echo "The directory path you entered is $dir"
echo "Is the directory path correct (Y/N/Q)?"
read answer
fi

if [ "${answer}" = "Q" ]
then
echo "Quiting process"
exit;
fi 
done


----------



## cgjoker (Aug 13, 2003)

I tried it out and it works great, thanks.

Ill also need to ask a user for a filename (so both the filename and directory they entered will be used to identify where to read the input), and also ask them for an output directory, (the output directory is where the files will be moved to at the end of other programs). Hence why im putting these into a file so they can be referenced later.

Ill continue to plug away and let you know what I come up with.


----------



## Squashman (Apr 4, 2003)

Did you bother to look at CodeJockey's example. Look at the two variables he defines at the beginning of the script and how he uses them in the While Loop. And dont use *dir* as a variable. I don't mean to sound rude, but what version of Unix are you using and what Shell are you using. I don't like to ask questions twice.


----------



## Squashman (Apr 4, 2003)

cgjoker said:


> I tried it out and it works great, thanks.
> 
> Ill also need to ask a user for a filename (so both the filename and directory


Why don't you ask them to enter in the full path to the filename in one fail swoop. And then do a check to see if the file and path exist. Then you dont need two variables.

You got way to much code in your example. CodeJockeys is so much cleaner. You are duplicating the check for the directory name. If you would do it CodeJockeys way, you would not need to do it twice.

What happens when they enter NO twice. They dont get a third option to enter in the correct directory. You need to set it up the way CodeJockey has it.


----------



## cgjoker (Aug 13, 2003)

I know Codejockeys code is cleaner which is why I said I would try it. I merely posted my code to show what I was doing. I have used his and it works fine. 

I also realized you asked what version of Unix im using and the reason I didn't answer was because at the time I was pressed for time and so I just simply posted my reply and went on my way. Im using Aix 4.1.

I could have them enter the full path but I will be using both the directorythey enter and the filename as two different inputs for something else later on.

This being said, thank you both for your help. I think im good to go now.


----------



## Squashman (Apr 4, 2003)

cgjoker said:


> I also realized you asked what version of Unix im using and the reason I didn't answer was because at the time I was pressed for time and so I just simply posted my reply and went on my way. Im using Aix 4.1.




```
echo $SHELL
```



> I could have them enter the full path but I will be using both the directorythey enter and the filename as two different inputs for something else later on.


yeah, that makes sense. You could have them enter in the directory path and then when you ask them for the filename you could do an ls of the directory.


----------



## codejockey (Feb 11, 2002)

> Ill also need to ask a user for a filename (so both the filename and directory they entered will be used to identify where to read the input), and also ask them for an output directory, (the output directory is where the files will be moved to at the end of other programs). Hence why im putting these into a file so they can be referenced later.


A couple of things to consider:

(1) if your user enters a full pathname, you can use the dirname and basename commands to extract the pathname and filename components, respectively.
(2) if you need to inform subsequent programs about where things are located, consider using either environment variables or parameters (i.e., invoke the subsequent program and pass it the info it needs). Communicating via files is OK, but it does have difficulties (e.g., all scripts/processes must agree on the filename(s)).

Of course, without knowing everything you're trying to do, I may have provided suggestions that are off the mark, so feel free to disregard at will ... ))

Hope this helps.


----------

