Advanced Scripting

Yay Scripting!

Overview

  • Getting Started
  • Shell Script
  • Shell Script Output
  • Explanation
    • Why we're doing this

Objectives

  • Being BOSS 
    • at scripting
  • Understanding
    • arrays
    • loops
    • if/fi
    • Git status shortie
  • Happy! 

Instructions to get started

1. cd ~/Desktop                                                       

 

2. mkdir scripts                                                  

 

3. cd scripts                                                           

 

4. touch git_checkoutStatus.sh                             

 

5. chmod a+rwx git_checkoutStatus.sh              

 

6. ls -l (or ll)

Instructions to get started (con't)

7. Open directory in atom (atom .)                                                       

8. open git_checkoutStatus.sh in atom                                                  

9. copy/paste the slack script into git_checkoutStatus.sh                                                           

10. save the file git_checkoutStatus.sh

 

Switch back over to iterm and make sure that you are still in ~/Desktop/scripts

Instructions to get started (con't)

11. ./git_checkoutStatus.sh                      

 

You have a working bash script,

but what does it do?

git_checkoutStatus.sh

#!/bin/bash

cd ~/galvanize/assessments/
dirArray=(*/)

 

echo " ">~/Desktop/scripts/Daily_Assessment_Status.txt
echo "======git status=====">>~/Desktop/scripts/Daily_Assessment_Status.txt
date >> ~/Desktop/scripts/Daily_Assessment_Status.txt
echo "===================">>~/Desktop/scripts/Daily_Assessment_Status.txt
echo " ">>~/Desktop/scripts/Daily_Assessment_Status.txt
echo " ">>~/Desktop/scripts/Daily_Assessment_Status.txt

git_checkoutStatus.sh

for ((i=0; i<${#dirArray[@]}; i++)); do

 

         <insert some stuff to do here>
 

done

  • dirArray is the array that we created at the top of the script
  • ${#dirArray[@]} is how you get array.length in bash.
  • Note that in bash you must use "do" and "done"

git_checkoutStatus.sh

  if test -d ${dirArray[$i]}/.git/
  then

                  <insert some stuff to do here>
  fi

  • test -d is a bash conditional for (does directory exist.
  • ${dirArray[$i]} is how we call our array at position index from our for loop
  • take note that in bash you must specify (then) once you establish your conditional and (fi) once your if statement is complete

git status -s

==== g48_js-accumulator-assessment/ ===
 M README.md
 M src/every.js
?? nodehelp.js
==========================

       echo '====' ${dirArray[$i]} '==='>>~/Desktop/scripts/Daily_Assessment_Status.txt
      cd ${dirArray[$i]}
      
git status -s>>~/Desktop/scripts/Daily_Assessment_Status.txt
       git log --stat origin/master..HEAD>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo '=========================='>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo ' '>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo ' '>>~/Desktop/scripts/Daily_Assessment_Status.txt
     cd ~/galvanize/assessments/

  • Flags to modify the commands:
    • -s  The Dragnet version (“just the facts, m’am.”)
      • EXAMPLE:
      • yourname:galvanize-sports/ (master✗) $ git status -s  [13:26:25]
        MM    galvanize_sports.js
        M     test/test.js
    • -sb  Dragnet version but with paths included

 

Understanding git status -s

  ?  ?  wingding.md

  MM README.md

    M README.md

  • the first column (“X”) corresponds to the staging area (things that have been created or altered and then “git added”
  • The second is for the working directory

    • ' ' = unmodified

    • M = modified

    • A = added, D = deleted, R = renamed, C = copied, U = updated but unmerged, !! = ignored files

Understanding git status -s

  ?  ?   wingding.md

  MM README.md

     M  README.md

git status log

==== g48_big-o-assessment/ ===
commit 15ca9bc205896149e480c181777e15352cc96513

.......

==========================

       echo '====' ${dirArray[$i]} '==='>>~/Desktop/scripts/Daily_Assessment_Status.txt
      cd ${dirArray[$i]}
       git status -s>>~/Desktop/scripts/Daily_Assessment_Status.txt

       git log --stat origin/master..HEAD>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo '=========================='>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo ' '>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo ' '>>~/Desktop/scripts/Daily_Assessment_Status.txt
     cd ~/galvanize/assessments/

Now lets look at that OUTPUT file.

Daily_Assessment_Status.txt

Daily_Assessment_Status.txt

echo " ">~/Desktop/scripts/Daily_Assessment_Status.txt
echo "======git status=====">>~/Desktop/scripts/Daily_Assessment_Status.txt
date >> ~/Desktop/scripts/Daily_Assessment_Status.txt
echo "=====================">>~/Desktop/scripts/Daily_Assessment_Status.txt
echo " ">>~/Desktop/scripts/Daily_Assessment_Status.txt
echo " ">>~/Desktop/scripts/Daily_Assessment_Status.txt

 ---space
======git status=====
Thu Feb 23 20:53:46 PST 2017
=====================

 ---space

 ---space

Becomes

---->

Daily_Assessment_Status.txt

       echo '====' ${dirArray[$i]} '==='>>~/Desktop/scripts/Daily_Assessment_Status.txt
      cd ${dirArray[$i]}
       git status -s>>~/Desktop/scripts/Daily_Assessment_Status.txt
       git log --stat origin/master..HEAD>>~/Desktop/scripts/Daily_Assessment_Status.txt

       echo '=========================='>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo ' '>>~/Desktop/scripts/Daily_Assessment_Status.txt
       echo ' '>>~/Desktop/scripts/Daily_Assessment_Status.txt
     cd ~/galvanize/assessments/

==== g48_js-accumulator-assessment/ ===
 M README.md
 M src/every.js
?? nodehelp.js
==========================

Output Summarized

  • Readability
    • Elegant headers
  • Shows your git commit history
    • Did you not fork something before you cloned it?
  • Easily moddable
  • Scripting best practices
    • Use certain characters
    • Proper spacing and indentation
      • Remember that echo " "?

 

 

Be like this guy^

Why we care and so should you

Hint: it makes your life easier

  • Automate terminal actions
  • The Real World doesn't like GUIs
    • Remember VIM?
  • Reduce Human error
  • Monitor systems
    • Monitor parameters unavailable with third party software
  • Do stuff asleep!
  • THE CLOUD

Reading into it

Made with Slides.com