Reputation: 53
I have never done shell scripting before and need some help with a small project.
I want a user to enter three file names and check that the user has input THREE names and give an error if its more or less. The files will be sorted into another file but that is working fine just having problem with checking what the user has entered.
I have tried
echo Please select the three files you want to use
read $file1 $file2 $file3
if ! [ $# -eq 3 ]; then
echo "Please enter THREE values"
fi
Upvotes: 2
Views: 120
Reputation: 54081
Without changing your read
command:
if [ -z "$file1" -o -z "$file2" -o -z "$file3" ]; then echo "Please enter THREE values" fi
But the preferred way is using arrays here:
read -a files if [ ! ${#files[@]} -eq 3 ]; then echo "Please enter THREE values" fi
And btw. the elements are ${files[0]}
, ${files[1]}
and ${files[2]}
or, you could loop the array:
for f in "${files[@]}"; do echo $f done
Upvotes: 3
Reputation: 19797
You did well, the only problem is the use of $
in your read statement. Remove dollars and your code will work:
#!/bin/sh
echo "Please select the three files you want to use"
read file1 file2 file3
#Or something like:
#echo -n "File 1:"
#read file1
#echo -n "File 2:"
#read file2
#echo -n "File 3:"
#read file3
echo "File 1: $file1 File 2 : $file2 File 3 : $file3"
Upvotes: 0