Problems with if condition

My code:

server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 "
printf "\nCheck server: "
read input
if [ $input == $server ]
then

and the problem is "$input == $server" is wrong :(:(:(. Can someone fix this for me. I don't want to use || for all these servers.
Thanks

try

servers="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 "
for server in $( echo $servers | nawk '{A[++c] = $0} END { for ( i = 1; i <= c; i++ ) { print A }}' RS=' ' );
do
read input
if [ $input == $server ]
then
   echo "Success"
else
   echo "Failure"
fi
done
1 Like

Why not grep for $input in $server?

$ server="ix1 ix2 bx1 bx2"

$ echo $server
ix1 ix2 bx1 bx2

$ var1="ix1"

$ echo $var1
ix1

$ echo $server | grep $var1
ix1 ix2 bx1 bx2

$ var1="ix3"

$ echo $server | grep $var1

$
1 Like

Try

[ "${server}" != "${server/$input//}" ] && echo OK || echo NOK
1 Like
server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 "
for i in $server
do
  if [ "$i" = "$input" ]; then
    echo hello
  fi
done
case $server in
  ("$input "*|*" $input "*|*" $input")
     echo hello
esac

--
@RudiC @joeyg. There is a potential mismatch if there is a printer called icapp40 in the list for example or vice versa..

--edit--
Added spaces around equal sign, thanks clx..

4 Likes

Not sure, whether you want something like this ?

#!/bin/bash

server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 "

# -p "Prompt" : Display prompt to user without a newline 
read -p "Check Server : " server_name 


# i -> stands for upper/lower case 
# w -> --word-regexp

if grep -iw "$server_name" <<<"$server" >/dev/null; then
	echo "Yes $server_name found in server list"
         # some more code...
else
	echo "No $sever_name not found in server list"
        # some more code...
fi
1 Like

Try

X="�"${server// /�}"�"
[ "${X}" != "${X/�$input�/}" ] && echo OK || echo NOK

EDIT: or

X=" $server "
[ "${X}" != "${X/ $input /}" ] && echo OK || echo NOK
1 Like

@Akshay, there is a problem if server_name is empty, so there would need to be an additional test for that.

--
Another note is that you can use -q instead of >/dev/null and -w is not available in every grep (but it is in most)..

2 Likes

Hi RudiC, in the first case, it seems to only work properly if the variable is quoted "${X/�"$input"�/}" . I haven't figured out yet why there behavior is different. It is advisable to quote variables within variable expansions anyway. But then in both cases there either needs to be an additional test for an empty input string, or X should not contain two adjacent field separators ( || or " " )

2 Likes

Just to be specific about scrutinizer's post for possible typo, make sure you have spaces around "==" in post #5, else it would be true always (check for null)

server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 "
for i in $server
do
  if [ "$i" == "$input" ]; then
    echo hello
  fi
done
1 Like

Thanks. Use the "Use Default Values" parameter expansion, then:

[ "${X}" != "${X/ ${input:-x} /}" ] && echo OK || echo NOK

Thanks clx, that was as paste-o :slight_smile: corrected it in my post..

Thanks all but nothing works

server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 o4 o5 o6 m2 ic_ei ih_ei droltpn1 droltpn2 drmisn2 drmisn2"
printf "\nEnter server: "
read input
if [ $input == $server ]
then
clear
echo "\n"
echo "Hostname: $input"
echo "IP      :`ping $input|head -1|cut -d':' -f2|sed s/\(//|sed s/\)//`"
echo "\n"
sleep 1
else
echo "Wrong input"
sleep 1
Main_chk
fi

and now I have to do like this :

if [ $input == "icapp1" ] || [ $input == "icapp6" ] || [ $input == "ihapp1" ] || [ $input == "ihapp6" ] ......................................

:(:(:(:(:(:(:(:(:(:(:(:(:frowning:

Hello Friend
try

flag=0
server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 o4 o5 o6 m2 ic_ei ih_ei droltpn1 droltpn2 drmisn2 drmisn2"
printf "\nEnter server: "
read input
for i in $server
do
  if [ "$i" == "$input" ]; then
    flag=1
    break;
  else
    flag=0
  fi
done
 
if [ $flag -eq 1 ]; then
clear
echo "\n"
echo "Hostname: $input"
echo "IP      :`ping $input|head -1|cut -d':' -f2|sed s/\(//|sed s/\)//`"
echo "\n"
sleep 1
else
echo "Wrong input"
sleep 1
Main_chk
fi
1 Like

What have you tried and what did not work? Of course most of the examples in this thread are principles/coding practices that you would need to adjust a little for your own application.

1 Like

Hello Scrutinizer

He have "else" condition in his code so for "correct" input for loop will print "correct input" once and "wrong input" n-1 times.

i have added flag logic and modified the code.Hope it will help him.

1 Like

Try :

#!/bin/bash

server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 "

# -p "Prompt" : Display prompt to user without a newline 
read -p "Check Server : " server_name 


dothis(){
clear
echo ""
echo "Hostname: $server_name"

# I don't know what you are doing here...
echo "IP      :`ping $server_name |head -1|cut -d':' -f2|sed s/\(//|sed s/\)//`"
echo ""
sleep 1

}



for i in $server
do
  if [ "$i" = "$server_name" ]; then
    dothis; exit
  fi
done

echo "Wrong input"
sleep 1
Main_chk
1 Like

I tried all and I adjusted a little as well, but maybe in a wrong way :mad::mad::mad:.

Anyway, Makarand Dodmis's script worked. Thanks guys :D:D:D

1 Like
server="icapp1 icapp6 ihapp1 ihapp6 icapp2 icapp3 icapp4 ihapp2 ihapp3 ihapp4 icapp5 ihapp5 "
printf "\nCheck server: "
read input
if [[ " $server " == *" $input "* ]]
then

I.e. you compare the list (surrounded by extra delimiters) with the item (surrounded by delimiters and * wildcards).
NB the standard defines the match operator == only for [[ ]]