Do not understand why my while loop fail (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Do not understand why my while loop fail (bash)
# 1  
Old 02-17-2010
Do not understand why my while loop fail (bash)

Hi again Smilie
I still need your help now...


Code:
#!/bin/bash

SIZE=`ls -s text.txt`

while [ "$SIZE" != '0 text.txt' ]
do
[ my intructions ]
done

My problem is that the line "while ..." still print the same value after the first loop.
In [ my instruction ] one instruction change the size of text.txt
I've run my bash script in debug mode, and the problem is not here...

Can you help me please ?

Thanks Smilie
# 2  
Old 02-17-2010
Hi.

It will always print the same value. You set the variable "SIZE" before the loop, and never update it.

You can apply the same logic here, as in your previous post.

i.e.

Code:
while test -s somefile.txt; do
  file is not empty...
done

You also didn't listen about quoting your variable, as was mentioned in your previous post.
# 3  
Old 02-17-2010
Sorry about the quotes Smilie

I don't understand, my SIZE variable isn't executed when my script come back to the while line ?

I can't put the line SIZE =` ls -s text.txt` after the while because it will not work.
Shloud I have to update my SIZE variable in the end of the script, just before the done ?
I write basic bash script since yesterday, I have not done such thing yet
# 4  
Old 02-17-2010
Hi.

I think what I was trying to say is, you don't need to execute the ls command to find out if a file is empty, or not.

the -s test option can do that for you.

See the man page for test.
# 5  
Old 02-17-2010
Using some posixsh:
Code:
# number of chars in file
size=$(cat file | wc -c)
while ((size>0))
do
      do_something
      # look again
      size=$(cat file | wc -c)
done

Is same as scottn has written, using test command: is the file empty
Code:
while [ -s file ]
do
      do_something
done

# 6  
Old 02-17-2010
Am assuming that initially this file is not blank but you want to do something within the loop which will make it blank at the last.

If my assumption is correct then below mentioned code can help you in this.

Code:
 
echo "Hi" > test_file
while [ -s test_file ]
do
echo "In while loop"
echo "Doing something - assuming that you will make test_file blank withing the loop itself"
echo "Creating test_file of 0 bytes so that this loop can be stopped."
rm test_file
touch test_file
done



---------- Post updated at 12:43 PM ---------- Previous update was at 12:34 PM ----------

Is this what you were looking for or you wanted something else?
# 7  
Old 02-17-2010
Or if you are waiting for a file to exist and contain some data:

Code:
while true
do
        if [ -s test.txt ]
        then
                echo "my instructions"
                break
        else
                # Wait a reasonable time before trying again
                sleep 5
        fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to understand special character for line reading in bash shell?

I am still learning shell scripting. Recently I see a function for read configuration. But some of special character make me confused. I checked online to find answer. It was not successful. I post the code here to consult with expert or guru to get better understanding on these special characters... (3 Replies)
Discussion started by: duke0001
3 Replies

2. UNIX for Dummies Questions & Answers

Trouble understand and using for loop

Good evening all I have what might be a simple problem to solve but I do not know how to solve it myself. I am writing a bash script and my code looks something like this: mp3=`ls | grep \.mp3` for f in $mp3 do echo $f done Basically what I want to do is look through the current... (4 Replies)
Discussion started by: mistsong1
4 Replies

3. Shell Programming and Scripting

Learning curve to understand bash iteration

Hi Folks, I know on one side there is script and on another side there is smart script. I am able to achieve what I got but thought there has to be a code which doesn't require multiple lines. So if you guys can help me out than it will be awesome. Also I wrote in cshell but if can get both the... (4 Replies)
Discussion started by: dixits
4 Replies

4. UNIX for Dummies Questions & Answers

Another Simple BASH command I don't understand. Help?

I have a text file called file1 which contains the text: "ls -l" When I enter this command: bash < file1 > file1 file1 gets erased. However if I enter this command: bash < file1 > newfile the output from "ls -l" is stored in newfile. My question is why doesn't file1's text ("ls -l") get... (3 Replies)
Discussion started by: phunkypants
3 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

I want to understand bash better

Hi everyone, I have some questions regarding bash and my initializer files: when I look at my bashrc file, I see lots of "export".. export MAGICK_HOME="$HOME/bin..etc".. what does export really mean? in my bashrc, I have a line: "umask 0002" ... what does that do? I have a bash_profile... (6 Replies)
Discussion started by: patrick99e99
6 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

Understand Bash scripting encryption

Dear all, I have been working with Linux for quite few years but never required to learn Bash and Linux really dep doan. I want to learn bash I bought books and Have been reading on the Internet but I cannot find my answers, maybe I am blind. For example: What does this means? # for i in... (4 Replies)
Discussion started by: renpippa
4 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

bash shell: 'exec', 'eval', 'source' - looking for help to understand

Hi, experts. Whould anybody clear explay me difference and usage of these 3 commands (particulary in bash) : exec eval source I've tryed to read the manual pages but did not get much. Also could not get something useful from Google search - just so much and so not exactly, that is... (3 Replies)
Discussion started by: alex_5161
3 Replies
Login or Register to Ask a Question