Red Hat System Administration II
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
Log in to the
serveramachine as thestudentuser.[student@workstation ~]$
ssh student@servera...output omitted... [student@servera ~]$Create and execute a simple Bash script.
Use the
vimcommand to create thefirstscript.shfile under your home directory.[student@servera ~]$
vim firstscript.shInsert 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
Use the
bashcommand to execute the script.[student@servera ~]$
bash firstscript.shReview the output file that the script generated.
[student@servera ~]$
cat output.txtThis is my first bash script #####################################################
Add more commands to the
firstscript.shscript, execute it, and review the output.Use the Vim text editor to edit the
firstscript.shscript.[student@servera ~]$
vim firstscript.shThe following output shows the expected content of the
firstscript.shfile:#!/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
Make the
firstscript.shfile executable by using thechmodcommand.[student@servera ~]$
chmod a+x firstscript.shExecute the
firstscript.shscript.[student@servera ~]$
./firstscript.shReview the output file that the script generated.
[student@servera ~]$
cat output.txtThis 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 #####################################################
Remove the exercise files and return to the
workstationmachine.Delete the
firstscript.shandoutput.txtfiles.[student@servera ~]$
rm firstscript.sh output.txtReturn to the
workstationmachine as thestudentuser.[student@servera ~]$
exitlogout Connection to servera closed. [student@workstation ~]$