Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any simple way to check all possibilities of yes/no option in Bash?

Tags:

bash

aix

awk

I am trying to implement a check for different possibilities of Yes/No. Below code is working fine but is there any simple way for doing this.

Note: I am using bash version 2.02.0.

read -p "Using dest path :${DESTPATH}" flag;
OPT=$(echo $flag|awk '{print tolower($0)}')
if [[ ${OPT:0:1} != 'y' ]]; then
     echo "Exiting..."; return
fi
like image 554
Pravin Junnarkar Avatar asked Dec 20 '22 05:12

Pravin Junnarkar


1 Answers

You don't need to call awk at all. You can take advantage of globbing:

read -p "Using dest path :${DESTPATH}" flag

if [[ $flag != [yY]* ]]; then
     echo "Exiting...";
     exit 1
fi

[yY]* will match any string starting with either y or Y

like image 69
anubhava Avatar answered Dec 22 '22 00:12

anubhava