#!/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.

Slides:



Advertisements
Similar presentations
#!/bin/sh Jaeho Shin. Interface between user and the operating system sh, bash, csh, tcsh, ksh, … What is a Shell? OSuser Shell.
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.
Basics of Shell Programming. What’s Shell? It’s acts an interface between the user and OS (kernel).It’s known as “ command interpreter”. When you type.
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.
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
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.
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.
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.
Shell Script Examples.
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.
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 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
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.
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.
#!/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.
LINUX programming UNIT-2 1. INDEX UNIT-IV PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Working with Bourne shell L1 1-2.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
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.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
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
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
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]
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.
CS 350 Lecture 3 UNIX/Linux Shells by İlker Korkmaz and Kaya Oğuz.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
LING 408/508: Computational Techniques for Linguists
UNIX Reference Sheets CSE 2031 Fall 2010.
Presented by, Mr. Satish Pise
Chapter 5 The Bourne Shell
CSC 352– Unix Programming, Fall, 2011
Chapter 5 Bourne Shells Scripts
More Shell Programming
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

#!/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

Shell Script? A command programming language that executes commands read from a terminal or a file Uses built-in commands/syntax and OS programs Why use shell scripts? –Can do repetitive tasks easily & quickly

Basic Syntax # comments… list; list; list; … list | list | list … list && list || list … list out <>io list n>&n list & (list) { list;} name=value name () { list;} ${name} $(command) $((expr))

Expansion ${parameter} ${parameter:-word} ${parameter:=word} ${#parameter} ${parameter%word} ${parameter#word} $(command) or `command` $((arithmetic expr))

Flow control - if if list ; then list ; [ elif list ; then list ; ] [ else list ; ] fi #!/bin/sh myvar="myvalue" if [ "$myvar" = "" ]; then echo "nothing!" else echo "you’ve got $myvar" fi

Flow control - for for name [ in word … ] do list done #!/bin/sh for i in ; do echo "countdown: $i"; sleep 1 done

Flow control – while while list; do list done #!/bin/sh unit="x" while [ "$string" != "xxxxxx" ]; do string=$string$unit echo "not yet~ ($string)" done echo "ok~ got $string"

Flow control - case case word in [ pattern [ | pattern ] ) list ;; ] … esac #!/bin/sh case $1 in hi) echo "hello~";; bye) echo "byebye~";; *) echo “what?!”;; esac

Test expression – [ (1/2) Usage: [ expr ] expr1 -a expr2 : expr1 and expr2 expr1 -o expr2 : expr1 or expr2 ! expr : not expr -f path : path is a file -s path : file at path is larger than 0 -r path : path is readable

Test expression – [ (2/2) -w path : path is writeable -x path : path is executable str1 == str2 : str1 is equal to str2 str1 != str2 : str1 is not equal to str2 arg1 OP arg2 : OP is one of -eq, -ne, -lt, -gt, -le, -ge. Arithmetic binary operation. …

Predefined variables # : number of arguments ? : last command’s return value $ : process id of this shell ! : process id of last background command 1, 2, 3, … 9 : n-th argument …

Example script #!/bin/sh # shortcut script case $1 in a) telnet ara;; s) telnet ska;; l) telnet loco;; n) telnet noah;; m) mutt t) if [ -f $HOME/TODO ]; then cat $HOME/TODO | more fi;; esac

Pipelining Syntax : command1 | command2 Connecting one process(command1)’s output to another process(command2)’s input Can do complex jobs by combining many small programs Examples –ls -l | sort –finger | cut -d " " -f 3,4,5 | wc

Redirection Syntax : command |>|>> path Connecting process’s output/input to a given file Can save any output to a file and can load any file to a program input Examples –du -sh * > du_dump.file –find ~ -name.*rc > rcfiles –sort sorteddata –date >> mydatelogfile

Job control Job control is a way to do multitasking with the command-line interface Syntax and commands –command & –bg [%n] –fg [%n] –jobs –kill [%n]

References man pages for sh/ksh/bash/csh/tcsh