skip to Main Content

Hello is this possible to create a file with these line of codes below and make it as an executable file on single line of code? Currently I’m doing manually. Your response is highly appreciated. Thank you

Manual Steps

-vi + content

-chmod +x filename

This is the file content:

#!/bin/bash

sudo apt-get update

sudo apt install curl -y

sudo apt install -y default-jdk

Screenshot:

CLI Image

Objective to write everything using one line of code

2

Answers


  1. You could generate a test.sh with bash script (we can call it 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
    

    When you execute bash download.sh, it will generate a test.sh automatically.

    Login or Signup to reply.
  2. If it is crucial that it be a one-liner and vi is not a requirement:

    echo -e '#!/bin/bashnsudo apt-get updatensudo apt install curl -ynsudo apt install -y default-jdk' > test.sh && chmod +x test.sh && ./test.sh

    If vi is a requirement you could do something like:

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

    in place of the echo, but this seems much less effective to me and I’m less familiar with using vi in this way.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search