LINUX: Shell Scripting Exercises

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
Search for a command to run...

A Tech Lead by profession, a Data Enthusiast and a forever learner. Experienced in DevOps, Cloud, FullStack Development, Data Science, Machine Learning and AI.
No comments yet. Be the first to comment.
Hashnode introduced 'headless' and while having a look at it, It really seemed interesting. It's nice to have a way to play with our publications and posts from Hashnode, outside of Hashnode without tedious data scraping. While reading another blog, ...

While trying to get Red Hat (RHEL9) installed in my WSL (Windows Subsystem for Linux) within my Windows 11 laptop, I stumbled upon the same hurdle I did for installing the older RHEL8. The list of available distros published by Microsoft still does n...

When it comes to collaborative software development, efficiency in communication and planning are critical. GitHub offers a feature called "Code owners" to improve project management and facilitate cooperation. Let's see how this can improve your dev...

PowerShell is a powerful tool, allowing to perform a myriad of tasks with just a few commands. However, there are times when elevated privileges are required to execute certain commands. This is as easy as right-clicking PowerShell and "Run As Admini...

While trying to setup RHEL with WSL 2, I stumbled upon this very helpful blog https://wsl.dev/mobyrhel8/ While doing the setup, I found this zsh theme very nice, and I have been using it since then. The above mentioned blog already has the steps, but...

Note : These exercises are from “Linux Training Academy’s” Shell Scripting course.
Exercise 1: Write a shell script that prints “Hello World!” to the screen.
Hint 1: Remember to make the shell script executable with the chmod command.
Hint 2: Remember to start your script with a shebang!
#!/bin/bash
echo "Hello World!"
Exercise 2: Modify the shell script from exercise 1 to include a variable. The variable will hold the contents of the message “Hello World!”.
#!/bin/bash
THE_MESSAGE="Hello World!"
echo "$THE_MESSAGE"
Exercise 3: Store the output of the command “hostname” in a variable. Display “This script is running on .” where “ “ is the output of the “hostname” command.
Hint: It’s a best practice to use the ${VARIABLE} syntax if there is text or characters that directly precede or follow the variable.
#!/bin/bash
THE_HOSTNAME=$(hostname)
echo "The script is running on $THE_HOSTNAME"
Exercise 4: Write a shell script to check to see if the file “/etc/shadow” exists. If it does exist, display “Shadow passwords are enabled.” Next, check to see if ou can write to the file. If you can, display “You have permissions to edit /etc/shadow.” If you cannot, display “You do NOT have permissions to edit /etc/shadow.”
#!/bin/bash
THE_PATH="/etc/shadow"
if [ -e $THE_PATH ]
then
echo "Shadow Passwords are enabled"
if [ -w $THE_PATH ]
then
echo "You have permission to edit $THE_PATH"
else
echo "Tou do NOT have permission to edit $THE_PATH"
fi
else
echo "Shadow Passowrds not enabled"
fi
Exercise 5: Write a shell script that displays “man”, “bear”, “pig”, “dog”, “cat”, and sheep to the screen with each appearing on a separate line. Try to do this in as few lines as possible. Hint: Loops can be used to perform repetitive tasks.
#!/bin/bash
ANIMALS="man bear pig dog cat sheep"
for ANIMAL in $ANIMALS
do
echo "$ANIMAL"
done
Exercise 6: Write a shell script that prompts the user for a name of a file or directory and reports if it is a regular file, a directory, or other type of file. Also perform an ls command against the file or directory with the long listing option.
#!/bin/bash
read -p "Enter a path for file/folder : " THE_PATH
if [ -d $THE_PATH ]
then
echo "$THE_PATH is a directory"
elif [ -f $THE_PATH ]
then
echo "$THE_PATH is a simple file"
elif [ -e $THE_PATH ]
then
echo "$THE_PATH is not a simple file"
else
echo "$THE_PATH does NOT exist!!"
fi
echo "$(ls -l $THE_PATH)"
Exercise 7: Modify the previous script so that it accepts the file or directory name as an argument instead of prompting the user to enter it.
#!/bin/bash
THE_PATH=$1
if [ -d $THE_PATH ]
then
echo "$THE_PATH is a directory"
elif [ -f $THE_PATH ]
then
echo "$THE_PATH is a simple file"
elif [ -e $THE_PATH ]
then
echo "$THE_PATH is not a simple file"
else
echo "$THE_PATH does NOT exist!!"
fi
echo "$(ls -l $THE_PATH)"
Exercise 8: Modify the previous script to accept an unlimited number of files and directories as arguments. Hint: You’ll want to use a special variable.
#!/bin/bash
for THE_PATH in $@
do
if [ -d $THE_PATH ]
then
echo "$THE_PATH is a directory"
elif [ -f $THE_PATH ]
then
echo "$THE_PATH is a simple file"
elif [ -e $THE_PATH ]
then
echo "$THE_PATH is not a simple file"
else
echo "$THE_PATH does NOT exist!!"
fi
echo "$(ls -l $THE_PATH)"
done