简体   繁体   English

如何使用 sed 替换 shell 脚本中的 cron?

[英]How to replace a cron in a shell script using sed?

I need to replace a cron entry in a file using sed or awk.我需要使用 sed 或 awk 替换文件中的 cron 条目。

tried this : didnt work试过这个:没用

sed -i 's/0 0 * * 0/0 1 * * 1/g' script.sh

script.sh脚本.sh

#!/bin/bash

mkdir -p .github/workflows

cd .github/workflows
touch semgrep.yml

cat << EOF > semgrep.yml
name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 0 * * 0'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

Kindly help me with the same .请帮助我。

Using mikefarah/yq to edit the file in place ( -i ):使用mikefarah/yq就地编辑文件 ( -i ):

yq -i '.on.schedule[].cron = "0 1 * * 1"' semgrep.yml

would turn a semgrep.yml containing会变成一个semgrep.yml包含

name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 0 * * 0'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

into one containing成一个包含

name: Semgrep
on:
  pull_request: {}
  push:
    branches:
      - master
      - main
    paths:
      - .github/workflows/semgrep.yml
  schedule:
    - cron: '0 1 * * 1'
jobs:
  semgrep:
    name: Static Analysis Scan
    runs-on: ubuntu-18-04

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM