There's couple problems with your script.
1. Unquoted $check variable
In this part,
} | crontab -u ubuntu -
echo $check will expand to
echo */$2 * * * * <other text here> , but bash
* will perform filename expansion to the contents of the current working directory of the script on each of the glob star symbols. Thus, effectively, your
crontab receives completely incorrect final string.
2. Potential null variable
$check variable set if and only if
$b is greater than 1. Well, what happens when
$b is 1 ? your variable won't be set.
if [ $b -gt 1 ];
check="*/$a * * * * /usr/bin/php /var/www/html/requests/check.php > /dev/null"
That also means when you
echo the variable, your
crontab command will receive a null string. Thus, you might want to implement another if statement there to check whether
$check variable is null, like
if [ "x$check" != "x" ];
echo "$check" | crontab -u ubuntu -
And of course, please please please, for the love of shell scripting , quote your string variables.
As requested in the comments, multiple entries could be output to crontab in a similar fashion:
$ check1="*/2 * * * * bash -c 'wall <<< \"check1\"'"
$ check2="*/2 * * * * bash -c 'wall <<< \"check1\"'"
$ printf "%s\n" "$check1" "$check2" | crontab -u $USER -
The idea here is that
printf keeps processing arguments until all are exhausted so it's basically same as a for loop. Alternatively, one could append each desired crontab string into array, and iterate using for loop via that same array.