1. Write a shell script that prints "Shell Scripting is Fun!" on the screen.
#!/bin/bash
echo "Shell Scripting is Fun!"
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
message="Shell Scripting is Fun!"
echo "$message"
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
my_hostname=$(hostname)
echo "This script is running on $my_hostname."
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/bash
file_path="/path/to/your/file"
if [ -e "$file_path" ]; then
echo "$file_path passwords are enabled."
if [ -w "$file_path" ]; then
echo "You have permissions to edit $file_path."
else
echo "You do NOT have permissions to edit $file_path."
fi
else
echo "$file_path does not exist."
fi
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
echo -e "man\nbear\npig\ndog\ncat\nsheep"
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
read -p "Enter the name of a file or directory: " item
if [ -e "$item" ]; then
if [ -f "$item" ]; then
echo "$item is a regular file."
elif [ -d "$item" ]; then
echo "$item is a directory."
else
echo "$item is another type of file."
fi
ls -l "$item"
else
echo "$item does not exist."
fi
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
item="$1"
if [ -z "$item" ]; then
echo "Usage: $0 <file-or-directory-name>"
exit 1
fi
if [ -e "$item" ]; then
if [ -f "$item" ]; then
echo "$item is a regular file."
elif [ -d "$item" ]; then
echo "$item is a directory."
else
echo "$item is another type of file."
fi
ls -l "$item"
else
echo "$item does not exist."
fi
8. Modify the previous script to accept an unlimited number of files and directories as arguments.
#!/bin/bash
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <file-or-directory-name> [more names...]"
exit 1
fi
for item in "$@"; do
echo "Processing: $item"
if [ -e "$item" ]; then
if [ -f "$item" ]; then
echo "$item is a regular file."
elif [ -d "$item" ]; then
echo "$item is a directory."
else
echo "$item is another type of file."
fi
ls -l "$item"
else
echo "$item does not exist."
fi
done
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.
#!/bin/bash
echo "This script will exit with 0 exit status."
exit 0
10. Write a shell script that accepts a file or directory name as an argument. Have the script report if it
is a regular 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/bash
item="$1"
if [ -z "$item" ]; then
echo "Usage: $0 <file-or-directory-name>"
exit 99
fi
if [ ! -e "$item" ]; then
echo "$item does not exist."
exit 99
fi
if [ -f "$item" ]; then
echo "$item is a regular file."
exit 0
elif [ -d "$item" ]; then
echo "$item is a directory."
exit 1
else
echo "$item is another type of file."
exit 2
fi