Skip to main content
not a loop device
Link
sourcejedi
  • 53.6k
  • 23
  • 179
  • 337
Post Closed as "Duplicate" by Chris Davies, peterh, mdpc, G-Man Says 'Reinstate Monica', Raphael Ahrens
Source Link
mike
  • 313
  • 1
  • 3
  • 10

grep within an if statement

I am writing a function that prompts for the name of a file and then make sure the input starts with a alpha char, if it doesn't it will re prompt the user for a name until the criteria is met. It also gives an additional message if the user inputs a path(a path is anything with a '/' for the purpose of this assignment). That is where I am getting an issue...

here is my code...

#1) make a getname function that will prompt for filename
function getname(){
trap control_c SIGINT
local fname=$1;

#if there is no input prompt user for file name
if [ ! $1 ]; then
        read -p "Enter a file name: " fname;
fi;

#until grep is given a valid file name
until ( grep -E '^[a-zA-Z_]\w+$' <<< "$fname" > /dev/null 2>&1); do





        #this is were the error is
        if echo "$fname" | grep -E '/';





         then                           #this tests if fname is a file directory
        echo "Paths are not a legal file name.";
        fi;
        read -p "Enter a legal file name: " fname;
done
echo "$fname"
}

The if statement within the until loop will not prompt the user that paths are not valid input under any circumstances and also produces no errors. How would I properly implement a grep in an if statement?