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
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..
#!/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
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 " " )
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
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.
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 [[ ]]