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
Related videos on Youtube
Author by
Vaios Argiropoulos
Updated on September 18, 2022Comments
-
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 almost 11 yearsPlease run it as
bash -vx ./script.sh
. Are the bash versions on the two platforms different? You may useif [ "$tempval" -gt $(($(date +%s) - 3600*24*30)) ]
instead. -
tripleee almost 11 yearsIs 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 almost 11 yearsOk 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 almost 11 years
-gt
only works with[
and[[
. -
Admin almost 11 yearsIf you have found a solution to your problem, post it as an answer to this question and accept it.
-
-
ott-- almost 11 yearsThese backticks are hard to read on the screen. You could consider using things like
$(expr $temptime - 2592000)
instead.