OPS435 Lecture 5 - Bash

From CDOT Wiki
Jump to: navigation, search
  • Program control:
    • while
    • for
      • seq command
  • Combining everything we learned so far
  • Scripts from OPS335 Labs

In-class examples

forloop.sh:

#!/bin/bash

VAR=one
echo $VAR
VAR=two
echo $VAR

#for I_LIKE_VARIABLES in {0..100}
#for I_LIKE_VARIABLES in `seq 100`
for I_LIKE_VARIABLES in a b c
do
  echo "Var is now: "$I_LIKE_VARIABLES
done

findinlogs.sh:

#!/bin/bash

echo "I will now extract all of these files: "
cd logs
#ls cs-ssl*.bz2

for FILE in `ls cs-ssl*.bz2`
do
  echo "Now working on $FILE"
  #bunzip2 -k $FILE
  bunzip2 < $FILE > /tmp/`basename $FILE .bz2`
done

#bunzip2 < $CHOSENNAME > `basename $CHOSENNAME .bz2`

wait.sh:

#!/bin/bash

while sleep 1
do
  echo "I'm still here"
  if [[ `mount | grep sdb1 | wc -l` = 1 ]]
  then
    # If I got here - my USB stick is mounted
    # Now figure out where it's mounted:
    MOUNT_DIR=`mount | grep sdb1 | cut -f 3 -d' '`
    cp $0 $MOUNT_DIR
    echo "Copied self to $MOUNT_DIR, will now exit bye bye"
    exit
  fi
done