我有一个模板文件(template.txt):
Hello, $x
Hello, $y
我有一个脚本文件(script.sh):
#!/bin/bash
$x=linux
$y=unix
# I want to include template.txt as a here-doc
如何将template.txt包含为here-doc of script.sh。
因此,当我运行script.sh时,输出应该是:
Hello, linux
Hello, unix
编辑:
我认为replace对我的工作来说是个很好的命令:
$ cat template.txt | replace '$x&
名为animals的原始文件如下所示:
dog
cat
dolphin
cat
这个命令通过将sed '/dog/i\ Linux Scripting' animals放在Linux Scripting之前来工作,我得到了以下内容:
Linux Scripting
dog
cat
dolphin
cat
然而,如果我做了动物头之后,文件是不变的。
$ head animals
dog
cat
dolphin
cat
如何以覆盖现有文件的方式执行此操作?
注意:用于多个文件,我希望animals与*.txt保持一致
如果我将cat命令保存到字符串中,然后执行它,那么我将得到一个错误。
linux# cmd="cat /data/test/test.tx* | grep toto"
linux# eval '$cmd'
cat: |: No such file or directory
cat: grep: No such file or directory
cat: toto: No such file or directory
即使是
linux# $cmd
cat: |: No such file or directory
cat: grep: No such file
代码:
# cat mylinux.py
# This program is to interact with Linux
import os
v = os.system("cat /etc/redhat-release")
输出:
# python mylinux.py
Red Hat Enterprise Linux Server release 7.6 (Maipo)
在上面的输出中,无论我为存储输出而定义的变量如何,都会显示命令输出。
如何仅使用os.system方法将shell命令输出存储到变量?
也许我的搜索是错的,但我找不到任何解决方案... 我想使用shell脚本将以下内容附加到我的bashrc文件中(以简化Windows Subsystem for Linux的安装): # Load X display (run 'XLaunch' on Windows with 'Disable access control' checked and 'Native opengl' unchecked!)
# https://sourceforge.net/projects/vcxsrv/
export DISPLAY=$(grep -m 1