# reveal.js

## IEEE Bash Scripting

## What is Bash?
### Compiled vs Interpreted languagess | Compiler | Interpreter | | -------- | ----------- | | "Compiles" down a program into binary (machine code) <!-- .element: class="fragment fade-right" data-fragment-index="1" style="font-size:0.7em;" -->| Does not save machine langauge <!-- .element: class="fragment fade-left" data-fragment-index="1" style="font-size:0.7em;" -->| | Must first compile a program into machine code before running <!-- .element: class="fragment fade-right" data-fragment-index="2" style="font-size:0.7em;" -->| Interprets each line and executes each as it reads them <!-- .element: class="fragment fade-left" data-fragment-index="2" style="font-size:0.7em;" -->| | Tries to make use of the cpu it compiles a program for <!-- .element: class="fragment fade-right" data-fragment-index="3" style="font-size:0.7em;" -->| Usually less finetuned for a CPU <!-- .element: class="fragment fade-left" data-fragment-index="3" style="font-size:0.7em;" -->| | Usually favored in production <!-- .element: class="fragment fade-right" data-fragment-index="4" style="font-size:0.7em;" -->| Usually favored in development and programming environments <!-- .element: class="fragment fade-left" data-fragment-index="4" style="font-size:0.7em;" -->|
### Example Languages | Compiled | Interpreted | | -------- | ----------- | | C/C++ <!-- .element: class="fragment fade-right" data-fragment-index="1" -->| Python <!-- .element: class="fragment fade-left" data-fragment-index="1" --> | | Java <!-- .element: style="visibility:hidden;" -->| JavaScript <!-- .element: style="visibility:hidden;" -->| | Rust <!-- .element: style="visibility:hidden;" -->| PHP <!-- .element: style="visibility:hidden;" -->| | Haskell <!-- .element: style="visibility:hidden;" -->| Ruby <!-- .element: style="visibility:hidden;" -->| - Bash is a command language <!-- .element: style="visibility:hidden;"--> *interpreter* <!-- .element: style="visibility:hidden;"-->

#### Compiled vs Interpreted Example

    program.c    program

    #include <stdio.h>
      int main() {
      int a = 36;
      int b = 6;
      printf("The answer to life, the universe,\
      and everything: %d\n", a+b);
      return 0;
      }

    $ gcc program.c -o program

    program.py

    a = 36;
      b = 6;
      print("The answer to life, the universe,\
       and everything: {}".format(a+b))

    $ python everything.py
      The answer to life, the universe, and everything: 42

#### Compiled vs Interpreted Example

    program.c    program

    #include <stdio.h>
      int main() {
      int a = 36;
      int b = 6;
      printf("The answer to life, the universe,\
      and everything: %d\n", a+b);
      return 0;
      }

    $ gcc program.c -o program
    $ ./program
    We know the answer is 42, but what is the question?

    program.py

    a = 36;
      b = 6;
      print("The answer to life, the universe,\
       and everything: {}".format(a+b))

    $ python everything.py
      The answer to life, the universe, and everything: 42

## TODO: Show how there exists a python shell

| Compiled | Interpreted | | -------- | ----------- | | C/C++ | Python | | Java <!-- .element: class="fragment fade-right" data-fragment-index="0" -->| JavaScript <!-- .element: class="fragment fade-left" data-fragment-index="0" -->| | Rust <!-- .element: class="fragment fade-right" data-fragment-index="0" -->| PHP <!-- .element: class="fragment fade-left" data-fragment-index="0" -->| | Haskell <!-- .element: class="fragment fade-right" data-fragment-index="0" -->| Ruby <!-- .element: class="fragment fade-left" data-fragment-index="0" -->| - Bash is a <!-- .element: class="fragment fade-up" data-fragment-index="1" --> *command* language <!-- .element: class="fragment highlight-red" data-fragment-index="3" --> *interpreter* <!-- .element: class="fragment highlight-green" data-fragment-index="2" -->

## TODO: Entering commands into terminal example example

How programs can be called: firefox, steam, discord, grep

## TODO: Go list what are some commands line by line

cd ls echo/printf

## TODO: Go list what are some commands line by line CONTD.

cat, other commands we might use in the scripts

## TODO: Explain how command shells there are: zsh on mac, Powershell and Command Line Prompt on Windows, and Bash, then more alternatives

