While loop depends on variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop depends on variable
# 1  
Old 04-18-2012
While loop depends on variable

The variable N is set upon start of script so while loop runs and keeps running because N is not being updated when loop is running:

N becomes less than 9 but it still runs (should be "done")....

Any ideas? Thanks!

Code:
N=`wc -l /home/count.txt | awk '{print $1}'`

while test "$N" -gt "10"
do
sed -i "1,1 d" /home/count.txt
done

# 2  
Old 04-18-2012
You need to decrement the variable. Assuming ksh or bash and using your example:

Code:
N=`wc -l /home/count.txt | awk '{print $1}'`

while test "$N" -gt "10"
do
    echo "something"
    ((  n-- ))
done

If you are using a shell that doesn't support the (( expression )) syntax you'll need to replace that with the proper statement that decrements N.

Last edited by methyl; 04-18-2012 at 08:17 PM.. Reason: change font so I can read it!
# 3  
Old 04-18-2012
You could also do:

Code:
N=`wc -l /home/count.txt | awk '{print $1}'`
[ $N -gt 10 ] && sed "1,$((N-10))d" /home/count.txt

or
Code:
tail -10 /home/count.txt > /tmp/trim.$$
mv /tmp/trim.$$ /home/count.txt

# 4  
Old 04-18-2012
Do this to save a process (awk) by not having wc print the filename in the first place, assuming your shell supports typeset:

Code:
typeset -i N  ## define N as an integer (strips out the leading whitespace from wc)
N=`wc -l < /home/count.txt`



# 5  
Old 04-18-2012
Combining some of the ideas above we can condense the script to:
Code:
while test $(wc -l < /home/count.txt) -gt 10
do
        sed -i "1,1 d" /home/count.txt
done

Which most would write as:
Code:
while [ $(wc -l < /home/count.txt) -gt 10 ]
do
        sed -i "1,1 d" /home/count.txt
done

Note the numeric compare -gt (no quotes).
This User Gave Thanks to methyl For This Post:
# 6  
Old 04-18-2012
I'd probably do it without the while:

Code:
sed -i "1,$(( $(wc -l <file) - 10 ))d" file

# 7  
Old 04-18-2012
@agama
I can't try your code because I don't have sed -i but the code looks good and efficient.

Reading between the lines the O/P posted code which should remove a line from a file one-line-at-a-time until it reached the required number of lines. Obviously an academic (or homework?) problem because there are much easier ways of achieving the final result, but agama's solution is really good.

It's been fun.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Result of nmap depends of mac address

Hi guys, i am having some mine bash script which print only ip addresses echo "Interfaces IP addresses:" ip -o addr | awk '!/^*: ?lo|link\/ether/ {print $2" "$4}' echo -n "enter range of IPs(192.168.0.1-254): " read range nmap -oG - $range >/home/test/Desktop/SCAN cat... (0 Replies)
Discussion started by: tomislav91
0 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. UNIX for Dummies Questions & Answers

valgrind Conditional jump or move depends on uninitialised value(s)

Hi, I know similar questions appeared here already, still i didnt find answer to my problem yet. Im getting the following message from valgrind (version 2): ==15414== 1 errors in context 1 of 6: ==15414== Conditional jump or move depends on uninitialised value(s) ==15414== at... (2 Replies)
Discussion started by: evasz
2 Replies

7. Programming

valgrind (Conditional jump or move depends on uninitialised value(s))

Hi everybody, I wrote a small subroutine 'GetStringDelim()' to return a substring from a string enclosed by a string 'Limit1' and a char 'Limit2'. It works perfectly and I don't get an error message together with valgrind, if I set the 3rd parameter in GetStringDelim() to NULL (see below). ... (3 Replies)
Discussion started by: MIB_Maik
3 Replies

8. UNIX for Dummies Questions & Answers

Please Help:Need to Split the file into mutliple files depends on the KEY field value

Hi Gurus, I am new to UNIX(HP). my requirmnet is File needs to needs to be split into multiple files dependa on one key value. example 1 abc 333 us 2 bbc 444 ch 5 nnn 333 kk 7 hhh 555 ll 3 hhh 333 hh now the requirment is line with 333 should be copied into test1.txt and... (14 Replies)
Discussion started by: arund_01
14 Replies

9. Shell Programming and Scripting

Depends on Machine?

I am trying to learn PERL programming. I copied this example from a book just to try it on my machine. It worked out fine but when I tried it on another machine, an error appears: syntax error near unexpected token ';'. I tried it on a third machine but a new error appears: command not found. ... (4 Replies)
Discussion started by: Slick
4 Replies

10. Shell Programming and Scripting

Please my job depends on this ....

I need to write a script that will move files from a direcory to another directory, but only the files that are owned by a specific owner. e.g. if seven files in a dir are owner by a user called 'budd' then move to a direcory called \budd. If not then leave alone. Many Thanks (2 Replies)
Discussion started by: Budd
2 Replies
Login or Register to Ask a Question