Problem 2: Write a bash script that checks for options passed to it. Your script
ID: 3819158 • Letter: P
Question
Problem 2:
Write a bash script that checks for options passed to it.
Your script should:
(i) Generate error messages
(ii) Check for invalid options
(iii) Check for options d, p, and z, where option ‘d’ must take an argument
(iv) Print to the screen information informing which (if any) option was triggered
For example, again assume that your script is named my_script.sh, a user could invoke it by typing $ my_script.sh -p, $ my_script.sh -z, $ my_script.sh –d argument_to_d, or combination of these options.
Problem 2:
Write a bash script that checks for options passed to it.
Your script should:
(i) Generate error messages
(ii) Check for invalid options
(iii) Check for options d, p, and z, where option ‘d’ must take an argument
(iv) Print to the screen information informing which (if any) option was triggered
For example, again assume that your script is named my_script.sh, a user could invoke it by typing $ my_script.sh -p, $ my_script.sh -z, $ my_script.sh –d argument_to_d, or combination of these options.
Problem 2:
Write a bash script that checks for options passed to it.
Your script should:
(i) Generate error messages
(ii) Check for invalid options
(iii) Check for options d, p, and z, where option ‘d’ must take an argument
(iv) Print to the screen information informing which (if any) option was triggered
For example, again assume that your script is named my_script.sh, a user could invoke it by typing $ my_script.sh -p, $ my_script.sh -z, $ my_script.sh –d argument_to_d, or combination of these options.
Explanation / Answer
#!/bin/bash -l
#PBS -l nodes=4:ppn=8,walltime=24:00:00
cat $PBS_NODEFILE | uniq | tr '\012' ' ' > tmp-$PBS_JOBID
read -a NODE < tmp-$PBS_JOBID
rm tmp-$PBS_JOBID
inode=-1
ijob=0
usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1>&2; exit 1; }
while getopts ":s:p:" o; do
case "${o}" in
s)
s=${OPTARG}
((s == 45 || s == 90)) || usage
;;
p)
p=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${s}" ] || [ -z "${p}" ]; then
usage
fi
echo "s = ${s}"
echo "p = ${p}"
for ((K=1;K<=8;K++))
do
[ $((ijob++ % 2)) -eq 0 ] && ((inode++))
ssh ${NODE[inode]} _somepath_/RUN$K/sub.script &
done
wait
exit 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.