if else statement in a while command is not working... BASH - bash

#!/bin/sh
#datafile
#script
while echo enter name
read -r code
do
if [ "$code" = 'STOP' ];
then
break
else
grep "$code" datafile > outputres
while echo ' Would you like to see B OR R'
read -r answer
do
if [[ "$answer" = 'B' && "$code" = datafile ]];
then
echo show B.
break
elif [[ "$answer" = 'R' && "$code" = datafile ]];
then
echo show R
break
elif [[ "$answer" = 'B' && "$code" != datafile ]];
then
echo No such THING.
break
elif [[ "$answer" = 'R' && "$code" != datafile ]];
then
echo No such THING.
break
else :
echo Enter only B or R.
fi
done
fi
done
echo Goodbye!
Hello, I hope someone can help. but I'm having some issues when I execute my program I receive "no such thing" when it should display "show r".
UPDATE: I changed code to a number found in the data and it works, so I need a better way to say ("$code" = filename) number is in data or not in the data.
btw I have also checked if i have any errors and I dont

Related

if statement in script file does not return output

read code
read answer
if [[ $answer" = B && "$code" = grep $code filename ]]
then echo blue
else :
fi
done
Hello, if someone can help me understand why I am not receiving an output I would appreciate it. I am supposed to have two conditions in my if statement, so if the user gives a B and the file contains the "code" then it will echo blue
if [ $answer" = B && "$code" = '6314' ]
then echo blue
else :
fi
done
...
I do get an answer
Suggesting to try this:
if [[ "$answer" == B && "$code" == "$(grep $code filename)" ]]; then
echo blue
else
echo red
fi
Or:
if [[ "$answer" == B && "$code" == '6314' ]]; then
echo blue
else
echo red
fi
if [ "$answer" = "B" ] && [ "`grep \"$code\" filename`" = "$code" ]; then
echo code blue
else
echo code red
fi

Terminate shell script after three invalid input

Restricting user from trying multiple invalid attempt in shell scripting. I wrote the below script but somehow it's not getting me desire output. I have shared the script and script output both. Kindly help. Here I wanted script to terminate if user tried more than 3 times.
While true
do
echo -n "Enter yes or no"
read opt
case $opt in
yes) break ;;
no) break ;;
*) echo "Invalid input"
while [[ $err -le 3 ]]
do
If [[ $err -le 3 ]]
then
echo "err: $err"
((err++))
break
else
echo "Max limit crossed"
exit 1
fi
done
;;
esac
done
This was a nice question and I had a lot of fun solving it. I have to mention that I'm new to shell programming.
n=0
until [ $n -ge 3 ]
do
read line
if [ "$line" = "XYZ" ]; then
echo "Accepted"
break
else
n=$[$n+1]
echo " trying " $n "times "
fi;
done
This article helped me a lot to solve it.
Try:
#!/bin/bash
ANSWER=
max=3
while true; do
echo "Enter yes or no:"
read -r ANSWER
[[ $ANSWER == "yes" || $ANSWER == "no" ]] && break
echo Invalid Input
ANSWER=
((--max))
[[ $max -le 0 ]] && { echo "Max limit crossed"; exit 1; }
done

Error "[: too many arguments" from bash script

I have a bash script:
#!/bin/bash
pathToTestCasesFile=$1
while IFS=';' read -r col1 col2
do
if [[ $col1 == V[0-9]:* ]]; then
echo "decrypt"
if [[ "$(decrypt "$col1")" == "$col2" ]] ; then
echo "$col1 is equal to $col2"
else
echo "$col1 is not equal to $col2"
fi
else
echo "encrypt"
if [[ "$(encrypt $col1)" == "$col2" ]] ; then
echo "$col1 is equal to $col2"
else
echo "$col1 is not equal to $col2"
fi
fi
done < $pathToTestCasesFile
exit 0
Here is the test file:
alex;V1:IVjd9qcAbUrR954gyPDbKw==
V1:IVjd9qcAbUrR954gyPDbKw==;alex
The output looks like this:
encrypt
alex is not equal to V1:IVjd9qcAbUrR954gyPDbKw==
decrypt
V1:IVjd9qcAbUrR954gyPDbKw== is not equal to alex
decrypt
But the output should say that everything is equal.
I am sure that after the commands de -encrypt the value is equal to the other one. I tested it separately.
Maybe there is an issue with the comparisons..
Thanks a lot for helping.
This should work:
#!/bin/bash
while IFS=, read -r col1 col2
do
echo "I got|$col1|$col2|"
if [[ "$col1" =~ V.:.* ]]; then
echo "decrypt"
if [[ "$(decrypt "$col1")" == "$col2" ]] ; then
echo "[$col1] is equal to [$col2]"
else
echo "[$col1] is not equal to [$col2]"
fi
else
echo "encrypt"
if [[ "$(encrypt "$col1")" == "$col2" ]] ; then
echo "[$col1] is equal to [$col2]"
else
echo "[$col1] is not equal to [$col2]"
fi
fi
done < "$pathToTestCasesFile"
for the field separator is used the , comma - as in your code. In the example data you have ;. If you need ; change the IFS=, to IFS=';'.

