需要 CentOS 脚本中的 SED 命令帮助,以自动更新 RStudio 包管理器中的精选 CRAN

Need assistance with SED command in CentOS script to auto update a curated CRAN in RStudio Package Manager

我正在尝试为 RStudio 包管理器编写脚本以自动更新。

到目前为止我有这个

    #!/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]\+\).*//'`


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

我的命令需要如下所示:

sudo /opt/rstudio-pm/bin/rspm update  --source=my-cran --snapshot=2021-06-25 --commit

(--snapshot= 标志中的日期是我最终需要更改的变量。日期必须与 CRAN 的最新版本相对应

变量$dryrun_result_last_line returns 看起来像

的文本

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'.

我需要把上面的2021-06-25拉出来

我怎样才能做到这一点。在我的日志文件中,我的 $param 变量只是 returning --snapshot=2021

如何使 return 成为 2021-06-25 的完整日期?

谢谢

How can I make it return the full date of 2021-06-25?

简答:要在匹配中包含连字符,请将其包含在方括号表达式 i. e. [0-9-].

如果您更感兴趣:您的 $dryrun_result_last_line 不只包含 $dryrun_result 的最后一行,因为 echo $dryrun_result 丢失了换行符;要保留它们,您必须引用:

dryrun_result_last_line=`echo "$dryrun_result" | tail -1`

如果您的 shell 支持 <<< 重定向,您还可以使用:

dryrun_result_last_line=`tail -1 <<<$dryrun_result`

甚至

dryrun_result_last_line="${dryrun_result##*
}"