Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.

Similar presentations


Presentation on theme: "Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett."— Presentation transcript:

1 bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham)
CPTE 440 John Beckett

2 What is a Shell? 1960s 1970s -> Apps interacted with hardware using physical addresses and port numbers Command Interpreter for control & util fns Apps used OS APIs to communicate with hardware Command Interpreter for control & util fns DIR

3 The Unix Concept Small modules, each of which did one thing well
Command Interpreter (now called a shell) became a way to connect module operations together dir | grep “x” > xlist.txt Since the system was open and a shell functioned like any app, we had a battle of competing shells: sh, csh, ksh, tcsh, bash bash is the winner – it’s over!

4 What a Shell Does Allows you to interact with the computer’s operating system so that you can accomplish whatever you need to do: Launch programs Sequence actions Utility functions Utility functions are actually small apps in themselves Many utility functions now combined in busybox That was before UNIX! Some commands are actually built-in to bash, but these tend to deal with the command process.

5 Command Prompt Last character is traditionally “$” for user mode, “#” for root (supervisor) mode Common practice: To include the username and current working directory This can easily be re-programmed (see the book)

6 Packaging a Script Use a text editor like nano to create the file
Start it off right: #!/bin/bash Change permissions so it will execute For now: chmod 755 filename This allows any users on your system to use it. We’ll talk about more options later

7 Some Special Characters
Meaning beyond its literal meaning # This line is a comment Ls –al #A comment may come after a command Echo hello; echo there # Two commands on one line, spliced with a semicolon . myfile # Dot is an “include” operation while : # Colon evaluates as “true” ! Negates a logical value

8 Variables When a variable is being set, we don’t include $ at the beginning of the name When a variable is being used, we include the $ at the beginning of the name.

9 Arithmetic Let Double-parentheses let a=4+3
let “a = 4 + 3” # Double quotes add readability Double-parentheses

10 Fun with Directories Pushd Popd dirs

11 Eval

12 Internal Command: Where Am I?
If the prompt doesn’t tell you what directory you are currently logged into, just: $ pwd Meaning: “Print Working Directory”

13 What’s Up With That File?
touch filename Create a file if it does not already exist, and set the access dates to right now. ls -l filespec List the file(s) specified. -l means “long” – more info including size and permissions. We can also use –la to include “hidden” files (filenames beginning with a dot) stat filespec Extensive information about the file

14 Quoting Rule of thumb: “Enclose a string in single quotes unless it contains elements that you want the shell to interpret.” Text in double quotes is subject to shell expansion and substitution (e.g. if it has variable names beginning with $). You can also escape the dollar sign

15 How do I know if this is internal?
type command Tells you where this command comes from Note: In Windows command interpreter, type means to “type the contents of this file on the console.”

16 Writing to the Terminal Window
Simply use echo. Bear in mind that words after the command are treated as parameters. So they will lose multiple spaces. Or you can space things out with quotes.

17 Formatting Output Borrows from the C language library
First argument is format control string Subsequent arguments are info to be printed E.g. print a string followed by a number, using an internal variable named LINES: You can format decimals:

18 Saving Output The “>” character causes output from a command to go into a file. Use “>>” to append to a file. Special case for ls: the “-c” preserves columnar output if you pipe to a file. This is because the authors of ls thought that if you were sending to a file, you’d want to parse one filename at a time.

19 Cron Jobs To run a script automatically, add a reference in /etc/crontab To have your script output discarded, add this to the end of the /etc/crontab entry: >/dev/null 2>&1

20 Tail tricks tail without any parameters gives you the last 50 lines of a file. Often this is fine for finding logged errors. tail –f continuously monitors new lines coming into a file. Using this I can actually monitor clicks of students taking CPTE 100 tests, since the program uses AJAX to collect data! tail skips the first 2400 lines of a file

21 References BASH Programming – Introduction HOW-TO by Mike G Advanced Bash-Scripting Guide by Mendel Cooper


Download ppt "Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett."

Similar presentations


Ads by Google