+ - 0:00:00
Notes for current slide
Notes for next slide

Lots of answers to this

  • "double click" something
  • spotlight something (or magic ubuntu menu that's the same)
  • type it into a terminal

Shells, Environment, Scripting, and Bash

(in 80 minutes[!])

1 / 17

Q: How does a program start?

2 / 17

Lots of answers to this

  • "double click" something
  • spotlight something (or magic ubuntu menu that's the same)
  • type it into a terminal

Q: How does a program start?

The jobs of a shell

  • Spawn (launch) new programs
  • Handle input and output to programs
  • Kill and clean up old programs
3 / 17

Lots of answers to this

  • "double click" something
  • spotlight something (or magic ubuntu menu that's the same)
  • type it into a terminal

Q: How does a program start?

The jobs of a shell

  • Spawn (launch) new programs
  • Handle input and output to programs
  • Kill and clean up old programs

What shells have you used?

4 / 17

Lots of answers to this

  • "double click" something
  • spotlight something (or magic ubuntu menu that's the same)
  • type it into a terminal
  • All the text ones: bash, csh, zsh, fish, (I'm sure there are more)
  • KEY POINT: The desktop is also a "shell", it does all these jobs!
    Indeed, the desktop in Gnome is called the "unity shell"

Let's poke around how the [Desktop] shell works

$ cp /usr/share/applications/firefox.desktop ~/Desktop/
$ chmod +x ~/Desktop/firefox.desktop
5 / 17

Let's poke around how the [Desktop] shell works

$ cp /usr/share/applications/firefox.desktop ~/Desktop/
$ chmod +x ~/Desktop/firefox.desktop

What makes firefox.desktop work?

6 / 17
  • Change "Name=..."
    • What's the point of Name[ar] and friends?
    • What might "Keywords=..." do? --> click ubuntu icon in top corner, try typing each keyword, what's first? --> can't change this as easily, doesn't read from files on ~/Desktop/
    • What about this "Exec=..." --> Exec=gnome-calculator

Let's poke around how the [Desktop] shell works

$ cp /usr/share/applications/firefox.desktop ~/Desktop/
$ chmod +x ~/Desktop/firefox.desktop

What makes firefox.desktop work?

How does the [desktop] shell:

  • Spawn (launch) new programs
  • Handle input and output to programs
  • Kill and clean up old programs
7 / 17
  • Change "Name=..."
    • What's the point of Name[ar] and friends?
    • What might "Keywords=..." do? --> click ubuntu icon in top corner, try typing each keyword, what's first? --> can't change this as easily, doesn't read from files on ~/Desktop/
    • What about this "Exec=..." --> Exec=gnome-calculator

Let's poke around how the [bash] shell works

$ firefox
<Ctrl-C>
$ firefox &
$ jobs
$ fg
<Ctrl-Z>
$ bg
$ echo "hello" > test
$ cat test
$ true && echo "hello"
$ false && echo "nope" || echo "whaaaat?"
8 / 17

Let's poke around how the [bash] shell works

$ firefox
<Ctrl-C>
$ firefox &
$ jobs
$ fg
<Ctrl-Z>
$ bg
$ echo "hello" > test
$ cat test
$ true && echo "hello"
$ false && echo "nope" || echo "whaaaat?"

How does the [bash] shell:

  • Spawn (launch) new programs
  • Handle input and output to programs
  • Kill and clean up old programs
9 / 17

Where's firefox anyway?

$ firefox # This works
$ gcc hello.c -o hello # This works
$ hello # This doesn't
$ ./hello # This works

Your environment affects program behavior

  • Even shells! (they're a program too)
10 / 17

Changing the environment will change program behavior

  • In this case, how a shell performs the search for programs
    $ PATH=$PATH:/home/username/ # Assuming "hello" is in this folder
    $ hello # Now this works!
    $ PATH=/home/username # What if you'd done this instead?
  • Also saw a brief example of environment variables in last week's homework
11 / 17

Your programs can use the environment too

Exercise for your own time:

#include <stdio.h>
int main(int argc, char **argv, char **envp) {
printf("argc: %d\n", argc);
printf("envp[0]: %s\n", envp[0]);
// while (*envp++ != NULL) { // Try me too!
// printf("%s\n", *envp); // Don't uncomment that
// } // first printf... (why?)
}
$ ./a.out
$ HELLO=world ./a.out
$ lower=fine many=okaytoo ./a.out
$ export IamPermanent=ish
$ ./a.out
$ # Try uncommenting the while loop, did you find the missing ones?
$ ./a.out | less # This may explain some of the funny colors
12 / 17

Now what about scripting?

13 / 17

Now what about scripting?

Surprise! You've been scripting this whole time!

  • Typing commands into the bash shell and running a bash script are the same

    $ cat test.sh
    echo "hello" > test
    cat test
    true && echo "hello"
    false && echo "nope" || echo "whaaaat?"
    $ chmod +x test.sh # What is this doing?
    $ ./test.sh
  • How to write a bash script?

    • Try things out in the terminal
    • Copy things that work into a file
    • Run that file
    • Repeat
14 / 17

Bash is old...

But useful, especially for really short things

But has ugly and finicky syntax

  • VARIABLE=test != VARIABLE = test :(

But running programs is really easy

  • (it's what it was built for after all)
  • g++ -O3 -m32 thread.o libinterrupt.a test1.cpp -ldl -o test1
  • ./test1

But doing much more is tricky

  • Validate program output (diff?), what if it varies?
  • Rule of thumb: More than 50-100 lines, more than a shell script
15 / 17

Live Python exercises

git clone https://gitlab.eecs.umich.edu/c4cs/rpn

16 / 17

Anatomy of a Python script

  • Syntax

    • comments

    • if: def:
    • whitespace
  • Fundamental data types

    • numbers
    • strings
    • (), [], {}
  • Dynamic Typing

  • Walk through how it works -- add prints, show off raise, make mistakes

  • Improvements to make:

    • Division support o Show off help(operator)

    • Add except EOFError to main while loop (ctrl-d support)

Scripts vs modules

>>> import rpn_basic as rpn
>>> rpn.calculate("1 1 +")
2.0

Closing remarks

  • Try one of the Advanced Exercises

  • Reminder: You must submit to staff at OH

17 / 17

Q: How does a program start?

2 / 17

Lots of answers to this

  • "double click" something
  • spotlight something (or magic ubuntu menu that's the same)
  • type it into a terminal
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow