[Solved] Problem with if-then-else loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Problem with if-then-else loop
# 1  
Old 08-26-2013
[Solved] Problem with if-then-else loop

Hi,

i have a problem with this script:

Code:
for i in $(cat list_ip_switch)
do
        if
                if [ $i -eq "10.155.249.173" ]; then
                echo "found ip"
        else
                echo "not found ip"
        fi
done


Code:
cat list_ip_switch

10.155.249.171
10.155.249.172
10.155.249.173
10.155.249.174

# 2  
Old 08-26-2013
you have one if too much here but sure its a typo...
the problem comes from -eq which is normally for equal to, but for numeric value (integer...) which is not the case here (the shell is seeing a string...)
Im sure you replace by = and it should work...
# 3  
Old 08-26-2013
If input file is long, then it's better to use io redirect. I use always while + read when use file input.
Code:
while read i xstr
do
        if [  "$i"  =  "10.155.249.173" ]; then
                echo "found ip"
        else
                echo "not found ip"
        fi
done < list_ip_switch

Why read i xtsr ?
It's not have to, but:
if line include ip and something else then 1st value is in the i and rest of line is in the xtsr.
# 4  
Old 08-26-2013
thanks works!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] For loop help

Hello, This is really breaking my head. I request you help to solve this problem. I have a list of files at the source directory (/tmp) as below, NewTransfer_20131202_APAC.dat NewTransferFile_20131202_APAC.dat NewTransfer_20131203_APAC.dat NewTransferFile_20131203_APAC.dat... (3 Replies)
Discussion started by: sravicha
3 Replies

2. Shell Programming and Scripting

[Solved] FOR loop / IF statement returning error

The code at the bottom is a simplified example of what we have. If I use the following: && echo "echo failed" $? returns 1 When I use if ; then echo "echo failed" ; fi $? returns 0 Does anyone know what's wrong with this? Using AIX 6.1 and KSH for NUM in 1 2 3 do ... (5 Replies)
Discussion started by: jfxdavies
5 Replies

3. Shell Programming and Scripting

[solved] Process ssh command in while loop

I have a script that reads a file containing a list of server names. It's suppose to loop through the list of names and execute a command on the remote server using ssh. It processes the ssh command for the first server in the list and then exits. Here's the code: #!/bin/bash ... (2 Replies)
Discussion started by: westmoreland
2 Replies

4. Shell Programming and Scripting

[Solved] Simple script not working- for loop

Hi all, Please guide me writing this script Follwing is the file which I have created, which contains the files to be copied. cat system1-dr.txt /etc/passwd /etc/shadow /etc/group /etc/vfstab /etc/profile /etc/default/init /etc/shells /etc/dfs/dfstab /etc/dfs/sharetab... (11 Replies)
Discussion started by: manalisharmabe
11 Replies

5. Shell Programming and Scripting

[solved] Question for using variables outside a while loop

I want to get newvar outside the while any ideas? while read myline; do var=${myline} newvar1=$(let "$var") done echo $newvar1 I found it its ok now Thank you! (0 Replies)
Discussion started by: sanantonio7777
0 Replies

6. UNIX for Dummies Questions & Answers

[Solved] For Loop with 2 commands? (hostname prob)

Hi I would like to ssh into serveral servers, print the hostname and do an ls (of files in a certain directory), and add these details to a file. so i have this: #!/usr/bin/bash for host in hostnames_here do ssh $host echo "$HOSTNAME" ls /directory_location > ~/new_file_name.txt ... (6 Replies)
Discussion started by: horhif
6 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Syntax error for awk in a loop

can some one please tell me what is the problem with my syntax:confused: I have 100 files in one folder 1. want to read each of the line by line 2. calculate their number of the words between the first word and the last word of each line 3. create file for each file with number of words... (8 Replies)
Discussion started by: A-V
8 Replies

8. Shell Programming and Scripting

[Solved] Use of until loop for user confirmation

Below is my script that is using to rename the name of file .Here I am using two methods to pass the both arguments wih script name or run the script and give the input one by one.But my issue is I want to rename the name of the file if user select Y(y) then it should rename the file else select... (4 Replies)
Discussion started by: anuragpgtgerman
4 Replies

9. UNIX for Dummies Questions & Answers

[Solved] Simple while loop does not exit

spath="/home/user/k/${1}" dpath="/home/user/seq/Nov17/${1}" cd $dpath ls -1 $spath > list c=1 while read list newlist=`echo $list | sed 's/.gz//' ` newnewlist=`echo $newlist | sed 's/.fastq//' ` do echo $c echo $list c=$(($c+1)) (6 Replies)
Discussion started by: analyst
6 Replies

10. Shell Programming and Scripting

[SOLVED] for loop to process files

I need to process a dirtree containing ms office files such that each file is stored as a variable and also, just the file file stem. Why? They will be using as input and output parameters for another script. For example /path/to/second_script -i filename.docx -o filename Here's what I... (1 Reply)
Discussion started by: graysky
1 Replies
Login or Register to Ask a Question