1 of 38

Introducion to Shell Scripting

By AECC-UPRRP

2 of 38

Get down & dirty with the Terminal

  • Everyday use shell commands and their use:
    • man: the most important command. Displays a manual telling you information about what a command does and how to use it e.g. “man ls”
    • pwd: print working directory (tells you where you are in the filesystem path)
    • ls: list files and directories in current directory
    • cd: change directory. Changes the current directory to the one given as an argument
    • cat: used to read files sequentially i.e. displays the contents of a file, or concatenate files
    • less/more: allows you to scroll through long files
    • mv: used to move files around the file system. Also used to rename files
    • rm: used to remove files (or directories if used with proper arguments)
    • clear: clear the terminal screen
    • cp: copies a given file to a given path
    • history: displays the history of the commands a user has run in a shell

3 of 38

That’s All!

Thanks for Coming

Now you can be this guy too!

4 of 38

5 of 38

THE SHELL

  • Command line interface for running commands and scripts to control an OS.
  • Usually interacted with via a terminal emulator(use of a shell in a gui) or command line

6 of 38

Usual file system structure and usage:

7 of 38

Table of Contents

  1. Permissions & Execution
  2. stdin, stdout, stderr, stdwtf??
  3. Parsing
  4. Path
  5. There’s more than 1 shell!?
  6. Scripting

8 of 38

Permissions:

  • File ownership (user : group)
  • File permissions (octal, rwx)

  • check: ls -l
  • change: chown, chmod

9 of 38

10 of 38

11 of 38

Shebang ( #! ) :

  • Tells the OS (*nix) what program to use to run the program
  • Program needs to be executable
  • Arguments can be used
  • Portability matters:
  • #! /usr/bin/env <program_on_path>

12 of 38

Standard streams

  • Data Stream: ordered sequence of bytes read until EOF

stdin

stdout

stderr

13 of 38

stdin ( ) :

  • Stream data (often text) going into a program
  • Usually expected from keyboard

14 of 38

stdout ( ) :

  • Stream where a program writes its output data
  • Usually output to the terminal or terminal emulator

15 of 38

stderr ( ) :

  • Another output stream typically used by programs to output error messages or diagnostics
  • Usually output to the terminal or terminal emulator

16 of 38

These can all be redirected!

< > << >> |

17 of 38

Piping and Redirecting

Piping:

  • Piping “pipes” the output of the first command into the input of the next command
  • Example:
  • `wget -q -O - http://ccom.uprrp.edu/2016/wordpress/ | grep -o "computer" | wc -l`

Redirecting:

  • Redirects the output of a command into a file or redirect the input from a file
  • Example: `curl icanhazip.com > publicIP.txt`
  • Example: `grep -E "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" < publicIP.txt`
  • Explained: https://regex101.com/r/w3OJvc/1
  • Combining both: `last | cut -d ' ' -f1 | uniq -u > users.txt`
  • All input and output in the usual *nix shells are text streams
  • Most commands take input from from standard input which usually comes from a keyboard
  • Most commands output to standard output which defaults to outputting to the display

* we are defining these concepts in the context of bash

18 of 38

19 of 38

20 of 38

$PATH

$PATH: an enviromental variable. Contains the directories where executables are stored

  • Enviromental Variable:
  • Type of variables that control the behaviour and information on the shell

Check Enviromental Vars: `env`

Check $PATH: `echo $PATH`

21 of 38

$PATH cont.

  • $PATH per user
  • Modify $PATH:
  • For every login: in .bashrc or .bash_profile:

`export PATH=$PATH:<directory>`

  • For current session:

`PATH=$PATH:<directory>`

22 of 38

Parsing

  • Extracting useful information from some text or string of symbols
  • Usually done by describing the text’s format or some pattern in it

Side note:�Extremely important for compilers

23 of 38

24 of 38

Regular expressions (regex)

  • Text strings that describe patterns to find text or positions within a body of text
  • Examples:
    • [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}
    • <([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>

25 of 38

Regular expressions (regex) cont.

  • Good tutorial site:
  • https://regexone.com
  • Good for testing your regexes out:
  • https://regex101.com

26 of 38

27 of 38

Variety of Shells

28 of 38

Shells :

  • bash
  • tcsh
  • ksh
  • zsh
  • fish
  • powershell*

*: Not *nix. Similar-ish, but EVERYTHING is an Object (I/O).

29 of 38

Scripting:

What’s the point?

  • automation
  • eliminate repetitive tasks
  • some companies care
  • “unlimited” power
  • system administration
  • backup
  • mail
  • security monitoring
  • parsing / data processing
  • logging

30 of 38

Scripting cont. :

Choose your favorite text editor. It’s probably better if you’re comfortable with a shell text editor such as vi/vim, nano, emacs (we’ll cover this more in depth in another workshop :) )

  • Remember to check permissions and make your script executable
  • We will be talking about bash from now on

31 of 38

Bash...tricky sintax:

  • Variables:
  • var=<value>
  • var=$((<math>))
  • var=$<other_var>
  • let var=<math>
  • var=$(<command>)
  • var=`<command>`
  • User input:
  • read var
  • Access return value:
  • $?
  • Access parameters:
  • $0,$1,$2,…,$n
  • Functions:
  • function f {}
  • f()
  • Number of arguments:
  • $#

B

32 of 38

Bash...tricky sintax pt 2:

Conditionals:

if [ <condition> ]; then

<stuff>

elif (( <arithmetic condition> )); then

<stuff>

else

<stuff>

fi

** use [ ] or test

A

33 of 38

Bash...tricky sintax pt 3:

  • Comparison operators:
  • -eq, -ne
  • -lt, -gt, -ge, -le
  • -z, -n (null)

- Arithmetic operators:

  • python style
  • let, or ((<math>))

  • Bitwise operators
  • C style

  • Misc. :
  • -e
  • -f
  • -d
  • -h
  • -r
  • -w
  • -x

- Logical operators:

  • C style

S

34 of 38

Bash...tricky sintax pt 4:

  • For loop:

for <iterations>

do

<stuff>

done

** can be:

in {i..n}

in <dir> or in <command_out>

C style

  • While loop:

while <condition>

do

<stuff>

done

H

35 of 38

Workshop :

Go to: http://bit.ly/aecc-shell

  • Can you guess what the script does? Each part?
  • Is this useful to you?
  • Do you have an idea for a script we can help you write?

36 of 38

Helpful list of more advanced shell commands:

  • ssh
  • grep
  • rsync
  • chmod
  • find
  • tail / head / tac
  • ps
  • kill
  • awk
  • eval
  • alias
  • ln
  • !! (sudo !!)
  • mount
  • dd
  • whoami / who / last
  • reset
  • chroot
  • history
  • curl
  • uniq
  • sort
  • readlink
  • top
  • tar
  • strings
  • time
  • date
  • which
  • env
  • sed
  • ifconfig
  • shasum
  • nmap
  • traceroute
  • chown

37 of 38

AECC’s Favorite/Most used Commands or packages and favorite shell:

  • Alejandro (shell: zsh):
  • atool (aunpack)
  • curl
  • say
  • nmap
  • open
  • José (shell: fish):
  • find
  • grep
  • rename
  • youtube-dl
  • open
  • Israel (shell: zsh):
  • su
  • tar
  • ssh
  • John Wilson
  • aircrack-ng

38 of 38

Some more resources: