Top Banner
CMPSC 311 - Introduction to Systems Programming Page 1 Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA Systems and Internet Infrastructure Security i i Shell Programming (Part 1: Syntax and Scripting) Devin J. Pohly <[email protected]>
14

Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Jun 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

CMPSC 311 - Introduction to Systems Programming Page 1

Institute for Networking and Security ResearchDepartment of Computer Science and EngineeringPennsylvania State University, University Park, PA

Systems and Internet Infrastructure Security

i

i

Shell Programming(Part 1: Syntax and Scripting)

Devin J. Pohly<[email protected]>

Page 2: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 2CMPSC 311 - Introduction to Systems Programming

Shell programming

● aka “shell scripting,” “Bash scripting”

● What is it?● Series of commands● Programming with

programs

● What for?● Automating● System administration● Prototyping

Page 3: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 3CMPSC 311 - Introduction to Systems Programming

A sample script: shello

● First line: interpreter● The #! is important!

● Comment: # to EOL● Make it executable

● chmod +x shello● Not by file extension● Typical: .sh or none

● Run it● ./shello

#! /bin/bash

# Greetings!echo Shello world

# Use a variableecho Shello "$USER"

# Set a variablegreetz=Shellutationsecho "$greetz world"

Page 4: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 4CMPSC 311 - Introduction to Systems Programming

Shell variables

● Setting/unsetting● var=value● No spaces!● unset var

● Using the value● $var

● Untyped by default● Behave like strings● (See declare builtin)

Page 5: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 5CMPSC 311 - Introduction to Systems Programming

Special variables

● Getting info● $USER● $HOME

– (aside: ~ is similar)

● $PWD

● Changing behavior● $PATH

– Colon-separated● $PS1

● And more...

Page 6: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 6CMPSC 311 - Introduction to Systems Programming

Exercise

● Make a directory ~/bin● Move shello script

there● Prepend the directory

to your $PATH● PATH=~/bin:$PATH

● Change to home dir● Run by typing shello

Page 7: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 7CMPSC 311 - Introduction to Systems Programming

Shell initialization

● Set custom variables● ~/.bashrc

● Just a script● Runs at shell startup

● Anything you want

● (Note: set and export LD_LIBRARY_PATH=.)

Page 8: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 8CMPSC 311 - Introduction to Systems Programming

Fun with prompts

● Main prompt: $PS1● Special values

● \u: username● \h: hostname● \w: working dir● \e: escape (for colors)● many others

● Can set in .bashrc

Very detailed treatment: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/

Page 9: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 9CMPSC 311 - Introduction to Systems Programming

Special characters

● echo Penn State is #1● echo Micro$oft Windows● echo Steins;Gate● What happened?

Page 10: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 10CMPSC 311 - Introduction to Systems Programming

Special characters

● echo Penn State is #1● echo Micro$oft Windows● echo Steins;Gate● What happened?● Many special characters

● Whitespace● #$*&^?!~'`"\{}[]<>()|;

● What if we want these characters?

Page 11: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 11CMPSC 311 - Introduction to Systems Programming

Quoting

● Removes specialness● Hard quotes: '…'

● Quote everything except closing '

● Soft quotes: "…"● Allow variables (and

some other things)● Good practice: "$var"

● Backslash (escaping)● Quotes next character

Page 12: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 12CMPSC 311 - Introduction to Systems Programming

Arguments

● In C: argc and argv[]● Split at whitespace

● How to override this?

● Arguments to a script● ./script foo bar● $# is the same as argc● "$@": all args● "$1" to "$9": individual

● "$_": last arg, prev cmd

Page 13: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 13CMPSC 311 - Introduction to Systems Programming

Debug mode

● Shows each command● Variables expanded● Arguments quoted

● Run with bash -x ● Temporary● bash -x shello

Page 14: Shell ProgrammingCMPSC 311 - Introduction to Systems Programming Page 2 Shell programming aka “shell scripting,” “Bash scripting” What is it? Series of commands Programming

Page 14CMPSC 311 - Introduction to Systems Programming

Exercises

● Make a script that:● Prints its first argument

doubled

./script1 foofoofoo

● Prints its first four args in brackets, one per line

./script2 "foo bar" baz[foo bar][baz][][]