How to Run Shell Files in Debian


Shell script is basically a file that contain lots of different commands that a user wants to execute. Shell scripts save time over running each command individually because they can be executed several times.

This article describes how to create and execute a shell script on a Debian system. The bash shell will be used, which is the default. To creating and executing shell scripts, the following steps will be taken:

Step 1: Write Shell Script

You only need a plain text file and a text editor like Nano, or anything like to create a shell script. After that, you must add the commands to the text file in the order that you want them to be executed.

The script “myscript” will be written using the Nano editor, but you are free to use any editor of your own. Before you begin creating the script, make sure the name of the file does not conflict with the commands. Entering “which” after the script’s name will reveal the answer.

$ which myscript

The result shown below shows that there is no command or program with the name “myscript” that is pre-installed or currently in use. We can now run other commands using this script name.

Run-Shell-Files-Debian

Run the following command in Terminal to create the script titled “myscript” as a result.

$ sudo nano myscript.sh

When you use the command, the Nano editor will correctly open an empty file.

Run-Shell-Files-Debian

Start by writing “#! /bin/bash” at the beginning of the file, then add each command you want to run in the script one at a time starting on the following line. In this instance, we want to create a script that prints the IP address, copies the files from the download folder into the documents, and prints the files that are available.

Run-Shell-Files-Debian

After writing your desired code you need to press the “CTRL +O” key to save your code.

Step 2: Enabling Permission to Execute

Generally, bash scripts are usually executable but if you don’t have these rights then you can enable them by writing the command mentioned below.

$ sudo chmod +x myscript.sh

Run-Shell-Files-Debian

Step 3: Execution

You’ll be able to correctly run the script after you’ve granted it execute access. Make care to first navigate to the current working directory if your script isn’t there already. The command to execute our “myscript.sh” script, for instance, would be:

$ ./myscript.sh

Run-Shell-Files-Debian

Other than using the /script name command, the following steps can be taken to launch the script:

$ bash myscript.sh

Along with either the /script name command or the bash command, the following third method can be used to start the script:

$ sh myscript.sh

Run-Shell-Files-Debian

Conclusion

We have covered the fundamentals of shell files in this post, followed by instructions on how to execute the script in Debian. These actions entail authoring the script, changing its permissions, and running the script. We’ve learned how to run an a.sh file shell script on Debian by using the chmod and dot (.) or sh/bash commands.

Print Friendly, PDF & Email
Categories