Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.

Slides:



Advertisements
Similar presentations
Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
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.
Further Shell Scripting Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
UNIX Shell Scripting > Echo “A quick how-to”. What is shell scripting? Interpreted (non-compiled) language Slower compared to compiled languages Much.
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.
Lab 8 Shell Script Reference:
Shell Control Structures CSE 2031 Fall August 2015.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
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.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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.
#!/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)
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths Corporate Information and Computing Services The University.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell Programming Features “Full” programming language “Full” programming language Conditional statements Conditional statements Arithmetic, String, File,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
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]
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
#!/bin/sh 2002/07/26 Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a “Shell”? OSuser Shell.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
 Prepared by: Eng. Maryam Adel Abdel-Hady
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Shell Scripting 10 Second Guide.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Pepper (Help from Dr. Robert Siegfried)
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
Command Substitution Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the.
Presented by, Mr. Satish Pise
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Bash Scripting CS 580U - Fall 2018.
Presented by, Mr. Satish Pise
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Shells

Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE

comment #this is a comment #this just prints to screen echo hello mum save this as hello.sh change permissions run as sh hello.sh

utilities and commands a utility is a program, a command is an instruction uname -a > outputfile.txt (will display vital statistics on the system.)

Five different types of command simple command pipeline list command function definition compound command

Grouping Commands (echo start ; cat hello.sh ; echo end) > new.txt adds start and end to hello.sh command1 || (command2 && command3)

Exit Status 0 if previous command successful non zero if previous command not successful chmod u-w testfile cat > testfile echo $? 1

basic calculator bc /3 scale=5 (5 decimal places) sqrt(2)

operations + addition - subtraction * multiplication % integer remainder ^ power

Functions sqrt length scale (need option -l for library) s, sine c, cosine a, inverse tanget e, exponetial l, natural logarithm. echo "1+2" | bc echo 2/3 | bc –l (-l is better!!!!)

The Test Statement two ways test arguments [arguments] [ -e ouput.txt ] && echo file exists if the file exists, echo the message [ -e ouput.txt ] || echo file not exist if the file does not exist, echo the message

If…file exists test -e ouput.txt && echo file exists if the file exists, echo the message test -e ouput.txt || echo file not exist if the file does not exist, echo the message

File operators used by test -d, if exists and is directory -e, if exists -f, if exists and is regualar file -r, if exists and is readable -s, if exists and has non-zero size -w, if exists and is writable -x, if exists and is executable

String operators used by test -n, if string has non-zero length -x, if string has zero length string, if string is not null s1=s2, if strings are equal s1!=s2, if strings are different.

Example – NAME not set $ [ "$NAME" ] || echo NAME not set NAME not set $ [ $NAME ] || echo NAME not set NAME not set

Arithmetic Operators used by test -eq, equal -ne, not equal -gt, greater than -ge, greater than or equal to -lt, less than -le, less than or equal to.

Example -eq and = 0 and 00 are numerically equal $ [ 0 -eq 00 ] && echo equal equal 0 and 00 are different strings. $ [ 0 = 00 ] && echo equal $

White Space is important # [ "0" -eq "00" ] && echo equal equal # [ "0" -eq "00"] && echo equal bash: [: missing `]'

IF statement if [ hell = hell ] then echo equal else echo not equal fi if [ hell1 = hell2 ] then echo equal else echo not equal fi

For Loops for name in values do commands done

For loop example for i in $( ls ) do [ -r $i ] && echo $i is readable done

While Loops while true do echo tick tock (this the sound of a clock) date sleep 1 done

Until Loops until command1 do command2 done