Home > OS >  Linux write basic commands and make it executable using single line of code
Linux write basic commands and make it executable using single line of code

Time:10-04

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

CodePudding user response:

If it is crucial that it be a one-liner and vi is not a requirement:

echo -e '#!/bin/bash\nsudo apt-get update\nsudo apt install curl -y\nsudo 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.

CodePudding user response:

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.

  • Related