=========== Exercise 01 =========== Create a script called ex01.sh. This script
ID: 3730719 • Letter: #
Question
===========
Exercise 01
===========
Create a script called ex01.sh. This script should check the number of
command line arguments. If there are 2 or more is should print the
message:
CORRECT USAGE
and terminate with a return code of 0
If there are less than 2 command line arguments, it should print the
message:
INCORRECT USAGE
and terminate with a return code of 123
===========
Exercise 02
===========
Create a script called ex02.sh that behaves as follows:
1) If there is 1 command line arguments, the script should
print the message "usage: ex02.sh max|min|sum v1 [v2 ...]" and
exit with an error code of 1.
2) If there are one or more command line arguments, the first argument
must be either "min", "max" or "sum", if it is none of these, the
script should print "ERROR: invalid command: <command>" and exit
with an error code of 2.
3) Command line arguments after the first one should be treated as
integers. If the "max" command was given, the script should print
the message "MAX: <value>" where <value> is the largest integer
passed on the command line. If the "min" command was given, the
script should print the message "MIN: <value>" where <value> is
the smallest integer passed on the command line. If the "sum"
command was given, the script should print the message "SUM:
<value>" where <value> is the sum of the numbers passed on the
command line.
===========
Exercise 03
===========
Create a script called ex03.sh that behaves as follows:
1) It should accept a single command line argument. If there are zero
or more than one argument it should print the message "usage:
ex03.sh <filename>" and exit with return code 1
2) If the command line argument represents a regular file, the script
should print out the symbolic file permissions for the file's
owner, in upper-case and then exit with return code 0. For
example, if the file was readable and executable by the owner, the
script should output "R-X". Use the output from ls, cut and the tr command
to get these permissions, you should not need a bunch of if statements.
3) If the command line argument is not a regular file, the script
should print out: "invalid argument: <arg>" where <arg> is
whatever was passed in to the script and exit with return code 2.
===========
Exercise 04
===========
Create a script called ex04.sh that behaves as follows:
1) It should accept two command line arguments. If any other number
of arguments is passed in it should print the message "usage:
ex04.sh <arg1> <arg2>" and exit with return code 1.
2) The script should do a case insensitive comparison of the first two
arguments and if they match, exit with return code 0, otherwise
exit with a return code of 1. There should be no output on
standard output from this script.
===========
Exercise 05
===========
Create a script called ex05.sh that behaves as follows:
1) It should accept a single command line argument. If it is invoked
with any other number of arguments, it should print a message
"usage: ex05.sh <dir>" and exit with return code 1.
2) If the first argument is not a directory, the script should exit
with the message "invalid path: <value>" where <value> is whatever was
passed as the first argument and exit with return code 1.
3) Loop across all of the directories below the specified directory
and take the following actions:
a) If the directory does not have any files (directories are ok)
other than a file called EMPTY the script should create a zero
byte file called EMPTY in that directory. If the EMPTY file
exists, but does not have zero bytes, it should be removed and
replaced with a zero byte one.
b) Otherwise, the script should create a file called NONEMPTY that
contains only the number of files in that directory excluding
the NONEMPTY file (and excluding any directories).
Explanation / Answer
# Create a script called ex01.sh. This script should check the number of
# command line arguments. If there are 2 or more is should print the
# message:
# CORRECT USAGE
# and terminate with a return code of 0
# If there are less than 2 command line arguments, it should print the
# message:
# INCORRECT USAGE
# and terminate with a return code of 123
#!/bin/sh
#filename ex01.sh
if [ $# -gt 2 ]
then
echo "CORRECT USAGE"
return 0
else
echo "INCORRECT USAGE"
return 123
fi
----------------------------------------------------------------------
----------------------------------------------------------------------
# ===========
# Exercise 02
# ===========
# Create a script called ex02.sh that behaves as follows:
# 1) If there is 1 command line arguments, the script should
# print the message "usage: ex02.sh max|min|sum v1 [v2 ...]" and
# exit with an error code of 1.
# 2) If there are one or more command line arguments, the first argument
# must be either "min", "max" or "sum", if it is none of these, the
# script should print "ERROR: invalid command: <command>" and exit
# with an error code of 2.
# 3) Command line arguments after the first one should be treated as
# integers. If the "max" command was given, the script should print
# the message "MAX: <value>" where <value> is the largest integer
# passed on the command line. If the "min" command was given, the
# script should print the message "MIN: <value>" where <value> is
# the smallest integer passed on the command line. If the "sum"
# command was given, the script should print the message "SUM:
# <value>" where <value> is the sum of the numbers passed on the
# command line.
#!/bin/sh
#filename ex02.sh
#!/bin/sh
#filename ex02.sh
if [ $# -eq 1 ]
then
echo "usage: ex02.sh max|min|sum v1 [v2 ...]"
exit 1
elif [ "$1" != "max" -a "$1" != "min" -a "$1" != "sum" ]
then
echo "ERROR: invalid command: <command>"
exit 2
fi
if [ "$1" = "max" ]
then
shift
max=$1
shift
for i in "$@"; do
if [ $max -lt $i ]
then
max=$i
fi
done
echo "Max: $max"
fi
if [ "$1" = "min" ]
then
shift
min=$1
shift
for i in "$@"; do
if [ $min -gt $i ]
then
min=$i
fi
done
echo "MIN: $min"
fi
if [ "$1" = "sum" ]
then
shift
sum=0
for i in "$@"; do
sum=$(($sum + i));
done
echo "SUM: $sum"
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.