gpt4 book ai didi

How to execute shell scripting in md file(如何在md文件中执行shell脚本)

转载 作者:bug小助手 更新时间:2023-10-25 22:31:03 41 4
gpt4 key购买 nike



We have a MarkDown file where we are storing versions of multiple components.
Below is the sample .md file

我们有一个标记文件,其中存储了多个组件的版本。下面是示例.md文件


component1=1.2
components2=2.3
component3=`cat file1 | grep 'App_Version' | grep -P '(?<==).*' && rm -rf file1`

Here the component3 version is dynamic, so we are executing the command to get the version.

这里的Component3版本是动态的,所以我们执行该命令来获取版本。


Need help on accomplishing this in correct way.

需要帮助才能以正确的方式实现这一点。


更多回答
优秀答案推荐

Markdown is not a scripting language, so you probably need one form or another of preprocessing. Example with GNU m4 (but any preprocessor with similar capabilities would do the job):

Markdown不是一种脚本语言,因此您可能需要某种形式的预处理。GNU M4的示例(但任何具有类似功能的预处理器都可以完成这项工作):


$ cat sample.m4
m4_changequote(`"""', `"""')m4_dnl
component1=1.2
components2=2.3
component3=m4_esyscmd("""grep -Po '(?<=App_Version=).*' file1 && rm -f file1""")m4_dnl
component4=foo

$ cat file1
App_Version=4.0.2

$ m4 -P sample.m4 > sample.md

$ cat sample.md
component1=1.2
components2=2.3
component3=4.0.2
component4=foo

$ ls file1
ls: cannot access 'file1': No such file or directory

Explanations:

解释:



  • The -P option of m4 modifies all builtin macro names so they all start with the m4_ prefix. It is not absolutely needed but it makes the source code easier to read.

    M4的-P选项修改所有内置宏名称,以便它们都以M4_前缀开头。它并不是绝对需要的,但它使源代码更易于阅读。



  • The sample.m4 file is your source file, the one you edit. The:

    Sample.m4文件是您编辑的源文件。该项目:


    m4 -P sample.m4 > sample.md

    m4 -P sample.m4 > sample.md


    command preprocesses the source file to produce the markdown file.

    命令对源文件进行预处理以生成标记文件。



  • The m4_changequote macro at the beginning of sample.m4 changes the quotes that m4 uses for text strings. Use any left and right quotes you want (""" in our example) as long as it is not used in your markdown text.

    sample.m4开头的m4_changequote宏更改m4用于文本字符串的引号。使用任何你想要的左引号和右引号(在我们的例子中是”),只要它不在你的markdown文本中使用。



  • m4_dnl is the macro that suppresses the rest of the line, including the newline character.

    M4_dnl是取消该行其余部分的宏,包括换行符。



  • m4_esyscmd("""cmd""") substitutes the output of the cmd shell script.

    M4_esyscmd(“cmd”)替换cmd外壳脚本的输出。




Note: I assumed that you wanted grep -Po '(?<=App_Version=).*' file1 instead of cat file1 | grep 'App_Version' | grep -P '(?<==).*' which looks like several anti-patterns at once.

注意:我假设您需要grep-Po‘(?<=App_Version=).*’file1而不是cat file1|grep‘App_Version’|grep-P‘(?<==).*’,这看起来像是同时有几个反模式。



if your code is inside code block, try to parse out the code block and then execute it as is.
this is a bash function to do so

如果您的代码在代码块内,请尝试分析出代码块,然后按原样执行它。这是一个用于执行此操作的bash函数


function mbe() {
if [ -f "$1" ]; then
cat $1 | # print the file
sed -n '/```bash/,/```/p' | # get the bash code blocks
sed 's/```bash//g' | # remove the ```bash
sed 's/```//g' | # remove the trailing ```
sed '/^$/d' | # remove empty lines
/usr/bin/env sh ; # execute the command
else
echo "${1} is not valid" ;
fi
}

source: https://gist.github.com/gmolveau/67d10dfaea1fdd729865b2f8d46f7488

消息来源:https://gist.github.com/gmolveau/67d10dfaea1fdd729865b2f8d46f7488


更多回答

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com