Solutions to CS160B Fall 2017 Final Exam

(Questions from Boyd | Solutions by Grace)

Question 1

Although question likely was meant to work in close proximity with the provided info sheet, we can assume that $thegroup contains just one line of the format: group:pword:group_id:members.

To grab the third column and put it into the variable thegid, do:

thegid="`cut -d: -f3,3 $thegroup`"; echo $thegid

Question 2

     if [ ! -w . ]
     then
       echo "Current directory is not writeable"
       exit
     fi

     echo "Current directory is writeable"
    

Question 3

    #!/bin/bash

    DIR=./*

    for f in $DIR
    do
      echo "Processing $f file ..."
      movefile = "`mv $f $f\.jpg`"
      echo $movefile
    done
    

Question 4

4 arguments ; only

2 arguments ; $LOGNAME

Question 5, Question 6 and Question 7

Per the suggestion, it is better to read Questions 6 and 7 for a full picture of the code required here. In fact, the way I approached this problem was to use skeleton code for Question 6 which includes a function at the top before modifying. I'll provide the answers here as such. I'll also accept pseudocode for significant points in this solution.


# Solution for Question 5
showon() {
  USERS="`users`"
  for i; do
    SEARCHING="`echo $USERS | grep $i `"
    if [[ ! -z "$SEARCHING" ]]
    then
      echo $i is logged on
    fi
  done
}



# Solution for Question 6
whoson() {
if [ $# -ne 1 ]
then
  echo "Requires exactly one argument"
  exit
fi

GROUPNAME=$1

GROUPLINE="`grep $GROUPNAME etcgroup`"

if [[ -z "$GROUPLINE" ]]
then
  echo "group $GROUPNAME does not exist"
  exit
fi

GROUPLINE="`echo $GROUPLINE | cut -d: -f 4,4`"
GROUPLINE="`echo $GROUPLINE | tr , \ `"

showon $GROUPLINE

return 0
}

# Solution for Question 7
IFS='\n'
GROUPNAMES=$(cut -d: -f 1 etcgroup)
unset IFS
while read -r line;
do
  if newc=$(whoson $line)
  then
    echo "**** $line ****"
    whoson $line
  fi
done <<< "$GROUPNAMES"

    

Question 8

group=$(id -gn)

Question 9

Use echo to check which part is not returning output

Question 10

set -f

This particular command turns off globbing

Question 11

Double quotes make this a literal interpretation. Need to use grep

Question 12

TBD

Question 13

isint() {

if [[ $1 =~ ^[1-9\-][0-9]*$ ]]; then
  return 0
else
  return 1
fi

}

Question 14

TBD