I can't figure how to detect equality and return equal vars, tried many way with this thread
tag="AA"
prst_tag[1]="BB"
prst_tag[2]="CC"
prst_tag[3]="AA"
prst_tag[4]="EE"
What i exactly want to do:
if $tag or ${prst_tag[1]} or ${prst_tag[2]} or ${prst_tag[3]} or ${prst_tag[4]} have equal value; then
echo "equal TAG found"
echo "tag: $tag"
echo "prst_tag[1]: ${prst_tag[1]}"
echo "prst_tag[2]: ${prst_tag[2]}"
echo "prst_tag[3]: ${prst_tag[3]}"
echo "prst_tag[4]: ${prst_tag[4]}"
fi
Help appreciated
I really didn't understand your question, but i assume you want to compare strings. Here is example how to compare string with array of strings:
tag="AA"
prst_tag[1]="BB"
prst_tag[2]="CC"
prst_tag[3]="AA"
prst_tag[4]="EE"
found=false
for i in "${prst_tag[#]}"
do
if [ "$tag" = "$i" ]; then
found=true
fi
done
if [ "$found" = "true" ];then
echo "equal TAG found"
else
echo "equal TAG not found"
fi
Related
I am a beginner at unix and I am trying to use a while loop to get a user integer input for 2 numbers, but I need to check if it is an integer and reask if it's not, so I attempted to utilize if statements inside the while loop. I cannot get this to work, what am I doing wrong with the while and if loop?
#! /bin/bash
echo “Enter first number:“
while read number1
do
if[[ $number1 ]] && [ $input -eq $input 2>/dev/null ]
then
echo “$number1”
else
echo "$number1 is not an integer or not defined.Try again”
fi
done
echo “Enter second number:“
while read number2
do
if[[ $number2 ]] && [ $input -eq $input 2>/dev/null ]
then
echo “$number2”
else
echo "$number2 is not an integer or not defined.Try again”
fi
done
If you are trying to get the number and checking it until it becomes integer, Please try the below code.
#!/bin/bash
echo "enter number"
while read number1
do
if ! [[ $number1 =~ ^[0-9]+$ ]]
then
echo "number is not an integer"
else
echo "number is an integer"
exit;
fi
done
I think i'm missing something very obvious. But shouldn't the following code produce the opposite response? I thought if the statement "if s == d" is used and s is not equal to d then the if statement should return false and not run the following code. This is not what appears to happen. Can anyone explain what i've missed. I think it's something very obvious.
Thanks
s=2
d=3
if ! [ "$s == $d" ]; then echo "hello"; fi
if [ "$s == $d" ]; then echo "hello"; fi
hello
You are quoting the entire string "$s == $d" when you should be quoting the two arguments "$s" and "$d".
This means that instead of comparing $s to $d, you are checking whether "2 == 3" is a non-empty string (which it is).
This will correctly print "not equal":
s=2
d=3
if ! [ "$s" == "$d" ]; then echo "not equal"; fi
if [ "$s" == "$d" ]; then echo "equal"; fi
if [[ $DATA == *?xml* ]]
then
if [[ $DATA == *misuse* ]]
then echo "Misuse" >> $OUTPUTPAST2
else echo "All Good" >> $OUTPUTPAST2
fi
else echo "Not xml" >> $OUTPUTPAST2
fi
Where $DATA does not contain the string ?xmlI am expecting an output of Not xml, but I am getting output of All Good.
What am I doing wrong?
? is special character in glob which means match any single character.
Use it like this:
if [[ "$DATA" == *'?xml'* ]]
then
if [[ "$DATA" == *misuse* ]]
then echo "Misuse" >> $OUTPUTPAST2
else echo "All Good" >> $OUTPUTPAST2
fi
else
echo "Not xml" >> $OUTPUTPAST2
fi
While quoting the question mark will suffice to solve your immediate problem, this looks like code which really really wants to be a case statement instead.
case $DATA in
*'?xml'*misuse* | *misuse*'?xml'*)
echo Misuse ;;
*'?xml'*) echo All Good ;;
*) echo Not xml;;
esac >>$OUTPUTPAST2
Notice also how the redirection can go after the entire block, to avoid a lot of repetition.
(If "misuse" can only go after the "?xml" marker, you can simplify.)
I am looking for suggestions for a solution and on best approaches to handle figuring out if multiple IFs are null.
I have:
if [ -n "$sfcompname" ]; then
echo $sfcompname
fi
if [ -n "$sfcompip" ]; then
echo $sfcompip
fi
if [ -n "$lacompname" ]; then
echo $lacompname
fi
if [ -n "$lacompip" ]; then
echo $lacompip
fi
.. I'm sure that can be done better, but my main problem at the moment is trying to then say:
if (all those IFs) = null
echo "Please check the name you entered and try again"
Somewhat silly, but should work
if ! [[ ${sfcompname}${sfcompip}${lacompname}${lacompip} ]]
then
echo "Please check the name you entered and try again"
fi
You can use another variable for this which you initialise to a value and then change if any of the if statements fire. Then at the end, if it hasn't changed, then you know that none of them fired. Something like this will suffice:
fired=0
if [ -n "$sfcompname" ]; then
echo $sfcompname
fired=1
fi
if [ -n "$sfcompip" ]; then
echo $sfcompip
fired=1
fi
if [ -n "$lacompname" ]; then
echo $lacompname
fired=1
fi
if [ -n "$lacompip" ]; then
echo $lacompip
fired=1
fi
if [[ ${fired} -eq 0 ]] ; then
echo 'None were fired'
fi
Another possibility is to use the variable check short-cut:
name="$sfcompname$sfcompip$lacompname$lacompip"
${name:?"Please check the name you entered and try again"}
This will exit the program if none of the variables are set. The message is optional, it overrides the standard "parameter null or not set".
For follwoing script
#!/bin/sh
count1=0
noOfArg=0
checkOtherParam()
{
echo $parameter
return 4
}
if($count1 eq $noOfArg)
then
echo "Yes"
else
echo "No
"
fi
~
I am getting error
./sample.sh: 0: not found
No
please let me know, what is the problem
I would write the comparison like this
if (($count1 == $noOfArg))
then
...
fi
The above is an arithmetic comparison, as opposed to the conditional comparison performed by
if [ $count1 -eq $noOfArg ]
then
...
fi
However in this case I'm assuming they would both produce the same result.
It's your use of parenthesis in the if statement. That's not proper bash syntax. Here it is corrected (sans the mysterious broken checkOtherParam function):
#!/bin/sh
count1=0
noOfArg=0
if [ $count1 -eq $noOfArg ]
then
echo "Yes"
else
echo "No"
fi
if($count1 eq $noOfArg)
should be
if [ $count1 -eq $noOfArg ]
you can also use case/esac
case "$count" in
"$noOfArg" ) echo "yes";;
*) echo "no";;
esac