If variable equals string help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If variable equals string help
# 1  
Old 01-14-2015
If variable equals string help

Hi All,
Trying to get my bash script if statement to work however my if variable equals xxx statement doesnt appear to work, could anyone shed some light.

Code:
./script $password &> output
INCORRECTPASS=`grep "Permission denied, please try again." output`
        echo "$INCORRECTPASS"
        if [[ $INCORRECTPASS == "Permission denied, please try again." ]]; then
        echo "Incorrect Password"
fi

As you can see i have put in an echo to print the variable set and this prints sucessfully so I know for sure the variable exists.

Thanks
# 2  
Old 01-14-2015
Would have been wise to post the error msg as well, so we don't have to guess... My guess is, if sees too many words in the first operand. Try quoting the variable.
# 3  
Old 01-14-2015
Quote:
Originally Posted by RudiC
Would have been wise to post the error msg as well, so we don't have to guess... My guess is, if sees too many words in the first operand. Try quoting the variable.
There isnt an error message, the script completes as if the variable didnt match the if statement. Quoting the variable also doesnt work.
# 4  
Old 01-14-2015
Run it with the shell options -v and -x set and post the output.
# 5  
Old 01-14-2015
Quote:
Originally Posted by RudiC
Run it with the shell options -v and -x set and post the output.
Hi RudiC as requested.

Code:
#!/bin/bash
clear
+ clear
echo -e "\033[1;94m";
+ echo -e '\033[1;94m'
echo "Checking input file"
+ echo 'Checking input file'
Checking input file
if grep -nv '^44[0-9]\{10\}$' numberlist.txt
        then
                echo -e "\033[1;31m";
                echo 'Please check the above entries in the numberlist.txt file.'
                echo -e "\033[1;37m";
                exit
fi
+ grep -nv '^44[0-9]\{10\}$' numberlist.txt
sleep 1
+ sleep 1
echo "***************************************************************************************************************"
+ echo '***************************************************************************************************************'
***************************************************************************************************************
echo "The Script is currently in progress, exiting from this window will stop the script and cause it not to complete"
+ echo 'The Script is currently in progress, exiting from this window will stop the script and cause it not to complete'
The Script is currently in progress, exiting from this window will stop the script and cause it not to complete
echo "***************************************************************************************************************"
+ echo '***************************************************************************************************************'
***************************************************************************************************************
VAR=`grep -c "44" numberlist.txt`
grep -c "44" numberlist.txt
++ grep -c 44 numberlist.txt
+ VAR=1
echo "The amount that will be checked is $VAR"
+ echo 'The amount that will be checked is 1'
The amount that will be checked is 1
./msscript $1 &> expectoutput
+ ./msscript random
INCORRECTPASS=`grep "Permission denied, please try again." expectoutput`
grep "Permission denied, please try again." expectoutput
++ grep 'Permission denied, please try again.' expectoutput
' INCORRECTPASS='Permission denied, please try again.
echo "$INCORRECTPASS"
' echo 'Permission denied, please try again.
Permission denied, please try again.
        if [[ "$INCORRECTPASS" == "Permission denied, please try again." ]]; then
        echo "Incorrect Password"
fi
 == \P\e\r\m\i\s\s\i\o\n\ \d\e\n\i\e\d\,\ \p\l\e\a\s\e\ \t\r\y\ \a\g\a\i\n\. ]]
cat expectoutput | tr -d $'\r' | awk '{print}' ORS=', ' | grep -o 'MS ??[^\n]*' | sed 's/\--//g' | sed 's/\Please e//g' | sed 's/\--//g' |  sed 's/;.*//' | sed 's/\MS ??,//g' | sed 's/\ //g' > intialout
+ cat expectoutput
+ tr -d $'\r'
+ awk '{print}' 'ORS=, '
+ grep -o 'MS ??[^\n]*'
+ sed 's/\--//g'
+ sed 's/\Please e//g'
+ sed 's/\--//g'
+ sed 's/;.*//'
+ sed 's/\MS ??,//g'
+ sed 's/\ //g'
echo "CREATING CSV FILE CALLED relay-finaloutputfile.csv"
+ echo 'CREATING CSV FILE CALLED relay-finaloutputfile.csv'
CREATING CSV FILE CALLED relay-finaloutputfile.csv
#rm expectoutput
#rm intialout

