Bookmark this page

Guided Exercise: Writing Simple Bash Scripts

In this exercise, you will write a simple Bash script containing a sequence of commands and run it from the command line.

Outcomes

You should be able to:

  • Write and execute a simple Bash script.

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

Log in to workstation as student using student as the password.

On workstation, run the lab console-write start command. This command runs a start script that determines if the servera machine is reachable on the network. The script will alert you if it is not available. It also installs the vim-enhanced package if needed.

[student@workstation ~]$ lab console-write start
  1. From workstation, open an SSH session to servera as student. The systems are configured to use SSH keys for authentication, therefore a password is not required.

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

    1. Use the vim text editor to create a new text file under your home directory, and name it firstscript.sh

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

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

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

      [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 firstscript.sh

      [student@servera ~]$ vim firstscript.sh
    2. Append the following lines in bold to the firstscript.sh file.

      #!/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
      
    3. Make the firstscript.sh file executable using the chmod command.

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

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

      [student@servera ~]$ cat output.txt
      This is my first bash script
      
      #####################################################
      LIST BLOCK DEVICES
      
      NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
      sr0     11:0    1 1024M  0 rom
      vda    252:0    0   10G  0 disk
      └─vda1 252:1    0   10G  0 part /
      vdb    252:16   0    5G  0 disk
      
      #####################################################
      FILESYSTEM FREE SPACE STATUS
      
      Filesystem      Size  Used Avail Use% Mounted on
      devtmpfs        892M     0  892M   0% /dev
      tmpfs           915M     0  915M   0% /dev/shm
      tmpfs           915M   17M  899M   2% /run
      tmpfs           915M     0  915M   0% /sys/fs/cgroup
      /dev/vda1        10G  1.5G  8.6G  15% /
      tmpfs           183M     0  183M   0% /run/user/1000
      #####################################################
  4. Remove the exercise files and log off from servera.

    1. Delete the script file firstscript.sh and output file output.txt.

      [student@servera ~]$ rm firstscript.sh output.txt
    2. Log off from servera.

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

Finish

On workstation, run the lab console-write finish script to complete this exercise.

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

This concludes the guided exercise.

Revision: rh134-8.2-f0a9756