Shell Scripting Exercises - Sanka Dharmarathna - Medium - Reader View
Shell Scripting Exercises - Sanka Dharmarathna - Medium - Reader View
medium.com /@sankad_19852/shell-scripting-exercises-5eb7220c2252
Exercise_1 - Write a shell script that prints “Shell Scripting is Fun!” on the screen
Output
Exercise_2 - Modify the shell script from exercise 1 to include a variable. The variable will
hold the contents of the message “Shell Scripting is Fun!”
Output
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.
#!/bin/bashHOSTNAME=$(hostname)
echo “This script is running on $HOSTNAME”
Output
Exercise_4 - Write a shell script to check to see if the file “file_path” exists. If it does
exist, display “file_path passwords are enabled.” Next, check to see if you can write to
the file. If you can, display “You have permissions to edit “file_path.””If you cannot,
display “You do NOT have permissions to edit “file_path””
#!/bin/bashFILE=”/home/svimukthi/Assignment/sanka”if [ -e “$FILE” ]
then
echo “$FILE passwords are enabled”
fiif [ -x “$FILE” ]
then
echo “You have permition to execute $FILE” else
echo “You do Not have permissions to execute $FILE”
fi
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 1/8
4/13/23, 11:39 PM Shell Scripting Exercises - Sanka Dharmarathna - Medium
Output
Output
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 another type of file. Also perform an ls
command against the file or directory with the long listing option.
Output
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 2/8
4/13/23, 11:39 PM Shell Scripting Exercises - Sanka Dharmarathna - Medium
Exercise_7 - Modify the previous script to that it accepts the file or directory name as an
argument instead of prompting the user to enter it.
#!/bin/bashFILE=$1if [ -f “$FILE” ]
then
echo “$FILE is a reguler file”elif [ -d “$FILE” ]
then
echo “$FILE is a directory”else
echo “$FILE is another type of file”
fils -l $FILE
Output
Exercise_8 - Modify the previous script to accept an unlimited number of files and
directories as arguments.
Output
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 3/8
4/13/23, 11:39 PM Shell Scripting Exercises - Sanka Dharmarathna - Medium
Exercise_9 - Write a shell script that displays, “This script will exit with 0 exit status.” Be
sure that the script does indeed exit with a 0 exit status.
Output
Exercise_10 - Write a shell script that accepts a file or directory name as an argument.
Have the script report if it is reguler file, a directory, or another type of file. If it is a
directory, exit with a 1 exit status. If it is some other type of file, exit with a 2 exit status.
#!/bin/bashFILE=$1if [ -f $FILE ]
then
echo “It is reguler File”
exit 0elif [ -d $FILE ]
then
echo “It is directory”
exit 1 else
echo “Another type”
exit 2
fi
output
Output
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 4/8
4/13/23, 11:39 PM Shell Scripting Exercises - Sanka Dharmarathna - Medium
Exercise_12 - Write a shell script that consists of a function that displays the number of
files in the present working directory. Name this function “file_count” and call it in your
script. If you use variable in your function, remember to make it a local variable.
#!/bin/bashfunction file_count()
{
local NUMBER_OF_FILE=$(ls -l | wc -l)
echo "$NUMBER_OF_FILE"
}file_count
Output
Exercise_13 - Modify the script from the previous exercise. Make the “file_count”
function accept a directory as an argument. Next, have the function display the name of
the directory followed by a colon. Finally display the number of files to the screen on the
next line. Call the function three times. First on the “/etc” directory, next on the “/var”
directory and finally on the “/usr/bin” directory.
#!/bin/bashfunction file_count()
{
local Directory=$1
COUNT_FILE=$(ls $Directory|wc -l)
echo "$Directory"
echo "$COUNT_FILE"
}file_count /etc
file_count /var
file_count /usr/bin
Output
Exercise_14 - Write the shell script that renames all files in the current directory that end
in “.jpg” to begin with today’s date in the following format: YYYY-MM-DD. For example, if
a picture of my cat was in the current directory and today was October 31,2016 it would
change name from “mycat.jpg” to “2016–10–31-mycat.jpg”.
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 5/8
4/13/23, 11:39 PM Shell Scripting Exercises - Sanka Dharmarathna - Medium
#!/bin/bashDAY=$(date +%F)
cd /home/vimukthi/Picturesfor FILE in *.png
do
mv $FILE ${DAY}-${FILE}
done
Exercise_15 - Write the script that renames files based on the file extension. Next,It
should ask the user what prefix to prepend to the file name(s). By default, the prefix
should be the current date in YYYY-MM-DD format. If the user simply press enter,the
current date will be used. Otherwise,whatever the user entered will be used as the prefix.
Next,it should display the original file name and new name of the file. Finally,it should
rename the file.
#!/bin/bashcd /home/vimukthi/Pictures
DAY=$(date +%F)secho "Pleace enter the file extension:"
read EXTENSIONecho "Pleace enter the prifix:(press enter for $DAY)"
readfor NAME in *.$EXTENSION
do
echo "Renaming $NAME to ${DAY}-${NAME}"
mv $NAME ${DAY}-${NAME}
done
Exercise_16 - Created the start-up script for an application start and stop.
#!/bin/bashINPUT=$1
cd /hms/installs/mongod/mongodb-linux-x86_64-2.6.0/bin
case $INPUT instart)
./mongod -f ../../mongod.conf &
echo "Mongodb server Start"
;;stop)
PID_ID=$(ps -ef | grep mongo | cut -d" " -f3 | sed '1!d')
kill $PID_IDif [ $? -eq '0']
echo "Mongodb server Stop"
;;*)
echo "Error input"
;;esac
Exercise_17 - Write the shell script that displays one random number on the screen and
also generates a system log message with that random number.Use the “user” facility
and “info” facility for your messages.
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 6/8
4/13/23, 11:39 PM Shell Scripting Exercises - Sanka Dharmarathna - Medium
Exercise_18 - Modify the previous script to that it uses a logging function. Additionally,
tag each syslog message with “randomly” and include process ID. Generate a 3 random
numbers.
#!/bin/bashfunction logging()
{
MESSAGE=$@
SET_MESSAGE="Random Number is:$MESSAGE"
echo "$SET_MESSAGE"
logger -i -p user.info "$SET_MESSAGE"
}logging $RANDOM
logging $RANDOM
logging $RANDOM
Exercise_19 - Write a shell script that exits on error and displays command as they will
execute, including all expansions and substitutions. Use 3 ls command in your script.
Make the first one succeed, the second one fail, and third one succeed. If you are using
the proper options, the third ls command not be executed.
Output
Exercise_20 - Modify the previous exercise so that script continuous, even if an error
occurs. This time, all three ls command will execute.
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 7/8
4/13/23, 11:39 PM Shell Scripting Exercises - Sanka Dharmarathna - Medium
Output
chrome-extension://ecabifbgmdmgdllomnfinbmaellmclnh/data/reader/index.html?id=1928955621&url=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedium.com%2F%40sa… 8/8