Problems with if condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with if condition
# 8  
Old 06-10-2014
@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)..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 9  
Old 06-10-2014
Quote:
Originally Posted by RudiC
Try
Code:
X="¦"${server// /¦}"¦"
[ "${X}" != "${X/¦$input¦/}" ] && echo OK || echo NOK

EDIT: or
Code:
X=" $server "
[ "${X}" != "${X/ $input /}" ] && echo OK || echo NOK

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 " ")

Last edited by Scrutinizer; 06-10-2014 at 11:12 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 10  
Old 06-10-2014
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)

Code:
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

This User Gave Thanks to clx For This Post:
# 11  
Old 06-10-2014
Thanks. Use the "Use Default Values" parameter expansion, then:
Code:
[ "${X}" != "${X/ ${input:-x} /}" ] && echo OK || echo NOK

# 12  
Old 06-10-2014
Thanks clx, that was as paste-o Smilie corrected it in my post..
# 13  
Old 06-11-2014
Thanks all but nothing works

Code:
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 :

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

SmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilieSmilie

Last edited by Scrutinizer; 06-11-2014 at 07:56 AM.. Reason: Additional code tags
# 14  
Old 06-11-2014
Hello Friend
try
Code:
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


Last edited by Makarand Dodmis; 06-11-2014 at 10:15 AM..
This User Gave Thanks to Makarand Dodmis For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. Shell Programming and Scripting

if condition

if chr1:109457160 1 109457160 99.1735537190083 + chr1:109457233 1 109457233 99.1735537190083 - chr1:109457614 1 109457614 99.1735537190083 + chr1:109457618 1 109457618 100 + chr1:109457943 1 109457943 100 - chr1:109458224 1 109458224 99.1735537190083 - file1.txt If 6th column in... (3 Replies)
Discussion started by: johnkim0806
3 Replies

3. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

4. Shell Programming and Scripting

If condition

Hi All, I am using the below if command to comparing two variables but is condition is true but going in else part. ################# if ; then TAKE_SNAPS echo "Snaps updates done" UPDATE_DS_SNAPS_TAKEN else echo "Seemes DS & JDP is still running KIndly... (4 Replies)
Discussion started by: ajaincv
4 Replies

5. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

6. Shell Programming and Scripting

if condition

Hi how to write this: if then usage fi thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

7. Shell Programming and Scripting

if condition ...

i have following if condition if above statement is case sensitive.....what is syntax if i have to make above comparision case insensetive (4 Replies)
Discussion started by: mahabunta
4 Replies

8. UNIX for Dummies Questions & Answers

if condition

suppose a name is read how to check whether the name contains only alphabets and space not non-alphabetic characters and special characters (5 Replies)
Discussion started by: manisha_agrawal
5 Replies

9. UNIX for Dummies Questions & Answers

help with if condition

Does anybody know when the following if condition is true (ie. what it means) if ($1 ~ "^") thnx :) rkap (2 Replies)
Discussion started by: rkap
2 Replies

10. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies
Login or Register to Ask a Question