Thursday, June 18, 2009

Use of bash until loop - shell script


- I had to execute a script "" 10 times.
- basically outputs loglines that to be redirected to log file.
- Once that log file reaches 5MB, kill and again execute (continue this for 10 times)

The script:


sh >> $lfile &

sleep 2
until [ $(ls -l $lfile | awk '{print $5}') -gt 5000 ]
echo "Sleeping for next 5 seconds"
sleep 5

echo "Executed $i times"
kill $Pid

Important use:
- Bash until loop


Nathan said...

I like it!!
too bad im too busy these days to post some scripts here , will have some spare time in about few weeks...

Jadu Saikia said...

@Nathan, thanks. Your comments/scripts are always useful, thanks.

Mahesh Kharvi said...

i++ can also be used in place of i+1 in for loop.

Mahesh Kharvi said...

Just wonder whether you mean 5MB or 5KB, 5000 will be 5KB.

We can make use of seq command too ;)

for i in $(seq 10); do

Jadu Saikia said...

@Mahesh,thanks for the 5MB thing, I just put that, :-)

And for the seq, the problem with seq is that it has to pre evaluate seq and then it will start iterate. Anyway for smaller iterations, its fine and quite useful. Thanks.

© Jadu Saikia