(Linux) Use the following shell script to answer the questions: x=$1 script ~/bi
ID: 3712263 • Letter: #
Question
(Linux)
Use the following shell script to answer the questions:
x=$1
script ~/bin/lab$x.out
a2ps -A fill -C -E -g -o - ~/bin/ci132-lab$x-q??.sh ~/bin/lab$x.out | ps2pdf - /opt/ci132/submit/ci132-lab$x-username.pdf
a. Extend the shell script to display an appropriate error and terminate the script if a
command line argument is not provided.
b. Extend the previous shell script to also ensure the command line argument is an integer and shell
scripts exist matching the file naming convention (ci152-labxx-qyy.sh (Where xx is the lab number and yy is the question number)). Display an appropriate error
and terminate the shell script if either condition is not met.
Explanation / Answer
To test if an argument is an integer, it is sufficient to test if it is greater or equal than zero (which an integer should be) if that test fails you know it's not an integer.
Part a:
x=$1
if (!( x >= 0 )) 2>/dev/null; then
echo "The argument is not an integer"
exit 1
fi
script ~/bin/lab$x.out
a2ps -A fill -C -E -g -o - ~/bin/ci132-lab$x-q??.sh ~/bin/lab$x.out | ps2pdf - /opt/ci132/submit/ci132-lab$x-username.pdf
Part b:
To match regexes you need to use the =~ operator.
x=$1
if (!( x >= 0 )) 2>/dev/null; then
echo "The argument is not an integer"
exit 1
fi
FILEPATH='~/bin/ci132-lab$x-q??.sh'
echo "The file convention is not matched"
exit 1
script ~/bin/lab$x.out
a2ps -A fill -C -E -g -o - ~/bin/ci132-lab$x-q??.sh ~/bin/lab$x.out | ps2pdf - /opt/ci132/submit/ci132-lab$x-username.pdf
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.