Bash Scripting Advanced - MD
Bash Scripting Advanced - MD
html
Objectives
After completing this lab you will be able to make use of the following features of the bash shell
Metacharacters
Quoting
Variables
Command substitution
I/O Redirection
Pipes and Filters
Command line arguments
Exercise 1 - Metacharacters
Run the commands given below on a newly opened terminal.
Click here for Hint
There are several characters which have special meanings to the shell.
Following are some of the special characters and their usage.
1.1 '#' - For adding comments
Lines beginning with a # (with the exception of #!) are comments and will not be executed.
# This is a comment line
pwd;date
Exercise 2 - Quoting
If any special character has to be treated without their special meaning, we need to quote them.
The following examples show how quoting is done in shell.
2.1 Quoting using backslash (\)
Backslash removes the meaning of the special character that follows it.
echo The symbol for multiplicaton is \*
Create a new variable called 'balance' with a value of 10000. List all the variables again.
balance=10000
Run the set command to check if the variable balance has been created.
set
Run the set command to check if the variable balance has been removed.
set
echo $myip
The output of command which cat is the path to the command cat. This path is sent to ls -l as an input.
You should see the permissions
for the file cat in the output.
5.2 Save the output of date command into the file 'output.txt'.
date > output.txt
You should see the output of uname and date commands appended to the file newoutput.txt
5.4 Dipslay the contents of file 'newoutput.txt' in all uppercase.
You can use the command tr for this translation.
tr command does not accept file names as arguments. But it accepts standard input.
So, we will redirect the content of file 'newoutput.txt' to the input of tr command.
tr "[a-z]" "[A-Z]" < newoutput.txt
You should see the list of five largest files from the /bin directory.
#! /bin/bash
In the output you should see number of files and directories in the directory /tmp.
Practice exercises
1. Problem.
Create a variable called 'color' and store the string 'light green' in it.
Display the list of all the files whose name starts with 'b' and ends with '.log' in the directory /var/log.
3. Problem.
Display the count of all files whose name starts with 'c' in the /bin directory.
4. Problem.
5. Problem.
6. Problem.
Write a shell script named latest_warnings.sh that prints the latest 5 warnings from the /var/log/bootstrap.log file.
Other Contributors
Rav Ahuja
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2021-05-30 0.1 Ramesh Sannareddy Created initial version of the lab
Copyright (c) 2021 IBM Corporation. All rights reserved.