4) Write a Bash shell script named “shiftCase.sh” that a. Convert the supplied s
ID: 3587077 • Letter: 4
Question
4) Write a Bash shell script named “shiftCase.sh” that
a. Convert the supplied string arguments to all uppercase or all lowercase depending on the
supplied flag
b. If the flag is “-l” performs the lowercase conversion and print the result to screen
c. If the flag is “-u” performs the uppercase conversion and print the result to screen
CPSC-254 Fall 2017 Prof. Thomas Nguyen Assignment #4 pg. 2
d. Only alphabetic characters are converted. Non-alphabetic characters (such as numbers,
punctuation marks, etc.) are not affected.
e. Exactly one of two options (-l and -u) must be specified. If none are specified, or both are
specified, or something other than -l or -u is specified, print an error message and exit.
f. If there is no argument specified, then print an error message and exit
g. If there is more than one argument, convert each argument and print each result on a
separate line
h. Hint: Use getopts and tr commands
5) Write a Bash shell script named “getAbsPath.sh” that does the following:
a. Take 1 or more relative paths to files or directories as arguments and print the
corresponding absolute paths
b. For each argument given on the command line, print the argument as is, followed by “ => ”,
followed by the corresponding absolute path of the argument
c. If argument is a file or directory that doesn’t exist, print “does not exist” after “ => “
d. If argument is already an absolute path, print “already absolute path” after “ => “
e. If argument is a symbolic link you do not have to follow the link, just determine the absolute
path of the symbolic link
f. If there is no argument, print an error message and exit
g. Important: You can NOT use “realpath” or “readlink” commands.
h. Hint: Use pwd and basename and dirname commands
need 5 ASCII text files, one for each script. The files must be ready for execution, i.e.
requiring no changes (other than setting the execute permission bits).
use the following function to determine whether something is a valid integer.
isInteger() {
if [[ $1 =~ ^[+-]?[0-9]+$ ]]; then
return 0
else
return 1
fi
}
Explanation / Answer
4.
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No arguments"
exit 1
fi
sum=0
num2=1
while [ $# -gt 0 ]
do
case $1 in
"-l")
sum=`expr $sum + $num2`
shift
;;
"-u")
sum=`expr $sum + $num2`
shift
;;
*)
echo "Unexpected argument"
exit -1
;;
esac
done
echo $sum
if [ $sum -gt 1 ]
then
echo "Use only one argument switch"
exit 1
fi
while [ $# -gt 0 ]
do
case $1 in
"-l")
while [ $# -gt 0 ]
do
#echo "$#"
shift
y="$1"
echo "${y,,}"
done
exit -1
;;
"-u")
while [ $# -gt 0 ]
do
#echo "$#"
shift
y="$1"
echo "${y^^}"
done
exit -1
;;
*)
echo "Unexpected argument"
exit -1
;;
esac
done
5.
#!/bin/sh
if [ $# -eq 0 ]
then
echo "No arguments"
exit 1
fi
while [ $# -gt 0 ]
do
if [ -f "$1" ]
then
echo "$1 exists and is a regular file" > /dev/null
ABS_PATH=`echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")`
elif [ -d "$1" ]
then
echo "$1 exists and is a directory" > /dev/null
ABS_PATH=`cd "$1"; pwd`
elif [[ "$1" = /* ]]; then
echo "$1 => already a absolute path"
elif [ -e "$1" ]
then
echo "$1 => exists but is something else"
exit -1
else
echo "$1 => does not exist"
exit -1
fi
echo "$1 => $ABS_PATH"
shift
done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.