0

I trying to write a simple script that determines if a new file exist in a folder. It will eventually along side another script that will create a bunch files and then remove some that don't meet some criteria and tell you if it has any new files that met the criteria later.. anyway..

For testing purposed I created a folder called booty and put 1 file in it.

then I wrote this bash script:

#! /bin/bash
declare -i prev=$(ls booty/ | wc -l)
echo 'we have '$prev' files'
echo '--------------------------------'
echo 'testing a 1 > 1'
if [ $now > 1 ]; then
        echo 'we have a new file: WRONG'
else
        echo ' no new files: GREAT!'
fi

echo '--------------------------------'
echo 'lets ad a file'
touch booty/new.txt
declare -i now=$(ls booty/ | wc -l)
echo 'now we have '$now' files'

echo 'was ' $prev ' and now ' $now 'files'

if [ $now > $prev ]; then
        echo 'we have a new file: GREAT!'
else
        echo ' No new files WRONG'
fi

echo '--------------------------------'

echo 'setting a static number for compare'
declare -i static=1
echo 'static set to ' $static
echo 'comparing static:'$static ' to prev:'$prev
if [ $static > $prev ]; then
        echo 'static is greater than prev WRONG'
else
        echo ' static NOT greater than prev GREAT!'
fi

echo '---------------------------------'
echo 'lets try an incriment of static'
static=$(expr $static + 1)
echo 'now static now set to ' $static

if [ $static > $prev ]; then
        echo 'static is greater than prev GREAT!'
else
        echo ' NOT greater prev WRONG'
fi

rm booty/new.txt

I am expecting all "GREATS!" to be printed out, but when I get the static number I am getting the wrong results.. it echo's the right values but when it compares 1 > 1 it comes back as true? see output below:

erilidde$ sh test.sh 
we have 1 files
--------------------------------
testing a 1 > 1
 no new files: GREAT!
--------------------------------
lets ad a file
now we have 2 files
was  1  and now  2 files
we have a new file: GREAT!
--------------------------------
setting a static number for compare
static set to  1
comparing static:1  to prev:1
static is greater than prev WRONG
---------------------------------
lets try an incriment of static
now static now set to  2
static is greater than prev GREAT!

I am new to bash scripting and assume this has something to do with data type, but I'm lost. please tell me what stupid mistake I'm making.

erik
  • 4,946
  • 13
  • 70
  • 120
  • 1
    You could paste your script to [shellcheck.net](https://www.shellcheck.net/) to see basic shell mistakes... – oliv Oct 25 '18 at 13:39

1 Answers1

2

You don't use correctly >,<,<=,>= operators, note that in bash only the (( ... )) construct permits arithmetic expansion and evaluation (Double-Parentheses Construct). For square brackets you need to use -gt, -eq, -lt.. conditions (bash Comparison Operators)

And at top of script i suggest you to declare $now as an empty variable before use it in if statement

Madriot
  • 76
  • 5