## POSIX IEEE 1003.1 (2017)

## TODO: Funny Yash shell

## Bash Logic Crash Course --- ## Variables ```bash # Implicitly defined answer="42" echo $answer # Or better # printf "%s\n" $answer ```

## Control Logic

If Statement:

    if COMMAND; then
      # Insert more commands here
    fi

## Control Logic

If Statement:

    if COMMAND; then
      # Insert more commands here
    fi

    if COMMAND; then
      # Insert more commands here
    else
      # Do otherwise
    fi

## Control Logic

If Statement:

    if COMMAND; then
      # Insert more commands here
    fi

    if COMMAND; then
      # Insert more commands here
    else
      # Do otherwise
    fi

    if [[ 2 -eq 2 ]]; then
      # Insert more commands here
    else
      # Do otherwise
    fi

For loops: ```bash for i in <item1> <item2> <item3>; do # Insert more commands here # $i holds the information fi ```

### TODO on these things

- Piping - If Statement: - && and || - For loops: - How the scripts actually work - functions, associative arrays, string substition, etc. - Look at a good cheatsheet

### TODO: Give good resources on bash cheatsheets: https://devhints.io/bash

### TODO: mild script example 1

### TODO: mild script example 2

### TODO: mild script example 3

### TODO: rclone

### TODO: httpd servers

### TODO: wget

### TODO: combining wget and http to mirror sites

### Get every Canvas file from your course!

Here's a script to download all files for a given Canvas course. You will need to get an API Token from Canvas for UCSD and the course ID. To get the course ID, take the ID from the course's URL. Go to your browser, open your course page, you will get a URL like this: https://canvas.ucsd.edu/courses/123456 The ID is the number at the end. Place it at the end of the variable COURSE_ID below with quotes. For the API token, click on your profile picture, then click "Settings". After that, create a "New Access Token" under the "Approved Integrations" section. Add a name and expiration date. You will get a token on the dialog after that looks something like this: "12345~..." Just put it down in the variable API_TOKEN below. This uses "jq" to filter the JSON received into a readable format for our script. '.[]' takes every element from the array and lists it line-by-line, while the second part creates a string containing the fields from the request, comma-delimited. Try running the command above on its own and experiment with different "jq" queries. There are cheat sheets online you can find, it's cool! For each line from the above query, we'll get the filename and the URL. Then, for all of them, we will download the file in the directory "files/" and just name it as-is on the page. The line variable looks something like this: "NAME OF FILE,URL TO DOWNLOAD FILE" Awk is a scripting language for text manipulation. You can use to quickly parse comma-delimited values, which is what I do below. Download the file and move it to the files directory. "-s" silences the progress bar that shows up with cURL, you can remove the "-s" to see it.

    #!/usr/bin/env bash

    COURSE_ID="123456"
    API_TOKEN="12345~..."

    mkdir -p files/

    FILES=$(
    	curl
    	-X GET
    	-H "Authorization: Bearer $API_TOKEN" \
    		https://canvas.ucsd.edu/api/v1/courses/$COURSE_ID/files |
    		jq -r '.[] | "\(.filename),\(.url)"'
    )

    echo "$FILES" | while IFS= read -r line; do
    	FILENAME=$(echo $line | awk -F',' '{print $1}')
    	URL=$(echo $line | awk -F',' '{print $2}')
    	echo "Downloading $FILENAME..."
    	curl -s "$URL" -o "$FILENAME"
    	mv "$FILENAME" "files/$FILENAME"
    done

    echo "Done! All your files are in the 'files/' directory."

### TODO: Plug in how bash is essential to supercomputing

### TODO: Plug in SCC Apps and also our website

### TODO: Thank you Slide

### WARNING: Slides after this are just for debugging and messing with layouts,css,js,etc.

Trying to braek the bounds of the perhaps margin on this thing. They contain the maximum size of content for slides.

## Example Script

    #!/bin/bash
    # Descr: Bash Script for Bash Workshop
    printf "Hi, how are you doing?\n"

    firefox

    wget -k -m -r https://ieeeucsd.org/

    #!/bin/bash
    # Descr: Bash Script for Bash Workshop
    printf "Hi, how are you doing?\n"

    firefox

    wget -k -m -r https://ieeeucsd.org/

## HStack Example

One

Two

Three

One

Two

Three