0

Current script:

#!/bin/sh

declare -i r_code=0                     # initialize the script's return code

cd /opt/software/dbascripts
# the dbbkp.auth file has the name of the db user and db password
source ../dbaauthfiles/dbbkp.auth

NEW=`date +"%Y%m" --date="next month"`
OLD=`date +"%Y%m" --date="last month"`
PARTITION=`date --date="+2 month -$(($(date +"%d")-1)) days 00:00:00" +"%s"`

echo "$PARTITION"
echo "Creating p$NEW and deleting p$OLD"

mysql --batch -u$DB_USER -p$DB_PASSWORD db -f << eof
ALTER TABLE table1 ADD PARTITION ( PARTITION p$NEW VALUES LESS THAN ($PARTITION));
ALTER TABLE table1 DROP PARTITION p$OLD ;
eof
r_code+=$?

echo "### Completed ${0##*/}, exiting with return code $r_code"
exit $r_code

This fails to work on certain days of the month, but I need it to be able to run no matter what day of the month it runs.

Error when I ran this on the 8th of the month:

line 11: 08: value too great for base (error token is "08")
Warning: Using a password on the command line interfac

I know this has something to do with how octal numbers are looked at from bash/Linux perspective, but I'm not sure how to correct this.

e can be insecure.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
user3299633
  • 2,971
  • 3
  • 24
  • 38
  • 1
    Please paste your script at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus Jun 13 '23 at 19:37
  • Why should it not be tagged with bash? It's a shell script within the bash shell. – user3299633 Jun 13 '23 at 19:38
  • 1
    how are you calling this script? you've tagged this question as `bash` but the shebang (`#!/bin/sh`) isn't the standard shebang for `bash`; `/bin/sh` implies `bourne` shell though some distros have `/bin/sh` pointing at `bash` or `dash`; it is possible to override the shebang but depends on how you're calling the script, otherwise use an appropriate shebang (eg, `/bin/bash` - or whatever the path is for `bash` on your system) – markp-fuso Jun 13 '23 at 19:39
  • Btw.: I suggest to remove `+` in `r_code+=$?` to assign `$?` to `r_code`. ` `r_code+=$?` increments the content of `r_code`. Since the content of `r_code` is 0, it makes no difference exactly in this case. – Cyrus Jun 13 '23 at 21:52

1 Answers1

1

Found I needed to modify line to the following for this to work:

$((10#$(date +"%d")-1))
user3299633
  • 2,971
  • 3
  • 24
  • 38