Welcome to My World (www.dgmayor.com)

소프트웨어/웹 서버 등 개발 환경 세팅

48. How to write Shell Script in Linux

dgmayor 2022. 8. 24. 21:15
728x90

Note: The steps explained in this post have been tested on Debian 10 (Buster) system.

Step 1: Creating the Shell Script

To create a shell script, you only need to create a plain text file using any text editor like Nano, Vim, Gedit, etc. Then in the text file, you will need to add the commands in the same sequence you want them to be executed. Remember following points when creating a shell script:

  1. Begin the shell script with “#!/bin/bash”
  2. Each line is treated as a command
  3. Start a comment line with #

We will be creating the script named “testscript” using the Nano editor; however, you can choose any editor of your choice. Before creating a script file, make sure its name does not conflict with the commands. You can check this by typing “which” followed by the script name:

$ which script_name

In our example, it would be:

$ which testscript

The following output shows there is no such command or program installed with the name “testscript”. Therefore, we can proceed with this name.

In order to create the script named “testscript”, run the following command in Terminal:

$ sudo nano testscript.sh

After running the above command, an empty file will be opened in the Nano editor. First, add “#!/bin/bash” at the beginning of the file. Now add the commands you like to run in the script with each command in a separate line. Here we want to create a script that can do the following tasks:

  • Print the IP address of our system
  • List files in the Downloads directory
  • Backup files from Downloads directory to the Documents directory

So, we have written the following lines of commands in our script:

#!/bin/sh

echo "Now we are going to print IP address"

ip addr | grep ens37

echo "Now we are going to ping Google website"

ping -c 2 google.com

echo "Now we are going to backup our Downloads directory to Documents directory"

cp -rv ~/Downloads ~/Documents

After entering the commands in the script, save and close it.

This is how our script file in Nano editor looks like:

Step 2: Set Execute Permissions

Now you will need to make the script file executable in order to run it as a program. You can so by running the following command in Terminal:

$ chmod +x script_name

In our example, it would be:

$ chmod +x testscript.sh

Step 3: Running the Shell Script

After the execute permissions have been applied to the script, you can run the script as follows

$ ./script_name

Note: If your script has been placed in some directory other than the current working directory, make sure to navigate to that directory first and then execute the script.

For instance, to run our script named “testscript.sh”, the command would be:

$ ./testscript.sh

After running the script, the shell will execute each command contained in the script in a sequence and will display the output. Following is the output of our script looks like:

Other than running the script using the ./script_name command, there are two more ways you can run the script, which is as follows:

$ bash script_name

$ sh script_name

This is how you can create and run a shell script in Debian 10 Buster system.

Shell scripts are just a series of commands that you add in a file and run them together. After following this post, we hope now you can easily create a shell script and automate repetitive jobs.

728x90