Difference between revisions of "OPS435 Lecture 5 - Bash"

From CDOT Wiki
Jump to: navigation, search
(Created page with '* Program control: ** while ** for *** seq command * Combining everything we learned so far')
 
m (Andrew moved page OPS435 Lecture 5 to OPS435 Lecture 5 - Bash)
 
(2 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
*** seq command
 
*** seq command
 
* Combining everything we learned so far
 
* Combining everything we learned so far
 +
* Scripts from [[OPS335_Weekly_Schedule | OPS335 Labs]]
 +
 +
= In-class examples =
 +
 +
forloop.sh:
 +
 +
<source lang="bash">
 +
#!/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
 +
</source>
 +
 +
findinlogs.sh:
 +
 +
<source lang="bash">
 +
#!/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`
 +
</source>
 +
 +
wait.sh:
 +
 +
<source lang="bash">
 +
#!/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
 +
</source>

Latest revision as of 13:40, 22 August 2017

  • 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