IF condition in bash

I have just started learning to write bash scripts. This a simplified form of what I want to write.
The problem is despite of the input, it prints only "YES".
#! /usr/bin/bash
read input
if (("$input"== "y" || "$input" == "Y"))
then
echo "YES";
elif (("$input" == "n" || "$input" == "N"))
then
echo "NO";
else
echo "Not a valid input!!"
fi
Use [[ instead of (( like,
if [[ "$input" == "y" || "$input" == "Y" ]]
and also there must be a space exists before == operator.
ie,
input="n"
if [[ "$input" == "y" || "$input" == "Y" ]]
then
echo "YES";
elif [[ "$input" == "n" || "$input" == "N" ]]
then
echo "NO";
else
echo "Not a valid input!!"
fi
You could use regular expression also for condition checking purpose.
if [[ "$input" =~ ^[yY]$ ]]
then
echo "YES";
elif [[ "$input" =~ ^[nN]$ ]]
then
echo "NO";
else
echo "Not a valid input!!"
fi
When you automaticly convert the input to lowercase (using typeset), you do not have to bother with the the uppercases.
When you use an elif, always think 10 seconds for another solution. In this case you might want to use a "switch", in shell written as a case-statement:
#!/usr/bin/bash
typeset -l input
read input
case ${input} in
"y") echo "Yes";;
"n") echo "NO";;
*) echo "Not a valid input!!";;
esac

bash if logical boolean string

I am not looking for a different way to accomplish the apparent intention. I'm looking to understand why this exact syntax is not working.
[root#lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" == "n" ];then
> echo
> echo "bye"
> exit
> elif [ "$ans" != "" -o "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)? **"Should have continued"**
Invalid entry...
Would you like the script to check the second box ([y]n)? **"Should have continued"**
y
Invalid entry...
Would you like the script to check the second box ([y]n)? **"Correct behavior"**
alskjfasldasdjf
Invalid entry...
Would you like the script to check the second box ([y]n)? **"Correct behavior"**
n
bye
Here's a reference that's identical to so many others i found. I understand what it's doing, it's using the non logical's for AND and OR when everything I've read said that it should be using logical bools.
http://www.groupsrv.com/linux/about140851.html
Ok so here it is, with Nahuel's suggestion behaving how I had originally expected it to:
[root#lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" = "n" ];then
> echo
> echo "bye!"
> exit
> elif [ "$ans" != "" -a "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)?
asdfad
Invalid entry...
Would you like the script to check the second box ([y]n)?
[root#lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" = "n" ];then
> echo
> echo "bye!"
> exit
> elif [ "$ans" != "" -a "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)?
y
[root#lvs ~]# while true;do
> echo "Would you like the script to check the second box ([y]n)?"
> read ans
> if [ "$ans" = "n" ];then
> echo
> echo "bye!"
> exit
> elif [ "$ans" != "" -a "$ans" != "y" ];then
> echo "Invalid entry..."
> else
> break
> fi
> done
Would you like the script to check the second box ([y]n)?
n
logout
The problem is that : [ "$ans" != "" -o "$ans" != "y" ] is always true because of the or and the negation. $ans cannot be equal to "" and to "y".
Try replace these lines
if [ "$ans" == "n" ];then
elif [ "$ans" != "" -o "$ans" != "y" ];then
by these
if [ "$ans" = "n" ];then
elif [ "$ans" != "" -a "$ans" != "y" ];then
or these
if [[ $ans == n ]];then
elif [[ $ans != "" && $ans != y ]];then
The easier is to do is a case:
case $ans in
y) echo "yes"
;;
n) echo "no"
;;
*)
;;
esac
also break must be used only in a for or while loop, or in a select but it is missing in your post .
I don't really understand, why do you use -o in the elif. I would use "||" or "OR" operator. When you use two conditions in if, you should use double [[ and ]].
So if you use:
elif [[ "$ans" != "" || "$ans" != "y" ]];then
it works fine.
also logically its a flawed way of doing things.
firstly using case would be best in this scenario, secondly you are looking for == n then stating if it is blank or not equal to yes - so although no is caught out in first if statement in theory it would still meet second criteria
surely the most logical way to ensure input is 100% would be
if [ "$ans" == "n" ];then
echo
echo "bye"
exit
elif [ "$ans" == "y" ];then
echo Yes
break;
else
echo "Invalid entry... >$ans<"
fi

Resources