LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   loop not working in bash (https://www.linuxquestions.org/questions/linux-newbie-8/loop-not-working-in-bash-4175571780/)

chewinghum 02-09-2016 07:13 AM

loop not working in bash
 
Hi people,

I have a small bash script in which i use "while" or "for" loop and none of them are working.
This below is the part that's giving me head aches:

Code:

if [ "$1" == "z" ] && [ ! -z "$2" ] ; then
        no=1; end=100
        while [ $no -lt $end ] ; do
                if [ ! -f "file.$no" ] ; then
                        break
                fi
        done
        echo "$no"
fi

so this above sais that if argument 1 is "z" and argument 2 exists, loop until find file named file.$no
and break and of course remember the last $no.

when searching for file.$no it loops from 1 to 100 and the first file it finds is file.8 so it should return no=8

why is it returning no=1 ??

i tried for loop like this:
Code:

if [ "$1" == "z" ] && [ ! -z "$2" ] ; then
        no=1; end=100
        for (( $no; $no<$end; $no++ )) ; do
                if [ ! -f "file.$no" ] ; then
                        break
                fi
        done
        echo "$no"
fi

and same result ... no=1

Can someone please help me with this?

zhjim 02-09-2016 07:27 AM

First things first. Please use code tag so the code is more readable.

I doubt that the first file it finds is file.8. Lets assume the if clause is true and we have our variables no and end set. We then go to the while and loop for ever. Our exit from the while loop is the break if file.$no is not found. Do you have a file named "file.1"? I doubt it. Because if oyu would the while loop would be endless. But why. Probably cause you dont increment $no, no?

I did not look at the second move. Specially cause you are looking for ips and not file. Use some set -x at the start of your script to see what bash actually does. There are some more bash options to debug things. You might also use some echo commands to see what the content of various variables are.

chewinghum 02-09-2016 07:32 AM

That is what the script should do... find "file.8" because file.1 to file.7 don't exist, and then break with last $no. How can i do this?
What am i doing wrong man ? i tried everything.

chewinghum 02-09-2016 07:58 AM

I did everything by the book, googled for every statement and copied what experts wrote. if you can't write the solution at least give me some hints.

michaelk 02-09-2016 08:02 AM

Here is a hint.

Quote:

if [ ! -f "file.$no" ]
This means true if the file does not exist. Since file.1 does not exist the statement is true and the script stops. As stated without a statement to increment your counter variable (no) in your first script it will always be equal to one.

https://bash.cyberciti.biz/guide/Logical_Not_!

chewinghum 02-09-2016 08:06 AM

Code:

if [ "$1" == "z" ] && [ ! -z "$2" ]
--> this is good i tested it with echo and returned $no and $end correctly,
Code:

for (( $no; $no<$end; $no++ ))
--> this is good also
Code:

if [ ! -f "file.$no" ]
--> i think this is the problem but how else can i do it?
If file exists then ... what is wrong?

chewinghum 02-09-2016 08:12 AM

Okay,

tried it like this

Code:

if [ "$1" == "z" ] && [ ! -z "$2" ] ; then
        no=1; end=100
        for (( $no; $no<$end; $no++ )) ; do
                echo "$no"
                if [ -f "file.$no" ] ; then
                        break
                fi
        done
fi

result:

Code:

line 13: ((: 1++ : syntax error: operand expected (error token is "+ ")
this is line13:
Code:

for (( $no; $no<$end; $no++ )) ; do
and for this:

Code:

if [ "$1" == "z" ] && [ ! -z "$2" ] ; then
        no=1; end=100
        while [ $no -lt $end ] ; do
                echo "$no"
                if [ -f "file.$no" ] ; then
                        break
                fi
        done
fi

it outputs 1 without stopping

chewinghum 02-09-2016 08:17 AM

Code:

[ ! -z "$2" ]
--> why this means "if argument 2 exists" if it has "!" inside it?
and
Code:

[ ! -f "file.$no" ]
--> this means "if file does not exist" ?

they are written the same with "!"

michaelk 02-09-2016 08:17 AM

Code:

if [ condition ]; then
  statements
fi

Means execute the statements if the condition is true. In your case the -f means true if the file exist however the ! is a logical not so the condition is reversed and the statements are executed if the condition is not true i.e. file does not exist.

The [ ! -z $2 ] means true if the second command line argument length is not zero.

http://www.tldp.org/LDP/Bash-Beginne...ect_07_01.html

chewinghum 02-09-2016 08:25 AM

check this out:

http://stackoverflow.com/questions/6...h-shell-script

Code:

if [ -z "$1" ]
  then
    echo "No argument supplied"
fi


michaelk 02-09-2016 08:33 AM

Code:

no=1; end=100
while [ $no -lt $end ] ; do
echo "$no"

done
fi

The value of $no never changes so the loop never quits. You need to add a statement to increment $no.

grail 02-09-2016 08:34 AM

So if i understand correctly, you want to find the first file that does exist, ie. file.8 which would then have a value of 8 assigned to 'no'?

I would tackle the logic a little differently:
Code:

if [[ "$1" == "z" && -n "$2" ]] ; then
  end=100
  for (( no = 1; no < end; no++ )) ; do
    [[ -f "file.$no" ]] && break
  done
  echo "$no"
fi


chewinghum 02-09-2016 08:45 AM

i ran this:

Code:

if [[ "$1" == "z" && -n "$2" ]] ; then
        end=100
        for (( no = 1; no < end; no++ )) ; do
                echo "$no"
                [[ -f "file.$no" ]] && break
        done
fi

and it echoed numbers from 1 to 99 and stopped, this means it ignores

Code:

[[ -f "file.$no" ]] && break

michaelk 02-09-2016 08:51 AM

The point is to teach the OP about conditionals and not spoon feed the answer.

Code:

if [ "$1" == "z" ] && [ ! -z "$2" ]; then
This means true if $1 equals z and $2 string length is not zero. Are you planning on using $2 for anything?

chewinghum 02-09-2016 08:56 AM

this is the same as my last reply

Code:

if [ "$1" == "z" ] && [ ! -z "$2" ] ; then
        end=100
        for (( no = 1; no < end; no++ )) ; do
                echo "$no"
                [[ -f "file.$no" ]] && break
        done
fi

this line is ignored:

Code:

[[ -f "file.$no" ]] && break


All times are GMT -5. The time now is 06:04 AM.