w3resource

Bash Script Execution Exercises, Solutions, and Explanations

1.

Write a Bash script that prints "Hello, World!" when executed.

Code:

#!/bin/bash

# Bash script to print "Hello, World!"
echo "Hello, World!"

Save the file with a ".sh" extension, for example, "test.sh".

Make the script executable by running the following command in the terminal:

chmod +x test.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

./test.sh

Output:

Hello, World!

2.

Write a Bash script that echoes the current date and time when executed.

Code:

#!/bin/bash

# Bash script to echo the current date and time
echo "Current date and time: $(date)"

Save the file with a ".sh" extension, for example, "test.sh".

Make the script executable by running the following command in the terminal:

chmod +x test.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

./test.sh

Output:

Current date and time: Tue Apr 16 18:45:12 IST 2014

3.

Write a Bash script that prints the contents of a file named "output.txt" when executed.

Code:

#!/bin/bash

# Bash script to print the contents of a file named "output.txt"
cat output.txt

Save the file with a ".sh" extension, for example, "test.sh".

Make the script executable by running the following command in the terminal:

chmod +x test.sh

Ensure that there is a file named "output.txt" in the same directory as the script, and it contains some text.

To execute the script, navigate to the directory where the script is saved and run the following command:

./test.sh

Output:

-rw-r--r-- 1 ad ad 67 Apr 13 18:11 error.log
-rw-r--r-- 1 ad ad 284 Apr 13 17:41 file1.txt
-rw-r--r-- 1 ad ad 284 Apr 13 17:41 file2.txt
-rw-r--r-- 1 ad ad 284 Apr 13 18:09 input.txt
-rw-r--r-- 1 ad ad     2 Apr 13 18:15 nums.txt
-rw-r--r-- 1 ad ad 284 Apr 13 18:10 output.txt
-rw-r--r-- 1 ad ad 313 Apr 13 18:13 sample.txt
drwxrwxrwx 1 ad ad 512 Jun 10  2022 test
-rwxr-xr-x 1 ad ad 200 Apr 13 18:17 test.sh

4.

Write a Bash script that executes another script named "test.sh" when executed.

Code:

#!/bin/bash

# Bash script to execute another script named "test.sh"
./test.sh

Save the file with a ".sh" extension, for example, "test1.sh".

Make the script executable by running the following command in the terminal:

chmod +x test1.sh

Ensure that there is a script named "test.sh" in the same directory as the script you just created, and it is executable.

To execute the script, navigate to the directory where the script is saved and run the following command:

./test1.sh

Output:

Current date and time: Wed Apr 17 08:12:40 IST 2014

5.

Write a Bash script that displays a message indicating the current user's username when executed.

Code:

#!/bin/bash

# Bash script to display the current user's username
echo "Current user's username: $(whoami)"

In the script above,

  • echo "Current user's username: $(whoami)": This line prints a message indicating the current user's username. The $(whoami) command is used to retrieve the current user's username.

Save the file with a .sh extension, for example, test1.sh.

Make the script executable by running the following command in the terminal:

chmod +x test1.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

./test1.sh

This will display the current user's username.

Output:

Current user's username: ad

6.

Write a Bash script that executes a command to list all files in the current directory when executed.

Code:

#!/bin/bash

# Bash script to list all files in the current directory
ls

In the script above,

  • ls: This command lists all files and directories in the current directory.

Save the file with a .sh extension, for example, test1.sh.

Make the script executable by running the following command in the terminal:

chmod +x test1.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

./test1.sh

This will list all files in the current directory.

Output:

abc.sh        error.log  file2.txt  nums.txt    sample.txt  test1.sh
document.txt  file1.txt  input.txt  output.txt  test        test.sh

7.

Write a Bash script that executes a command to display the system's uptime when executed.

Code:

#!/bin/bash

# Bash script to display system uptime
uptime

In the script above,

  • uptime: This command displays the system's uptime, showing how long the system has been running, the number of users currently logged in, and the system load averages for the past 1, 5, and 15 minutes.

Save the file with a .sh extension, for example, test1.sh.

Make the script executable by running the following command in the terminal:

chmod +x tes1.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

./test1.sh

Output:

08:20:40 up  1:02,  0 users,  load average: 0.52, 0.58, 0.59

8.

Write a Bash script that executes a command to display the available disk space when executed.

Code:

#!/bin/bash
# Bash script to display available disk space
df -h

In the script above,

  • df -h: This command displays information about the available disk space on the system. The -h option is used to show the output in a human-readable format.

Save the file with a .sh extension, for example, disk_space.sh.

Make the script executable by running the following command in the terminal:

 
chmod +x disk_space.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

 ./disk_space.sh

Output:

Filesystem      Size  Used Avail Use% Mounted on
rootfs          101G   89G   12G  89% /
root            101G   89G   12G  89% /root
home            101G   89G   12G  89% /home
data            101G   89G   12G  89% /data
cache           101G   89G   12G  89% /cache
mnt             101G   89G   12G  89% /mnt
none            101G   89G   12G  89% /dev
none            101G   89G   12G  89% /run
none            101G   89G   12G  89% /run/lock
none            101G   89G   12G  89% /run/shm
none            101G   89G   12G  89% /run/user
tmpfs           101G   89G   12G  89% /sys/fs/cgroup
C:\             101G   89G   12G  89% /mnt/c
D:\             245G   54G  191G  22% /mnt/d
E:\             245G   40G  205G  17% /mnt/e
F:\             245G   47G  198G  20% /mnt/f
G:\             199G  135G   65G  68% /mnt/g
H:\             151G   19G  133G  12% /mnt/h
I:\             196G   11G  185G   6% /mnt/i

9.

Write a Bash script that executes a command to display the system's memory usage when executed.

Code:

#!/bin/bash

# Bash script to display system memory usage
free -h

In the script above,

  • free -h: This command displays information about the system's memory usage. The -h option shows output in human-readable format.

Save the file with a .sh extension, for example, memory_usage.sh.

Make the script executable by running the following command in the terminal:

chmod +x memory_usage.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

./memory_usage.sh

Output:

                    total        used        free      shared  buff/cache   available
Mem:            31G        8.0G         23G         17M        230M         23G
Swap:           12G        9.1M         12G

10.

Write a Bash script that executes a command to display the system's CPU usage when executed.

Code:

#!/bin/bash

# Bash script to display system CPU usage
top -bn1 | grep "Cpu(s)"

In the script above,

  • top -bn1: This command displays information about the system's CPU usage. The -'b' option is used to run "top" in batch mode, and the '-n1' option specifies that "top" should only display one iteration of data.
  • grep "Cpu(s)": This command filters the output of "top" to only show lines containing "Cpu(s)".

Save the file with a .sh extension, for example, cpu_usage.sh.

Make the script executable by running the following command in the terminal:

chmod +x cpu_usage.sh

To execute the script, navigate to the directory where the script is saved and run the following command:

./cpu_usage.sh

Output:

%Cpu(s):  6.9 us,  1.8 sy,  0.0 ni, 91.1 id,  0.0 wa,  0.1 hi,  0.0 si,  0.0 st

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.