Skip to content

Ubuntu - Scripting

Shell scripting in Ubuntu allows you to automate tasks by writing a sequence of commands in a file. This is incredibly useful for system administration, automating repetitive actions, and creating custom tools. Let’s create a simple script to display the machine’s primary IP address.

Step 1 − Open a text editor. For command-line editing, nano is a user-friendly option. You can also use a graphical editor like Gedit (Text Editor). To open Gedit, search for ‘Text Editor’ in your applications menu and launch it. For this example, let’s use nano in the terminal:

nano display_ip.sh

This command opens nano with a new file named display_ip.sh.

Step 2 − Enter the following text into the editor. The first line, #!/bin/bash, is called a ‘shebang’. It tells the system that this script should be executed with Bash, a common shell in Linux.

#!/bin/bash
# Script to display the primary IP address
# Get the primary IP address (works for most common configurations)
# hostname -I provides all IP addresses, awk '{print $1}' takes the first one.
primary_ip=$(hostname -I | awk '{print $1}')
# Check if an IP address was found
if [ -n "$primary_ip" ]; then
echo "This machine's primary IP address is: $primary_ip"
else
echo "Could not determine the primary IP address."
fi

Let’s break down the script:

  • #!/bin/bash: Shebang, specifies the interpreter.
  • # ...: Lines starting with ’#’ are comments, ignored by the interpreter.
  • primary_ip=$(hostname -I | awk '{print $1}'): This command captures the output. hostname -I lists all network interface IP addresses. awk '{print $1}' extracts the first IP address from that list. The result is stored in the variable primary_ip.
  • if [ -n "$primary_ip" ]; then ... else ... fi: This is a conditional statement. It checks if the primary_ip variable is not empty (-n). If it has a value, it prints the IP; otherwise, it prints an error message.
  • echo "...": This command prints text to the terminal.

Step 3 − Save the file and exit the editor. In nano, press Ctrl+O to write out (save), press Enter to confirm the filename, and then Ctrl+X to exit.

If you used a graphical editor like Gedit, save the file as display_ip.sh in your desired location (e.g., your home directory or Desktop).

Step 4 − Before you can run the script, you need to make it executable. Navigate to the directory where you saved the file (if you’re not already there) and use the chmod command:

chmod +x display_ip.sh

The chmod +x command adds execute permission to the file for all users. You can also use chmod u+x display_ip.sh to give execute permission only to the user who owns the file.

Step 5 − Now, execute the script. If you are in the same directory as the script, run it using:

./display_ip.sh

The ./ tells the shell to look for the script in the current directory.

The output should be similar to: This machine's primary IP address is: 192.168.1.10 (the IP address will vary based on your network configuration). If no IP is found, it will display the error message.

This is a basic example, but scripting can be used for complex tasks like backups, system monitoring, software deployment, and much more. Common tools used in scripts include grep, sed, awk, find, and control structures like loops (for, while) and conditionals (if).

Common Pitfall: Older tutorials might use ifconfig. The ifconfig command is deprecated in modern Linux systems. The ip command (ip addr) or hostname -I are the preferred modern alternatives for network information.