0

I have a shell script with the following lines (the full script at the end of post):

CRON_BCK_CMD=/home/user/bck.sh
CRON_CONF=" */30 * *   *   *     $CRON_BCK_CMD"

but when I simply run these in shell and then echo $CRON_CONF I get

*/30 ... /home/user/bck.sh

where ... stand for all files listed from /home/user, e.g. */30 bck.sh bck-ssh.sh Documents davmail.log.../home/user/bck.sh

The script used to work on my other machine I think. How do I do what I obviously want to do?


# SETUP
# CRON_BCK_CMD sets the full path to the backup script
CRON_BCK_CMD=/home/user/bck.sh
# CRON_CONF sets the configuration line for cron, the time is set as:
# minute hour day_of_month month day_of_week
# * means any value, eg. 15 * * * * would mean backup at any hour, any day, when minute on the clock equals 15
# repetition can be managet by /, eg. */15 * * * * means backup every 15 minutes
CRON_CONF=" */30 * *   *   *     $CRON_BCK_CMD"
# SETUP END
CRON_IS_BCK_CMD=$(crontab -l 2>/dev/null | grep $CRON_BCK_CMD) || true
if [[ ! $CRON_IS_BCK_CMD ]]
then
    echo "No entry for backup found in crontab, do you want to schedule?"
    read -p "y/n (y) " REPLY
    if [[ $REPLY =~ ^[n|N]$ ]]
    then
        echo "Nothing to do, exitting..."
        exit 0
    fi
    (crontab -l ; echo "$CRON_CONF") | crontab -
else
    echo "Found crontab entry, do you want to stop schedule?"
    read -p "y/n (y) " REPLY
    if [[ $REPLY =~ ^[n|N]$ ]]
    then
        echo "Nothing to do, exitting..."
        exit 0
    fi
    crontab -l | grep -v $CRON_BCK_CMD | crontab -
fi
Paulo Tomé
  • 3,754
  • 6
  • 26
  • 38
atapaka
  • 491
  • 6
  • 19
  • 3
    Double quotes? `echo "$CRON_CONF"` – Quasímodo Feb 13 '20 at 23:09
  • 1
    Quote your variables when you use them. – roaima Feb 13 '20 at 23:20
  • Does this answer your question? [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters) – roaima Feb 13 '20 at 23:22
  • 1
    Also, on lines 14 and 23 (`read -p "y/n (y) " REPLY`), read without -r will mangle backslashes. – Paulo Tomé Feb 13 '20 at 23:31

1 Answers1

0

Use quotation marks to represent a variable as a string and not as the original source of the variable. Because otherwise an asterisk, if it follows a slash, will be treated as a "star convention" in the directory path specification - so that asterisk will represent all the files in the specified directory.

If you want to run a shell script via CRON, it's also recommended, for problems under certain circumstances, to run the script through a shell interpreter (/bin/bash -c /home/script.sh &) and also run it as the task in background (& at the end).

#!/bin/bash

# SETUP
# CRON_BCK_CMD sets the full path to the backup script
CRON_BCK_CMD="/home/user/bck.sh"
# CRON_CONF sets the configuration line for cron, the time is set as:
# minute hour day_of_month month day_of_week
# * means any value, eg. 15 * * * * would mean backup at any hour, any day, when minute on the clock equals 15
# repetition can be managet by /, eg. */15 * * * * means backup every 15 minutes
CRON_CONF="*/30 * * * *      /bin/bash -c $CRON_BCK_CMD &"     # change the Shell to the one used in your Linux (dash, sh, ash, ...)
# SETUP END

if crontab -l | grep "$CRON_BCK_CMD" > /dev/null; then
    echo "Found crontab entry, do you want to stop schedule?"
    read -p "y/n (y): " REPLY
    if [ "$REPLY" != "n" ]; then            # if the "ENTER" key was pressed or the "y" character was typed...
        crontab -l | grep -v "$CRON_BCK_CMD" | crontab -
    fi
else
    echo "No entry for backup found in crontab, do you want to schedule?"
    read -p "y/n (y): " REPLY
    if [ "$REPLY" != "n" ]; then            # if the "ENTER" key was pressed or the "y" character was typed...
        (crontab -l; echo "$CRON_CONF") | crontab -
    fi
fi

exit 0
s3n0
  • 170
  • 1
  • 8