首页 > 解决方案 > 需要 CentOS 脚本中的 SED 命令帮助以在 RStudio 包管理器中自动更新策划的 CRAN

问题描述

我正在尝试为 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]\+\).*/\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

我的命令需要如下所示:

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

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

变量$dryrun_result_last_line返回看起来像的文本

此操作将添加或归档以下包: 名称 版本 路径 许可证需要编译依赖 操作 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 是 false 添加 dplyr 1.0.7 MIT + file LICENSE 是 false 添加 filelock 1.0.2 MIT + file LICENSE 是 true 添加 gert 1.3.1 MIT + file LICENSE 是 true 添加 ggplot2 3.3.4 MIT +文件 LICENSE no false 添加 ggsignif 0.6.2 GPL-3 | file LICENSE 否 false 添加 glmnet 4.1-2 GPL-2 是 false 添加 lme4 1.1-27.1 GPL (>= 2) 是 false 添加 lpSolve 5.6.15 LGPL-2 是 true 添加 mime 0.11 GPL 是 false 添加 openxlsx 4.2.4 MIT + file LICENSE 是 false 添加 pkgcache 1.2.2 MIT + file LICENSE no true 添加 pkgdepends 0.1.1 MIT + file LICENSE no true 添加 plotly 4.9.4。2021-06-25——提交”。

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

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

我怎样才能让它返回 2021-06-25 的完整日期?

谢谢你

标签: rlinuxsedcentos

解决方案


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##*
}"

推荐阅读