Bash:Scriptception,在脚本中创建脚本

标签 bash

我将如何创建一个脚本来创建两个单独的脚本,其中包含一些内容?

我试过了,但是失败了

#!/bin/bash
cat > scriptMaster.sh << EOF

   cat > script1.sh << EOF
   #code
   EOF

   cat > script2.sh << EOF
   #code
   EOF

EOF

最佳答案

您必须将 EOF 标记放在左边距,除非您在它前面加上 - 前缀,这允许它以 TAB 缩进字符,但不是 空格。您需要为外部和内部 here-docs 使用不同的 EOF 标记;否则,标记将在看到外部标记时立即终止(因为内部 here-docs 在您运行 scriptMaster.sh 脚本之前不会实际执行)。

#!/bin/bash
cat >scriptMaster.sh <<EOF
#!/bin/bash
cat > script1.sh <<EOF1
#!/bin/bash
#code
EOF1
chmod +x script1.sh # Make the script executable

cat >script2.sh <<EOF2
#!/bin/bash
#code
EOF2
chmod +x script2.sh

EOF
chmod +x scriptMaster.sh

#Now run the scripts:
./scriptMaster.sh
./script1.sh
./script2.sh

关于Bash:Scriptception,在脚本中创建脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22442880/

相关文章:

linux - 我的脚本无法解压缩文件

macos - Bash,批量转换IP地址

bash - yq - 添加多行字符串

c - VT100 数字键盘使用修饰符(ctrl/shift/alt)时的转义码

linux - 检查通过 SSH 命令输出的零行

bash - shell in()是另一个进程?

linux - Bash -- 查找超过 3 行的文件列表

linux - bash脚本通过循环同时Ping 1000+ ip

linux - 将数字添加到 bash 中某些行的开头

bash - 如何在命令中使用文件并将输出重定向到同一文件而不截断它?