Bookmark this page

Guided Exercise: Write Simple Bash Scripts

Write a simple Bash script with a sequence of commands and run it from the command line.

Outcomes

  • Write and execute a simple Bash script.

  • Redirect the output of a simple Bash script to a file.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

[student@workstation ~]$ lab start console-write

Instructions

  1. Log in to the servera machine as the student user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$
  2. Create and execute a simple Bash script.

    1. Use the vim command to create the firstscript.sh file under your home directory.

      [student@servera ~]$ vim firstscript.sh
    2. Insert the following text, and save the file. The number of hash signs (#) is arbitrary.

      #!/usr/bin/bash
      echo "This is my first bash script" > ~/output.txt
      echo "" >> ~/output.txt
      echo "#####################################################" >> ~/output.txt
    3. Use the bash command to execute the script.

      [student@servera ~]$ bash firstscript.sh
    4. Review the output file that the script generated.

      [student@servera ~]$ cat output.txt
      This is my first bash script
      
      #####################################################
  3. Add more commands to the firstscript.sh script, execute it, and review the output.

    1. Use the Vim text editor to edit the firstscript.sh script.

      [student@servera ~]$ vim firstscript.sh

      The following output shows the expected content of the firstscript.sh file:

      #!/usr/bin/bash
      #
      echo "This is my first bash script" > ~/output.txt
      echo "" >> ~/output.txt
      echo "#####################################################" >> ~/output.txt
      echo "LIST BLOCK DEVICES" >> ~/output.txt
      echo "" >> ~/output.txt
      lsblk >> ~/output.txt
      echo "" >> ~/output.txt
      echo "#####################################################" >> ~/output.txt
      echo "FILESYSTEM FREE SPACE STATUS" >> ~/output.txt
      echo "" >> ~/output.txt
      df -h >> ~/output.txt
      echo "#####################################################" >> ~/output.txt
    2. Make the firstscript.sh file executable by using the chmod command.

      [student@servera ~]$ chmod a+x firstscript.sh
    3. Execute the firstscript.sh script.

      [student@servera ~]$ ./firstscript.sh
    4. Review the output file that the script generated.

      [student@servera ~]$ cat output.txt
      This is my first bash script
      
      #####################################################
      LIST BLOCK DEVICES
      
      NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
      sr0     11:0    1  558K  0 rom
      vda    252:0    0   10G  0 disk
      ├─vda1 252:1    0    1M  0 part
      ├─vda2 252:2    0  200M  0 part /boot/efi
      ├─vda3 252:3    0  500M  0 part /boot
      └─vda4 252:4    0  9.3G  0 part /
      vdb    252:16   0    5G  0 disk
      vdc    252:32   0    5G  0 disk
      vdd    252:48   0    5G  0 disk
      
      #####################################################
      FILESYSTEM FREE SPACE STATUS
      
      Filesystem      Size  Used Avail Use% Mounted on
      devtmpfs        844M     0  844M   0% /dev
      tmpfs           888M     0  888M   0% /dev/shm
      tmpfs           355M  9.4M  346M   3% /run
      /dev/vda4       9.4G  1.7G  7.7G  18% /
      /dev/vda3       495M  161M  335M  33% /boot
      /dev/vda2       200M  7.6M  193M   4% /boot/efi
      tmpfs           178M     0  178M   0% /run/user/1000
      #####################################################
  4. Remove the exercise files and return to the workstation machine.

    1. Delete the firstscript.sh and output.txt files.

      [student@servera ~]$ rm firstscript.sh output.txt
    2. Return to the workstation machine as the student user.

      [student@servera ~]$ exit
      logout
      Connection to servera closed.
      [student@workstation ~]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish console-write

Revision: rh134-9.3-5fd2368