Having trouble greping a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Having trouble greping a variable
# 1  
Old 04-18-2012
Having trouble greping a variable

I'm trying to take a users input, and then such a text file to see if it contains it.
I get an error when running it:
"./Login.sh: Line 8: [grep: command not found
Failed to login."

Code:
echo Username
read username

if [grep $username register.txt ]; then
echo Login successful.
else
echo Failed to login.
fi

If someone could give me some input to where I'm going wrong it'd be greatly appreciated.
# 2  
Old 04-18-2012
you don't need to use [ ] at all. but the problem is that you need a space after '['. if takes a command. grep is a command. you can simply use grep there.

Code:
if grep -q "$username" register.txt; then

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 04-18-2012
Code:
 
echo Username
read username
grep $username register.txt > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
echo Login successful.
else
echo Failed to login.
fi

$? is a speacial variable which holds the exit code of the previous command.

so, if the grep is success, then it is 0 otherwise not zero
This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 04-18-2012
Quote:
Originally Posted by neutronscott
you don't need to use [ ] at all. but the problem is that you need a space after '['. if takes a command. grep is a command. you can simply use grep there.

Code:
if grep -q "$username" register.txt; then

This worked, thank you.

Last edited by Hegarz; 04-18-2012 at 10:39 AM..
# 5  
Old 04-18-2012
Check if you have the -q flag for grep so no output is produced. That is a neater way of writing it. You may need to check you can read the file first though:-
Code:
if [ -r register.txt ]
then
   grep -q $username register.txt
   RC=$?
else
   echo "Cannot read file"
   RC=99
fi

if [ $RC -eq 0 ]
then
   echo "Login successful."
else
   echo "Failed to login."
fi

you have also tried to use the numeric test -eq to compare two strings, so results can be unpredictable on different platforms. As we are sure that 0 is numeric, don't quote it. Similarly, RC will always be numeric.


I hope that this is useful.


Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble with setting a variable with vastool

Hi I have this command that when put on the command line it returns the output the way I want it. /opt/quest/bin/vastool list -a groups | grep testdev_li | grep dev | awk -F"" 'NF>2{print $2}' | cut -c2- | tr '\n' '|' The output of this is ... (2 Replies)
Discussion started by: ajetangay
2 Replies

2. Shell Programming and Scripting

Trouble with variable and command assignment

I have a section of a script where I want to check a log file for a certain oracle error and if there is only one error (and it is ORA-39152) then I want to email that script is complete. Otherwise email failure and log. Somehow with this while the log only has one error and it is ORA-39152, I... (5 Replies)
Discussion started by: cougartrace
5 Replies

3. Shell Programming and Scripting

Trouble Assigning Variable with Function

OSX 10.9 Good morning/afternoon/evening. I'm hoping to get some insight on assigning a variable when calling a function. The code below looks at my array and checks if the path exists. My actual code will have multiple arrays and I would like to define a specific array when I call the... (6 Replies)
Discussion started by: sudo
6 Replies

4. Shell Programming and Scripting

Trouble reading content of file from a variable

Hi , i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help. I dont want to use cat command to read. Can we do it with out using cat command. while read line do... (9 Replies)
Discussion started by: Ravindra Swan
9 Replies

5. Shell Programming and Scripting

Trouble with variable assignment and reading in shell scripting

Hi, I have an inputfile with some table names in it. For ex: t_arnge t_profl t_fac I need a script which reads the line one by one and need to assign to some dynamic variable. If to explain the above example: i need some a=table_name1 table_name1=t_arnge in first time and... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

6. Shell Programming and Scripting

Trouble appending string constant to variable

Hi. I define my variables as: month=jul DD=17 YEAR=2012 transmission_file_name_only=test_$month$DD$YEAR_partial.dat However when I run my script the variable 'transmission_file_name_only' resolves to: File "/downloads/test_jul17.dat" not found. How can I append this '_partial'... (3 Replies)
Discussion started by: buechler66
3 Replies

7. Shell Programming and Scripting

Trouble with passing variable to sed

Here is my code #!/bin/bash username=gnowicki sed '$s/$/ $username/' < sshd_config 1 <> sshd_config what this is supposed to do is take the name gnowicki and put it at the end of the last line of the sshd_config and it works except not using the variable, if I put the name "gnowicki" where... (2 Replies)
Discussion started by: slufoot80
2 Replies

8. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

9. Shell Programming and Scripting

Trouble saving variable

Hi, I have problems when you save a variable of a command. I have put the following line: CONEXION_BAGDAD = $ (grep-c "Please login with USER and PASS" $ LOG_FILE_BAGDAD) But I returned the following error: syntax error at line 67: `CONEXION_BAGDAD = $ 'unexpected Because it can happen?... (2 Replies)
Discussion started by: danietepa
2 Replies

10. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies
Login or Register to Ask a Question