Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Friday, September 17, 2021

Example of scripts and solution

Practice Exercises:

Exercise 1:

Write a shell script that prints "Shell Scripting is Fun!" on 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 !

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!".

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.

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 you 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."

Exercise 5:

Write a shell script that displays "man", "bear", "pig", "dog", "cat", and "sheep" on the screen with each appearing on[…]”


Excerpt From: Cannon, Jason. “Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming”. Apple Books. 



“Solutions to the Practice Exercises

Exercise 1:

#!/bin/bash

echo "Shell Scripting is Fun!"

Exercise 2:

#!/bin/bash

MESSAGE="Shell Scripting is Fun!"

echo "$MESSAGE"

Exercise 3:

#!/bin/bash

HOST_NAME=$(hostname)

echo "This script is running on ${HOST_NAME}."

Exercise 4:

#!/bin/bash

FILE="/etc/shadow"

if [ -e "$FILE" ]

then

  echo "Shadow passwords are enabled."

fi

if [ -w "$FILE" ]

then

  echo "You have permissions to edit ${FILE}."

else

  echo "You do NOT have permissions to edit ${FILE}."

fi

Exercise 5:

#!/bin/bash

for ANIMAL in man bear pig dog cat sheep

do

“ echo "$ANIMAL"

done

Exercise 6:

#!/bin/bash

read -p "Enter the path to a file or a directory: " FILE

if [ -f "$FILE" ]

then

  echo "$FILE is a regular file."

elif [ -d "$FILE" ]

then

  echo "$FILE is a directory."

else

  echo "$FILE is something other than a regular file or directory."

fi

ls -l $FILE

Exercise 7:

#!/bin/bash

FILE=$1

if [ -f "$FILE" ]

then

  echo "$FILE is a regular file."

elif [ -d "$FILE" ]

then

  echo "$FILE is a directory."

else

  echo "$FILE is something other than a regular file or directory."

fi

ls -l $FILE

Exercise 8:

#!/bin/bash

for FILE in $@

do

  if [ -f "$FILE" ]

  then

    echo "$FILE is a regular file."

  elif [ -d "$FILE" ]

  then

    echo "$FILE is a directory."

  else

    echo "$FILE is something other than a regular file or directory."

  fi

  ls -l $FILE

done”


Excerpt From: Cannon, Jason. “Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming”. Apple Books. 




Featured Post

Managing CA Certificates on Red Hat Linux 9: Understanding update-ca-trust extract

  Managing CA Certificates on RHEL9 RHEL8 OracleLinux9 OracleLinux8 In today's digital landscape, securing communications and verifying ...