0
declare -i fil="$1"
declare -t tid="$2"
notFinished=true
finnes=false

if [ -f $fil ];
then
finnes = true
fi

while $notFinished;
do

if [ -f $fil && ! $finnes ];          (14)
then
echo "Filen: $fil ble opprettet."
finished=true
fi

if [ ! -f $fil && $finnes ];         (20)
then
echo "Filen: $fil ble slettet."
finished=true
fi

sleep $tid
done

I'm trying to check if a file with the name $fil gets created, or deleted during the life of the script, only checking every $tid seconds. I also want to check if the file gets changed by comparing timestamps, but i'm not really sure how to do this.. Just want to mention that this is the first time trying to program in this language.

The only error i'm getting right now is:

  /home/user/bin/filkontroll.sh: line 14: [: missing `] '
  /home/user/bin/filkontroll.sh: line 20: [: missing `] '

@edit: fixed notFinished and some spacing

Patidati
  • 1,048
  • 2
  • 12
  • 19
  • 1
    Use spaces around every single `[` and `]` in your conditions. `if [$var -eq 2]` is wrong, it has to be `if [ $var -eq 2 ]` and so on. – fedorqui Sep 05 '14 at 09:48
  • @fedorqui ok fixed that, but got 2 new errors on line 14, 20 – Patidati Sep 05 '14 at 09:57
  • Please update your question with the current code, together with an indicator of where are lines 14 and 20 (I am not going to count them :D) – fedorqui Sep 05 '14 at 10:00
  • You are missing the `$` in the name of `finnes`. Also, use `&& ! $finnes`, not `!finnes`. – fedorqui Sep 05 '14 at 10:20
  • @fedorqui Done, and edited :) But still getting the same errors – Patidati Sep 05 '14 at 10:23
  • @fedorqui The script is running btw, but the 2 errors keep popping up every $tid seconds – Patidati Sep 05 '14 at 10:23
  • 2
    The current answers may already be solving your issue, but as an exercise, have a look at [inotifywait](http://man.cx/inotifywait) command... – anishsane Sep 05 '14 at 12:51
  • @anishsane Will check it out, just started learning bash, so anything helps :)! Thanks a lot :) Just wondering, is there any good method to check if the file has been edited? -N $fil doesnt work :/ – Patidati Sep 05 '14 at 13:06
  • ^^ When a file is written (will trigger `inotifywait` to write a line on `stdout`), take a backup. Next time when it gets written, `diff` against that backup, to see if the file was indeed edited, or just saved without any modification. If you want to save space, take `md5sum` instead of entire file backup (it may be slower). Frankly, I don't know if any better methods are available. – anishsane Sep 05 '14 at 13:10

2 Answers2

1

You can use something like this:

#!/bin/bash

declare -i fil="$1"
declare -t tid="$2"
notFinished=true
finnes=false

if [ -f "$fil" ]; then
   finnes=true
fi

while [ "$notFinished" = true ];
do
    if [ -f "$fil" ] && [ ! "$finnes" = true ]; then
       echo "Filen: $fil ble opprettet."
       finished=true
    fi

    if [ ! -f "$fil" ] && [ "$finnes" = true ]; then
       echo "Filen: $fil ble slettet."
       finished=true
    fi

    sleep $tid
done

Note you should read How to declare and use boolean variables in shell script? interesting question and answer (link to the answer I prefer), so that you can see the boolean checkings should be done like this (this applies to the if but also to the while):

if [ "$bool" = true ]; then

Also, note I quoted variables. It is a good practice that will avoid you getting crazy sometimes with strange behaviours when the variables are not set.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0
me@box:/tmp $ if [ a ] ; then echo foo; fi
foo
me@box:/tmp $ if [ b ] ; then echo foo; fi
foo
me@box:/tmp $ if [ a && b ] ; then echo foo; fi
bash: [: missing `]'
me@box:/tmp $ if [ a ] && [ b ] ; then echo foo; fi
foo
me@box:/tmp $ if [[ a && b ]] ; then echo foo; fi
foo
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243