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
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 ~]$
Create and execute a simple Bash script.
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
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
Use the sh command to execute the script.
[student@servera ~]$sh firstscript.sh
Review the output file generated by the script.
[student@servera ~]$cat output.txtThis is my first bash script #####################################################
Add more commands to the firstscript.sh script, execute it, and review the output.
Use the vim text editor to edit firstscript.sh
[student@servera ~]$vim firstscript.sh
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
Make the firstscript.sh file executable using the chmod command.
[student@servera ~]$chmod a+x firstscript.sh
Execute the firstscript.sh script.
[student@servera ~]$./firstscript.sh
Review the output file generated by the script.
[student@servera ~]$cat output.txtThis 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 #####################################################
Remove the exercise files and log off from servera.
Delete the script file firstscript.sh and output file output.txt.
[student@servera ~]$rm firstscript.sh output.txt
Log off from servera.
[student@servera ~]$exitlogout Connection to servera closed. [student@workstation ~]$
This concludes the guided exercise.