0% found this document useful (0 votes)
11 views5 pages

Guided Exercices

Uploaded by

abmanel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Guided Exercices

Uploaded by

abmanel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Guided Exercices- Shell Scripting

Exercise_1 - Write a shell script that prints “Shell Scripting is Fun!” on

the screen
#!/bin/bashecho “Shell Scripting is Fun!”

Output
[svimukthi@sanka Shell_Scripting]$ ./exe1.sh
Shell Scripting is Fun!

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!”
#!/bin/bash
NAME=”Shell Scripting is Fun!”
echo $NAME

Output
[svimukthi@sanka Shell_Scripting]$ ./exe2.sh
Shell Scripting is Fun!

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/bash
HOSTNAME=$(hostname)
echo “This script is running on $HOSTNAME”

Output
[svimukthi@sanka Shell_Scripting]$ ./exe3.sh
This script is running on vimukthi
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

Output
[svimukthi@sanka Shell_Scripting]$ ./exe4.sh
/home/svimukthi/Assignment/sanka passwords are enabled
You have permition to execute /home/svimukthi/Assignment/sanka

Exercise_5 - Write a shell script that displays

“man”,”bear”,”pig”,”dog”,”cat”,and “sheep” on the screen with each

appearing on a separate line. Try to do this in as few lines as possible.

#!/bin/bash
ANIMALS=”man bear pig dog cat sheep”
for ANIMAL in $ANIMALS
do
echo $ANIMAL
done

Output
[svimukthi@sanka Shell_Scripting]$ ./exe5.sh
man
bear
pig
dog
cat
sheep

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.


#!/bin/bash
echo “Enter the file path”
read FILE
if [ -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”
fi
ls -l $FILE

Output
[svimukthi@sanka Shell_Scripting]$ ./exe6.sh
Enter the file path
/home/svimukthi/sanka
/home/svimukthi/sanka is a directory
total 4
drwxr-xr-x. 2 svimukthi svimukthi 30 Nov 13 20:18 hs
-rw-rw-r — . 1 svimukthi svimukthi 12 Nov 12 12:09 sanka.txt
d — x — — — . 2 svimukthi svimukthi 20 Dec 13 18:53 test
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/bash

FILE=$1
if [ -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
[svimukthi@sanka Shell_Scripting]$ ./exe7.sh
/home/svimukthi/sanka
/home/svimukthi/sanka is a directory
total 4
drwxr-xr-x. 2 svimukthi svimukthi 30 Nov 13 20:18 hs
-rw-rw-r — . 1 svimukthi svimukthi 12 Nov 12 12:09 sanka.txt
d — x — — — . 2 svimukthi svimukthi 20 Dec 13 18:53 test

Exercise_8 - Modify the previous script to accept an unlimited number of

files and directories as arguments.

#!/bin/bash
FILES=$@
for FILE in $FILES
do
if [ -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”
fi
ls -l $FILE done

Output
[svimukthi@sanka Shell_Scripting]$ ./exe8.sh
/home/svimukthi/sanka /hms/apps
/home/svimukthi/sanka is a directory
total 4
drwxr-xr-x. 2 svimukthi svimukthi 30 Nov 13 20:18 hs
-rw-rw-r — . 1 svimukthi svimukthi 12 Nov 12 12:09 sanka.txt
d — x — — — . 2 svimukthi svimukthi 20 Dec 13 18:53 test
/hms/apps is a directory
total 60
drwx — — — . 7 svimukthi svimukthi 4096 Dec 7 15:08 ajuba
drwx — — — . 7 svimukthi svimukthi 4096 Dec 7 15:08 ajuba-
preference-data-loader
drwx — — — . 7 svimukthi svimukthi 4096 Nov 30 10:25 ajuba-survey
-rw-rw-r — . 1 svimukthi svimukthi 22772 Dec 10 17:01 sendKindle-
2.1–6.el7.psychotic.noarch.rpm
-rw-rw-r — . 1 svimukthi svimukthi 19875 Dec 10 17:04 sendKindle-
2.1–6.el7.psychotic.src.rpm
drwxr-xr-x. 2 svimukthi svimukthi 4096 Dec 17 08:24
Versions_Script

You might also like