# 6  
Old 01-14-2015
Are you sure there's no DOS <CR> line terminator in the grep output?
# 7  
Old 01-14-2015
Sorted it by just using the below however I would still like to understand why it didnt match the pattern.

Code:
INCORRECTPASS=`grep -c "Permission denied, please try again." expectoutput`
        if [[ "$INCORRECTPASS" == "1" ]]; then

---------- Post updated at 02:14 PM ---------- Previous update was at 02:11 PM ----------

Quote:
Originally Posted by RudiC
Are you sure there's no DOS <CR> line terminator in the grep output?
On looking at the file in Vi this is the line (not sure if the ^M is causing an issue)
Code:
Permission denied, please try again.^M

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Until string from remote command equals value run remote command

I solved my issue by using the following code #!/bin/bash function GET_STATUS { #values Active Passive Failed ssh -a localhost '/home/user/fakecommand.sh' } STATE="unknown" until ] do echo $STATE sleep 5 STATUS=`GET_STATUS` echo $STATUS | grep Active &&... (1 Reply)
Discussion started by: $scipt_Kid
1 Replies

3. UNIX for Dummies Questions & Answers

awk if statement / equals operator

Hi, I was hoping someone could explain this please :) I'm using bash, scientific linux... and I don't know what else you need to know. With awk '{ if( 0.3 == 0.1*3) print $1}' file.dat nothing will be printed since apparently the two numbers do not equate. (Using 0.3 != 0.1*3 is seen... (4 Replies)
Discussion started by: Golpette
4 Replies

4. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

5. Shell Programming and Scripting

[solved] how to check if two arrays are equals?

how to compare to arrays to check if each elements of the first are the same of the second? for ((i=0;i<$LENGTH;i++)) ; do for (j=0;j<$LENGTH;j++)) ; do if } == ${ARR2} ] echo "Are the same"; fi; done; done; i try this but it doesn't work :( if i make... (0 Replies)
Discussion started by: tafazzi87
0 Replies

6. Shell Programming and Scripting

How to output the partially equals

Hello i have 2 files: a.out 10.1.1.1 james.franco 10.1.1.3 google.gol 10.1.1.14 yahoo.bol b.out 10.1.1.1 10.1.1.3 10.1.1.45 I need to see an output just with: 10.1.1.1 james.franco 10.1.1.3 google.gol Thankz in advance!! (2 Replies)
Discussion started by: danielldf
2 Replies

7. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

8. Shell Programming and Scripting

Compare 3 files, delete data equals.

Hi, i have a problem, I have three files, file_1, File_2 file_3 and I need to compare the data with file_3 file_1, data that are equal to file_3 file_1 should be deleted, file_1 receive data and file_2 file_3. Ex: file_1 374905,2001, Selmar Santos, Técnico de Sistemas, U$3.000,00 789502,... (3 Replies)
Discussion started by: selmar
3 Replies

9. Shell Programming and Scripting

How TO: if [ Not Equals ] - Solaris 8

Hi, I have the following script which runs well :- ls -l /etc/*.txt > /dev/null 2>&1 if ; then "Success" fi But, if I try, if ; then "Success" fi Does not works ! Even, (4 Replies)
Discussion started by: angshuman_ag
4 Replies

10. Shell Programming and Scripting

only if column1 equals this print that

I have text file with hundreds of lines, space delimited, each line has the same amount of "columns" and the same amount of characters in each, Column 1, Column 2, and Column 3. I need a script that will print all columns of the "current" line along with the last two columns of the next line ONLY... (3 Replies)
Discussion started by: ajp7701
3 Replies
Login or Register to Ask a Question