I need to remove a line of a cron file but when I use this bash script code it removes everything from the cron file and doesn't overwrite it with the new result.
I choose which line to remove with a variable that is read as a number shown below:
read $lineToRemove
crontab -l | sed "${lineToRemove}d" | crontab -
From my understanding of this, i read the user input if they write the number 2 and press enter. Then the crontab file is listed in the first part of the pipe line. Then the sed
function is used to remove the line at the specified number in the variable and the result is written back to the cron file. Yet when i do this it removes everything from the cron file
read
's parameter should exclude $
, just like var=value
excludes $
. You're read
ing into a variable named $lineToRemove
, not lineToRemove
.
$ read x
hi
$ echo $x
hi
$ read $x
hi
$ echo $hi
hi
When I read $x
, the variable that stores the value is $x
, not x
, eg hi
. so $hi
subsequently refers to the thing read into $x
.