Reputation: 1
I have a simple "students" csv file with 5 columns and few lines, the first column is for names. I just want to prompt the user for a name and see if we have it on the csv file but it does not work and I do not know why. Also, I do not want to exit the program if it is not found, I just want to print the message
read -p "What is your name?: " arg1
if grep -q arg1 "$names.csv"; then
$ printf "%s\n" "We got you!"
else
$ printf "%s\n" "Sorry, you are not on the list!"
csv file with input:
Name,R1, R2 , R3 , R4
James,"35,587","55,800,000,000","1,567,988","491,000,000,000"
Tom,"16,000","11,300,000,000","706,250","120,000,000,000"
Sarah,"3,069","1,180,000,000","384,490","7,200,000,000"
Upvotes: 0
Views: 2213
Reputation: 133545
Could you please try following(since OP haven't mentioned samples so couldn't test it).
cat script.ksh
read -p "What is your name?: " arg1
if grep -q "$arg1" "names.csv"
then
printf "%s\n" "We got you!"
else
printf "%s\n" "Sorry, you are not on the list!"
fi
Problem's fixed in OP's code:
$
to variable arg1
else it will treat arg1
as a string while grepping it to names.csv
file.$
in front of printf
.fi
to complete if
block, please see the manual page for same every if
should be ended with fi
to complete its block.EDIT1: Tested with own created examples:
Let's say following is our Input_file.
cat Input_file
"singh","abc_test","1","2","3"
"abc","singh","1","2","4"
"1","2","3","4","5"
script is as follows:
cat script.ksh
read -p "What is your name?: " arg1
if grep -q "$arg1" "Input_file"
then
printf "%s\n" "We got you!"
else
printf "%s\n" "Sorry, you are not on the list!"
fi
Now when I run following code, I get the output.
./script.ksh
What is your name?: singh
We got you!
EDIT2: In case you want to match exact string like "singh"
in any field of csv then try following.
cat script.ksh
read -p "What is your name?: " arg1
if grep -q "\"$arg1\"" "file"
then
printf "%s\n" "We got you!"
else
printf "%s\n" "Sorry, you are not on the list!"
fi
Testing of above script:
./script.ksh
What is your name?: singh1
Sorry, you are not on the list!
./script.ksh
What is your name?: singh
We got you!
Upvotes: 1