Reputation: 1088
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
Upvotes: 0
Views: 572
Reputation: 289725
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.
Upvotes: 1
Reputation: 119877
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
Upvotes: 0