【问题标题】:Linux write basic commands and make it executable using single line of codeLinux 编写基本命令并使用单行代码使其可执行
【发布时间】:2022-10-04 11:46:29
【问题描述】:

您好,是否可以使用下面的这些代码行创建一个文件并将其作为单行代码的可执行文件?目前我正在手动操作。非常感谢您的回复。谢谢

手动步骤

-vi + 内容

-chmod +x 文件名

这是文件内容:

#!/bin/bash

sudo apt-get 更新

sudo apt install curl -y

sudo apt install -y default-jdk

截屏:

CLI Image

目标是使用一行代码编写所有内容

【问题讨论】:

  • 有点不清楚你想要什么。您不能将所有内容放在一个脚本中吗?

标签: linux bash shell ubuntu scripting


【解决方案1】:

如果它是一个单行代码并且 vi 不是必需的:

echo -e '#!/bin/bash sudo apt-get update sudo apt install curl -y sudo apt install -y default-jdk' > test.sh && chmod +x test.sh && ./test.sh

如果 vi 是一项要求,您可以执行以下操作:

vim file.txt "+i#!/bin/bash" "+osudo apt-get update" "+o..." and so on

代替回声,但这对我来说似乎不太有效,而且我对以这种方式使用 vi 不太熟悉。

【讨论】:

    【解决方案2】:

    您可以使用 bash 脚本生成test.sh(我们可以称之为download.sh):

    # 1. write script content to test.sh
    cat <<EOT >> test.sh
    #!/bin/bash
    
    sudo apt-get update
    
    sudo apt install curl -y
    
    sudo apt install -y default-jdk
    EOT
    
    # 2. make it executable
    chmod +x test.sh
    

    当你执行bash download.sh时,它会自动生成一个test.sh。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 2017-07-30
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多