Skip to content

Instantly share code, notes, and snippets.

@niftynei
Created April 13, 2016 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save niftynei/f9b94248d9251a816ac3e472c66c925d to your computer and use it in GitHub Desktop.
Save niftynei/f9b94248d9251a816ac3e472c66c925d to your computer and use it in GitHub Desktop.
intro to shell, a script

what is a shell?

Shell is a programming environment that terminal runs. it's a text based GUI for interacting with your system.

what is shell good for?

shell can give you information about your system, allows you to modify and run programs. let's try something

say "hello lisa" 
open http://recurse.com 

what is a command?

a command is a bash program, often referred to as a bash script. let's make a bash script really quickly.

first, let’s see that the say hello command does not exist yet.

sayhello

// this should fail with an error that command was not found Let’s make a sayhello command. Put the following in a file called sayhello

#!/bin/sh

say "hello lisa"

// Run this in the current directory (or try to). Fail and then show off source sayhello // Move this into your /usr/local/bin

now what happens if you run sayhello ?

sayhello

// this should fail with a new error: no permission to run. we can observe what permissions a file currently has by listing all the flles in a directory (ls)

ls -lah /usr/local/bin

// you should see alll the files that are in /usr/local/bin. scroll back up to wherever sayhello is.

notice that the beginning entry for sayhello is different than for any of the other entries. we need to change the letters to be the same as the ones. we’ll do this using chmod

first we need to figure out how to use it, so we’ll look at the man pages for it.

man chmod

// show off the MODES section briefly, explain verbally how the numbers add up to 7. // ask for someone in the audience to figure out how to get the correct numbers for sayhello

chmod 755 /usr/local/bin/sayhello

what are -lah for?

-lah are flags. let's see what happens when we only pass in a few of them...

// run ls -la , etc taking off letters as you go. explain the difference. ie: -h is human readable for the byte sizes. -l is for list. -a is for all (shows all files, including hidden ones) // show that you can run ls -l -a -h and that it’s the same as ls -lah

what commands are available in shell?

bash looks for executable files in certain directories; executable files in these directories become indexed and available as 'first level' commands. (i just made up that bit about being first level) bash is a programming environment; it stores the set of directories in a variable called PATH. we can see this PATH by printing it to stdout.

echo $PATH

we can see what all of the environment variables are by running printenv or env

small aside on how variables work:

HELLO="this is hello"
echo $HELLO; echo HELLO

where did all those other things in my path come from?

as an android developer, i wanted to be able to add the set of Android tools to my path. it's possible to configure these variables, and set new ones for your shell environment in a configuration file.

// show off .bashrc && explain difference between ,bashrc and .bash_profile // remove android utilities from path to show how that works, open a new terminal & explain why i have to open a new terminal

chain of command: pipes and stdout, briefly

// show off cat and pipe to wc. use pbcopy and pbpaste for this.

cat some.file | pbcopy # then paste it into a textedit page to show that it copied
pbpaste | wc -w # way to count words

// redirect some text into a file

pbpaste > new.file

// pretty good site for redirection explanation http://www.catonmat.net/blog/bash-one-liners-explained-part-three/

a little bit about processes

sometimes you want to run a long running process or command but still be able to do other things in the shell.

you can run shells inside shells, or fork off processes from a shell.

// run a static python site; navigate to site to show that it’s working

python3 -m http.server 8080

// run site in background (by adding a & to the end of it)

python3 -m http.server 8080 &

// show all jobs running with jobs terminate job with kill %ID // show off top and ps -ax

net cat : networking and redirection

src: http://osxdaily.com/2014/03/27/send-data-across-network-netcat/

now we’re going to have a little fun with networking and the shell!

let’s set up net cat to listen on a socket on the local network.

nc -k -l 2999 

Here’s my ip address:

ipconfig getifaddr en0

// open up another terminal and send data to your netcat instance.

cat sendthisdataover.txt | nc $IP_ADDRESS 2999

or

pbpaste | nc $IP_ADDRESS 2999

// encourage participants to send you some text!! 😄

some things we didn't talk about (too much)

moving, copying files grep find head, tail rsync, scp, ssh monitoring disk space or memory usage tips and tricks; shortcuts

Bonus round: PLAY BANDIT!

// walk thru the first level or two of bandit (showing off how to SSH into another shell!) http://overthewire.org/wargames/bandit/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment