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

Slides:



Advertisements
Similar presentations
The Unix File System. What are the three parts of every file on a Unix filesystem? And where is each stored? Filename - stored in directories Inode -
Advertisements

More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Linux+ Guide to Linux Certification, Second Edition
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS 497C – Introduction to UNIX Lecture 4: Understanding the UNIX Command Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Shell Script Examples.
Linux Commands LINUX COMMANDS.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
UNIX command line. In this module you will learn: What is the computer shell What is the command line interface (or Terminal) What is the filesystem tree.
Introduction to Shell Script Programming
Writing Shell Scripts ─ part 1 CSE 2031 Fall September 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux Operations and Administration
Scripting Languages Course 2 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CS465 - UNIX The Bourne Shell.
Beyond sh Not everyone is as fond of UNIX as most other people. The tutorial talks about the dark side of UNIX.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
 Creating and manipulating text has long been one of the primary tasks of scripting languages and  traditional shells. In fact, Perl (the language)
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Introduction to Programming Using C An Introduction to Operating Systems.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.
Tutorial of Unix Command & shell scriptS 5027
CIRC Winter Boot Camp 2017 Baowei Liu
Introduction to Shells
Writing Shell Scripts ─ part 1
Prepared by: Eng. Maryam Adel Abdel-Hady
Introduction to Python
System Programming and administration CS 308
Some Linux Commands.
Shell Scripting March 1st, 2004 Class Meeting 7.
The Linux Operating System
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
INTRODUCTION TO UNIX: The Shell Command Interface
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
What is Bash Shell Scripting?
LING 408/508: Computational Techniques for Linguists
John Carelli, Instructor Kutztown University
Tutorial of Unix Command & shell scriptS 5027
CSE 303 Concepts and Tools for Software Development
Lab 5: Complex Inputs.
CST8177 Scripting 2: What?.
Chapter 3 The UNIX Shells
Linux Commands LINUX COMMANDS.
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

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

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

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!

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.

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)

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

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

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.

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

Fun with Directories Pushd Popd dirs

Eval

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”

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

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

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.”

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.

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:

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.

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

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 +2400 skips the first 2400 lines of a file

References BASH Programming – Introduction HOW-TO by Mike G http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc5 Advanced Bash-Scripting Guide by Mendel Cooper http://www.tldp.org/LDP/abs/html/index.html