0

here is the script I wrote but it seems it has problem with while ! while suppose to compare the content of K with SDATE and while they are not equal go to the loop !

for d in \
 $(sed -nre 's/.*\[(..)\/(...)\/(....):(..:..:..) .*/\1 \2 \3 \4/p' thttpd.log | date +%s -f-);
do echo $d >s1; done

time=$(expr 60 \* 60 \* 24 \* 5)
EDATE=`tail -1 s1`
SDATE=$[$EDATE - $time]
time=$(expr 60 \* 60 \* 24 \* 5)
EDATE=`tail -1 s1`
SDATE=$[$EDATE - $time]
k=`tail -1 s1`
echo $k
echo $SDATE
while [$k -ne $SDATE](k and SDATE contain numbers)
 do
k=`tail -1 s1`
sed '1d' < s1 > tempfile
mv s1 s1.old
mv tempfile s1
echo $K| awk '{print strftime("%d/%m/%Y:%T",$1)}'|tee -a ass

done

3 Answers 3

1

The problem is that you do not have spaces around [ or ]. Which causes BASH to parse the line incorrectly.

With the following line, BASH will attempt to run the program [$K, probably not what you are intending. while [$k -ne $SDATE]

What you need to have is the following:
while [ $k -ne $SDATE ]

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

while [[ $k != $SDATE ]]

6 Comments

still has problem ! k is 1041454521 SDATE is 1041022521 and the error ./10: line 14: [[1041454521: command not found
One bracket would be sufficient. The important thing is the space after [ and before ]. [ is another way to write "test", like: while test "$k" != "$DATE" ; do ...
Hmm. I was always taught to use two brackets, because [[ is guaranteed to be a builtin, whereas [ might be an external program (and thus slower). And there were some other subtleties that I forgot.
still has problem ! it's gonna kill me till run properly ! :((( 31/12/1969:16:00:00 ./10: line 14: [: -ne: unary operator expected it gives me the wrong date ! it's suppose to be 2003 not 1969
I didn't write -ne anywhere.
|
0

Ah, shell programming is so touchy...

k=0
while [ $k != 4 ]; do echo $k; k=`expr $k + 1`; done

Works and prints 0, 1, 2, 3 on separate lines as expected and this essentially looks like what you have except for spaces, newlines, and sources of values. Maybe it is a string versus value problem but I did not think shell variables had types.

I would try stripping what you have back until it works then add back what you want it to do.

1 Comment

p.s. I also noticed on your third from last line (with the awk) it is $K instead of $k. Shell variables are case sensitive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.