0% found this document useful (0 votes)
117 views14 pages

Shell Programming: (Part 1: Syntax and Scripting)

This document is a lecture on shell programming and scripting. It introduces shell scripting and its uses for automating tasks and system administration. It covers writing simple shell scripts, using variables, special variables, arguments, quoting, debugging scripts, and exercises for students to practice. The document is intended to teach the syntax and basics of shell programming.

Uploaded by

prnjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views14 pages

Shell Programming: (Part 1: Syntax and Scripting)

This document is a lecture on shell programming and scripting. It introduces shell scripting and its uses for automating tasks and system administration. It covers writing simple shell scripts, using variables, special variables, arguments, quoting, debugging scripts, and exercises for students to practice. The document is intended to teach the syntax and basics of shell programming.

Uploaded by

prnjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

i Systems and Internet

i Infrastructure Security
Institute for Networking and Security Research
Department of Computer Science and Engineering
Pennsylvania State University, University Park, PA

Shell Programming
(Part 1: Syntax and Scripting)

Devin J. Pohly
<[email protected]>

CMPSC 311 - Introduction to Systems Programming Page 1


Shell programming
● aka “shell scripting,”
“Bash scripting”
● What is it?
● Series of commands
● Programming with
programs
● What for?
● Automating
● System administration
● Prototyping
CMPSC 311 - Introduction to Systems Programming Page 2
A sample script: shello
● First line: interpreter #! /bin/bash
● The #! is important! # Greetings!
● Comment: # to EOL echo Shello world
● Make it executable # Use a variable
echo Shello "$USER"
● chmod +x shello
● Not by file extension # Set a variable
greetz=Shellutations
● Typical: .sh or none echo "$greetz world"
● Run it
● ./shello

CMPSC 311 - Introduction to Systems Programming Page 3


Shell variables
● Setting/unsetting
● var=value
● No spaces!
● unset var
● Using the value
● $var
● Untyped by default
● Behave like strings
● (See declare builtin)

CMPSC 311 - Introduction to Systems Programming Page 4


Special variables
● Getting info
● $USER
● $HOME
– (aside: ~ is similar)
● $PWD
● Changing behavior
● $PATH
– Colon-separated
● $PS1
● And more...
CMPSC 311 - Introduction to Systems Programming Page 5
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

CMPSC 311 - Introduction to Systems Programming Page 6


Shell initialization
● Set custom variables
● ~/.bashrc
● Just a script
● Runs at shell startup
● Anything you want

● (Note: set and export


LD_LIBRARY_PATH=.)

CMPSC 311 - Introduction to Systems Programming Page 7


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/


CMPSC 311 - Introduction to Systems Programming Page 8
Special characters
● echo Penn State is #1
● echo Micro$oft Windows
● echo Steins;Gate
● What happened?

CMPSC 311 - Introduction to Systems Programming Page 9


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?
CMPSC 311 - Introduction to Systems Programming Page 10
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
CMPSC 311 - Introduction to Systems Programming Page 11
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

CMPSC 311 - Introduction to Systems Programming Page 12


Debug mode
● Shows each command
● Variables expanded
● Arguments quoted
● Run with bash -x
● Temporary
● bash -x shello

CMPSC 311 - Introduction to Systems Programming Page 13


Exercises
● Make a script that:
● Prints its first argument
doubled
./script1 foo
foofoo
● Prints its first four args
in brackets, one per line
./script2 "foo bar" baz
[foo bar]
[baz]
[]
[]

CMPSC 311 - Introduction to Systems Programming Page 14

You might also like