Expression error in bash script

6,321

I've actually made the changes suggested by the 3 guys at the comments and all of them were necessary for the script to work.

Final code below:

temptime=$(date +%s)
temptime=`expr $temptime - 2592000`

for DIR in $DIRS
do
tempdir=$DIR/
echo $tempdir
tempval=$(stat -c %Y $tempdir)
echo $tempval
if [[ $tempval -gt $temptime ]]; then
    echo "Exiting gracefully!!!"
    exit
else
    continue
fi
done
Share:
6,321

Related videos on Youtube

Vaios Argiropoulos
Author by

Vaios Argiropoulos

Updated on September 18, 2022

Comments

  • Vaios Argiropoulos
    Vaios Argiropoulos almost 2 years

    I am getting this really weird error despite of the fact that the same script runs fine on one platform (Arch linux) but not on my other mobile platform (Maemo Linux). I'll pass here the relevant part of the code with the line numbering:

    41 for DIR in $DIRS
    42 do
    43 tempdir=$DIR/
    44 tempval=$(stat -c %Y $tempdir)
    45 echo $tempval
    46 if (( $tempval > $(date +%s) - 3600*24*30 )); then
    47     echo "I am done basically. Exiting..."
    48     exit
    49 else
    50     continue
    51 fi
    52 done
    

    In the code above DIRS is a list which contains names of directories. In this loop i am trying to find one directory of the list which is newer than 30 days old and if i find one i exit the script.

    Line 45 is put there for debugging purposes basically.

    I am getting the error below:

    ./script.sh : line 52 : 1372757584 : not found
    

    After some changes suggested from the comments:

    Ok the error now is below:

    scone.sh: line 46: ((: 1372768246 -gt 1372770593 - 3600*24*30 : syntax error
    in expression (error token is "1372770593 - 3600*24*30 ")
    
    • Hauke Laging
      Hauke Laging almost 11 years
      Please run it as bash -vx ./script.sh. Are the bash versions on the two platforms different? You may use if [ "$tempval" -gt $(($(date +%s) - 3600*24*30)) ] instead.
    • tripleee
      tripleee almost 11 years
      Is it being run in bash in both instances? And/or could there be a runaway construct which starts somewhere in lines 1-40 which ends at line 52 and expands to the number you see?
    • Vaios Argiropoulos
      Vaios Argiropoulos almost 11 years
      Ok i've actually done both what you suggested. I run on bash and replaced > sign with -gt expression,and now i am getting the error above.See first post for the new error.
    • Admin
      Admin almost 11 years
      -gt only works with [ and [[.
    • Admin
      Admin almost 11 years
      If you have found a solution to your problem, post it as an answer to this question and accept it.
  • ott--
    ott-- almost 11 years
    These backticks are hard to read on the screen. You could consider using things like $(expr $temptime - 2592000) instead.