Reputation: 13
I am trying to write a script for the RStudio Package Manager to update automatically.
I have this so far
#!/bin/bash
LOGFILE=/var/log/rstudio-pm.update.log
echo "`date '+%D %H:%M:%S'`: Starting rstudio-pm update." >> $LOGFILE
echo "`date '+%D %H:%M:%S'`: Synchronizing with CRAN." >> $LOGFILE
sync_result=`/opt/rstudio-pm/bin/rspm sync`
echo "`date '+%D %H:%M:%S'`: $sync_result" >> $LOGFILE
echo "`date '+%D %H:%M:%S'`: Completed synchronization with CRAN." >> $LOGFILE
echo "`date '+%D %H:%M:%S'`: Starting dryrun update." >> $LOGFILE
dryrun_result=`sudo /opt/rstudio-pm/bin/rspm update --source=my-cran`
echo "`date '+%D %H:%M:%S'`: $dryrun_result" >> $LOGFILE
echo "`date '+%D %H:%M:%S'`: Completed dryrun update." >> $LOGFILE
dryrun_result_last_line=`echo $dryrun_result| tail -n 1`
echo "`date '+%D %H:%M:%S'`: BEGIN ECHO OF DRY RUN RESULT LAST LINE" >> $LOGFILE
echo "`date '+%D %H:%M:%S'`: $dryrun_result_last_line" >> $LOGFILE
echo "`date '+%D %H:%M:%S'`: END ECHO OF DRY RUN RESULT LAST LINE" >> $LOGFILE
if [[ $dryrun_result_last_line =~ "--snapshot=" ]]
then
echo "`date '+%D %H:%M:%S'`: Starting actual update." >> $LOGFILE
param=`echo $dryrun_result_last_line | sed 's/.*\(--snapshot=[0-9]\+\).*/\1/'`
echo "`date '+%D %H:%M:%S'`:PARAMETERS FOR UPDATE $param" >> $LOGFILE
cmd="/opt/rstudio-pm/bin/rspm update --source=my-cran $param --commit"
echo "`date '+%D %H:%M:%S'`: Running: $cmd" >> $LOGFILE
update_result=`$cmd`
echo "`date '+%D %H:%M:%S'`: $update_result" >> $LOGFILE
echo "`date '+%D %H:%M:%S'`: Completed actual update." >> $LOGFILE
else
echo "`date '+%D %H:%M:%S'`: All packages already up to date." >> $LOGFILE
fi
My command needs to look like this:
sudo /opt/rstudio-pm/bin/rspm update --source=my-cran --snapshot=2021-06-25 --commit
(the date in the --snapshot= flag is the variable i am ultimately needing to change. The date has to correspond with the most recent version of the CRAN
The Variable $dryrun_result_last_line
returns text that looks like
This action will add or archive the following packages: Name Version Path License Needs Compilation Dependency Action broom 0.7.8 MIT + file LICENSE no true add checkpoint 1.0.0 GPL-2 no false add colorspace 2.0-2 BSD_3_clause + file LICENSE yes false add curl 4.3.2 MIT + file LICENSE yes false add dplyr 1.0.7 MIT + file LICENSE yes false add filelock 1.0.2 MIT + file LICENSE yes true add gert 1.3.1 MIT + file LICENSE yes true add ggplot2 3.3.4 MIT + file LICENSE no false add ggsignif 0.6.2 GPL-3 | file LICENSE no false add glmnet 4.1-2 GPL-2 yes false add lme4 1.1-27.1 GPL (>= 2) yes false add lpSolve 5.6.15 LGPL-2 yes true add mime 0.11 GPL yes false add openxlsx 4.2.4 MIT + file LICENSE yes false add pkgcache 1.2.2 MIT + file LICENSE no true add pkgdepends 0.1.1 MIT + file LICENSE no true add plotly 4.9.4.1 MIT + file LICENSE no false add psych 2.1.6 GPL (>= 2) no false add rio 0.5.27 GPL-2 no false add rmarkdown 2.9 GPL-3 no false add scatterD3 1.0.0 GPL (>= 3) no false add testthat 3.0.3 MIT + file LICENSE yes true add xfun 0.24 MIT + file LICENSE yes false add broom 0.7.7 MIT + file LICENSE no archive checkpoint 0.4.10 GPL-2 no archive colorspace 2.0-1 BSD_3_clause + file LICENSE yes archive curl 4.3.1 MIT + file LICENSE yes archive dplyr 1.0.6 MIT + file LICENSE yes archive gert 1.3.0 MIT + file LICENSE yes archive ggplot2 3.3.3 MIT + file LICENSE no archive ggsignif 0.6.1 GPL-3 no archive glmnet 4.1-1 GPL-2 yes archive lme4 1.1-27 GPL (>= 2) yes archive mime 0.10 GPL yes archive openxlsx 4.2.3 MIT + file LICENSE yes archive plotly 4.9.4 MIT + file LICENSE no archive psych 2.1.3 GPL (>= 2) no archive rio 0.5.26 GPL-2 no archive rmarkdown 2.8 GPL-3 no archive scatterD3 0.9.2 GPL (>= 3) no archive testthat 3.0.2 MIT + file LICENSE yes archive xfun 0.23 MIT + file LICENSE yes archive To complete this operation, execute this command with these flags: '--snapshot=2021-06-25 --commit'.
I need to pull the 2021-06-25 out of the above
How can I achieve this. In my log file my $param
variable is only returning --snapshot=2021
How can I make it return the full date of 2021-06-25?
Thank You
Upvotes: 1
Views: 58
Reputation: 19395
How can I make it return the full date of 2021-06-25?
Short answer: To include the hyphen in the match, include it in the bracket expression, i. e. [0-9-]
.
If you're further interested: Your $dryrun_result_last_line
doesn't contain only the last line of $dryrun_result
because the echo $dryrun_result
loses the newline characters; to keep them, you'd have to quote:
dryrun_result_last_line=`echo "$dryrun_result" | tail -1`
If your shell supports <<<
redirection, you can also use:
dryrun_result_last_line=`tail -1 <<<$dryrun_result`
Or even
dryrun_result_last_line="${dryrun_result##*
}"
Upvotes: 0