LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2016, 07:13 AM   #1
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Rep: Reputation: Disabled
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?

Last edited by chewinghum; 02-09-2016 at 08:51 AM. Reason: no code tags
 
Old 02-09-2016, 07:27 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 02-09-2016, 07:32 AM   #3
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.

Last edited by chewinghum; 02-09-2016 at 07:47 AM.
 
Old 02-09-2016, 07:58 AM   #4
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-09-2016, 08:02 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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_!

Last edited by michaelk; 02-09-2016 at 08:04 AM.
 
Old 02-09-2016, 08:06 AM   #6
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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?

Last edited by chewinghum; 02-09-2016 at 08:51 AM.
 
Old 02-09-2016, 08:12 AM   #7
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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

Last edited by chewinghum; 02-09-2016 at 08:50 AM. Reason: incomplete
 
Old 02-09-2016, 08:17 AM   #8
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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 "!"

Last edited by chewinghum; 02-09-2016 at 08:50 AM.
 
Old 02-09-2016, 08:17 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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

Last edited by michaelk; 02-09-2016 at 08:23 AM.
 
Old 02-09-2016, 08:25 AM   #10
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
check this out:

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

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

Last edited by chewinghum; 02-09-2016 at 08:50 AM.
 
Old 02-09-2016, 08:33 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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.
 
Old 02-09-2016, 08:34 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
 
1 members found this post helpful.
Old 02-09-2016, 08:45 AM   #13
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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

Last edited by chewinghum; 02-09-2016 at 08:49 AM.
 
Old 02-09-2016, 08:51 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,759

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
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?
 
Old 02-09-2016, 08:56 AM   #15
chewinghum
LQ Newbie
 
Registered: Feb 2016
Posts: 15

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Working Expect script failing when placed inside bash Loop. Sigmaflux Linux - Newbie 1 01-27-2014 10:51 PM
[SOLVED] bash loop not working as expected... replica9000 Linux - Software 10 04-24-2011 09:08 AM
Bash for loop not working cryptinitedemon Linux - General 5 04-24-2010 10:44 AM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 10:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration