8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
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 Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
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 6: Shell Programming
An Introduction to Unix Shell Scripting
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Linux Operations and Administration
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
Shell Programming Any command or a sequence of UNIX commands stored in a text file is called a shell program. It is common to call this file a command.
CS465 - UNIX The Bourne Shell.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Shell Programming. An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
Writing Scripts Hadi Otrok COEN 346.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Linux Administration Working with the BASH Shell.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Bash Shell Scripting 10 Second Guide.
Department of Computer Engineering
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
System Programming and administration CS 308
CSC 352– Unix Programming, Fall 2012
The Linux Operating System
Shell Script Assignment 1.
CSE 303 Concepts and Tools for Software Development
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
LING 408/508: Computational Techniques for Linguists
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
CSC 352– Unix Programming, Fall, 2011
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

8 Shell Programming Mauro Jaskelioff

Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming –Permissions and making your file executable –Input to and Output from shell scripts –Control structures If then else For loops –Booleans - test –Controlling input from within the shell

Environment Variables Environment variables are pieces of information used by the shell and by other programs Useful for customising your working environment and for shell programming Some examples: –PATH - the directories the system searches to execute commands –TERM - The type of terminal (most commonly xterm and vt100) –HOME - Your home directory –PS1 – The format of the prompt

Using Environment Variables Conventionally written using all capitals To use, precede by a $ symbol E.g. to find the value of a variable –echo $VAR ~]$ echo $HOME /home/domain/zlizmj Variables are expanded by the shell before the command is executed

Using Environment Variables Environment variables can be used by any program public class EnvDemo1 { public static void main(String args[]) { String s = System.getenv(“PATH"); System.out.println(s); }

Assigning Environment Variables at the Prompt VAR=value The change is only visible in the current shell Child processes don’t automatically inherit the environment variables from their parent. We use export to let child processes get the changed value. export VAR=value To add something to your PATH: export PATH=$PATH:new stuff

Your PATH Variable Your PATH tells UNIX where to look for executables You will have a PATH already set up by default. –You can alter it or add to it When you type the name of a program, the shell will look for it in your PATH. Each directory on your PATH should be separated by a colon If you change your PATH in one window it is changed only in that window (since PATH is just an environment variable)

UNIX Command Line In UNIX you type commands at the keyboard and the system responds Every operating system has some sort of command interface In UNIX this is a separate program. There are different versions of this program to suit the taste of the user. We use bash (Bourne again shell) –… but it is possible to program in other shells and use your existing shell to interpret it In this lecture you will learn about Bourne shell scripting (sh), a predecessor of bash.

What is a Shell Script? The shell is a command interpreter –It reads commands and then executes them It can work interactively or from a text file A shell program is simply a text file which contains commands you would normally type at the prompt The only difference is that the commands are executed in a sub-shell (a child process is created).

Common Shell Script Components The first line (for bourne shell) is usually #!/bin/sh # is also used for comments Hence, this line is a special kind of comment: it tells the shell which program to use to execute the commands in the file

Writing and Running a Shell Script Create the text file (using Emacs) Make it executable (optional): chmod u+x filename Run it: –filename - only works if file is executable and your PATH is set correctly! –/Path/to/executable/filename if filename is executable../myscript.sh –sh filename if you haven’t made it executable –. filename or source filename if you want to execute it without creating a sub-shell

Making your File Executable ls -l tells you if files are readable, writable and/or executable and by whom You can change these permissions by using chmod ~]$ ls -l Foo -rw-r--r-- 1 zlizmj Domain U 0 Mar 12 10:23 Foo ~]$ chmod who?what filename

Chmod Revisited who is one of u (user), g (group) or o (other) –can also have a (all) ? is one of + (add a permission) or - (remove a permission) what is one of r (read permission), w (write permission) or x (execute permission)

Examples of chmod chmod o-r coursework –prevents others from reading your file chmod u+x shellscript –makes the file executable by you alone chmod a+x directory –makes the directory accessible by everyone (a = all  user, group and others) chmod +x directory –We can omit the a (it’s the default)

A Simple Shell Script Each command appears on a separate line #!/bin/sh ls echo "done " #This is a comment

The Simple Shell Script in Action 1]$ ls done.sh 1]$ chmod +x done.sh 1]$./done.sh done.sh done 1]$

Input and Output The first argument to a shell script is called $1 The second argument to a shell script is called $2 ….etc… Shell uses echo like Java’s println

Input and Output: An Example 1]$ cat firstarg.sh #!/bin/sh #Print first argument echo $1 1]$./firstarg.sh hello world hello

Control Structures Control structures are built in syntax for controlling the order in which execution happens Common structures are conditionals (if-then-else) and loops (for loops) Keywords should appear at the start of a line

Conditionals NOTE: the else is optional… if …. then …. else …. fi

Example Using Conditionals #!/bin/sh if javac $1 then echo "compilation done" else echo "compilation failed" fi

Booleans if needs something true or false Often this means you want to compare things This is more complicated in shell than in most languages Need to use test if test $1 -ge $2 –succeeds if the first argument is “greater than or equal to the second”

Some test inputs if test $1 = $2 –if $1 is equal to $2 (for strings) if test $1 -eq $2 –if $1 is equal to $2 (for numbers) -ge –(greater or equal) -gt –(greater) if test -f $FILE –if $FILE exists and is a normal file

More test inputs You don’t have to use “test” –if [$1 -ge $2] Is just syntactic sugar for –if test $1 -ge $2 To learn more about test: man test

For Loops NOTE: There are more complex forms of loop in Bash. for IDEN in list do …. done

A Simple For Loop Generally this script will look in the current directory: If you want it to look elsewhere, you need to put in the full path #!/usr/bin/sh # This is list_shell.sh for IDEN in *.sh do echo "$IDEN" done

The For Loop in Action $./list_shell.sh done.sh echo_three.sh edit_shell.sh list_shell.sh new.sh simple.sh

Your.bash_profile is a shell script # set up personal bin directories PATH=$HOME/bin:$PATH: EDITOR=emacs export PATH EDITOR

Variables and Shell You can use environment variables in shell scripts just like you can at the command line. –Environment variables are typically written in upper case and are accessed using a dollar symbol You might also want to use other variables in shell scripts –These are typically written in lower case and are accessed using a dollar symbol

Interacting with the User To get input use read followed by a variable $./interact.sh please type: hello computer #!/bin/sh # This is interact.sh echo "please type:" read ANS echo $ANS

Variables and read read can have more than one argument –e.g. read COMMAND ARGUMENTS It will bind the first word of input to the first variable and bind the rest to the second This acts like a list or array – so can be used with for

More Complex read Example #!/bin/sh # This is interact2.sh echo "please type:" read COMMAND ARGUMENTS for ARG in $ARGUMENTS do $COMMAND $ARG done

The Example in Action $./interact2.sh please type: cat interact.sh simple.sh #!/bin/sh echo "please type:" read ANS echo $ANS #!/bin/sh echo $1

Controlling Input from Within the Shell << tells a command to use input from within a shell script Syntax is command << end where end is some string which will tell the command to stop taking input (<< EOF is most common) This is useful when testing programs – you can automatically run them on sample input

Example of Input Control $./interact3.sh please type: hello goodbye hello #!/bin/sh # This is interact3.sh./interact2.sh <<EOF echo hello goodbye hello EOF

Summary Environment Variables Running Shell Programs Command Line Arguments If-then-else and for loops. Controlling Input and Output.