More Shell Programming Software Tools. Slide 2 Keyword Shell Variables l The shell sets keyword shell variables. You can use (and change) them. HOME The.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

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.
Perl Process Management Software Tools. Slide 2 system Perl programs can execute shell commands (Bourne shell) using the system function. system("date");
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.
Perl Process Management Learning Objectives: 1. To learn the different Perl’s commands for invoking system process 2. To learn how to perform process management.
The UNIX Shell Software Tools. Slide 2 Basic Shell Syntax command [-[options]] [arg] [arg] … l The name of the command is first l Options are normally.
UNIX chapter 04 UNIX Shells Mr. Mohammad Smirat. Introduction The shell is the software that listens to commands typed in at the terminal and translates.
UNIX Utilities Software Tools. Slide 2 Getting Started on UNIX The machines in CS Lab2 are named csl2wk01 through csl2wk41. csl2wk01 means “CSLab2, workstation#1”
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.
More Shell Programming Learning Objectives: 1. To learn the usage of environment (shell) variables in shell programming 2. To understand the handling of.
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.
UNIX Files and Security Software Tools. Slide 2 File Systems l What is a file system? A means of organizing information on the computer. A file system.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
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 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.
An Introduction to Unix Shell Scripting
Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
CS465 - UNIX The Bourne Shell.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
ICS 431 – Operating System. a command-line interpreter. a program that interprets commands and acts as an intermediary between the user and the inner.
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.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Control Statements and More
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
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
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
Shell Control Structures
The UNIX Shell Learning Objectives:
System Programming and administration CS 308
Part 1: Basic Commands/Utilities
Shell Scripting March 1st, 2004 Class Meeting 7.
LINUX System : Lecture 5 (English-Only Lecture)
Shell Script Assignment 1.
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Shell Programming.
Shell Control Structures
Shell Control Structures
More Shell Programming
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

More Shell Programming Software Tools

Slide 2 Keyword Shell Variables l The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where the shell looks for executables USER Your login name SHELL The name of the shell you are running PWD The current working directory PRINTER Can be loaded with your default printer

Slide 3 Keyword Example $ cat env1 #!/bin/sh echo "Hi $USER!" echo "Your home is: $HOME" echo "Your path is: $PATH" echo "Your current directory is: $PWD" echo "Your shell is: $SHELL" echo "Your printer is: $PRINTER" $ env1 Hi horner! Your home is: /homes/horner Your path is:/usr/bin:.:.:/homes/horner/Unix/bin:... Your current directory is: /homes/horner/111 Your shell is: /bin/tcsh Your printer is:

Slide 4 Readonly Shell Variables l $0 is the name the user typed to invoke the shell script: $ cat print1 #!/bin/sh echo "This script is called $0" $ print1 This script is called print1 $./print1 This script is called./print1 $ ~/111/print1 This script is called /homes/horner/111/print1

Slide 5 Command Line Arguments The command line arguments that you call a script with are stored in variables $1, $2,..., $9. $ cat args1 #!/bin/sh echo "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9" $ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.

Slide 6 Command Line Argument Example l How to write a command to swap two files? $ cat swap #!/bin/sh mv $1 /tmp/$1 mv $2 $1 mv /tmp/$1 $2 $ cat it1 contents of file1 $ cat it2 contents of file2 $ swap it1 it2 $ cat it1 contents of file2 $ cat it2 contents of file1 $

Slide 7 Command Line Arguments $* lists all the command line args: $ cat args2 #!/bin/sh echo "The args are $*" $ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 $# contains the number of args: $ cat args3 #!/bin/sh echo "The number of args is $#" $ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The number of args is 10

Slide 8 shift The shift command promotes each command line argument by one (e.g., the value in $2 moves to $1, $3 moves to $2, etc.) $ cat shiftargs #!/bin/sh echo "The args are 0 = $0, 1 = $1, 2 = $2" shift echo "The args are 0 = $0, 1 = $1, 2 = $2" shift echo "The args are 0 = $0, 1 = $1, 2 = $2" shift $ shiftargs arg1 arg2 arg3 The args are 0 = shiftarg, 1 = arg1, 2 = arg2 The args are 0 = shiftarg, 1 = arg2, 2 = arg3 The args are 0 = shiftarg, 1 = arg3, 2 = The previous $1 becomes inaccessible

Slide 9 shift Example l How to write a general version of the swap command for two or more files? swap f1 f2 f3... fn_1 fn f1 <--- f2 f2 <--- f3 f3 <--- f4... fn_1 <--- fn fn <--- f1

Slide 10 shift Example $ cat swap1 #!/bin/sh orig1=$1 mv $1 /tmp/$1 while [ $2 ] do mv $2 $1 shift done mv /tmp/$orig1 $1 $ cat it1 it2 it3 contents of file1 contents of file2 contents of file3 $ swap1 it1 it2 it3 $ cat it1 it2 it3 contents of file2 contents of file3 contents of file1

Slide 11 shift Example $ swap1 it1 it2 it3 $ cat it1 it2 it3 contents of file3 contents of file1 contents of file2 $ swap1 it1 it2 it3 $ cat it1 it2 it3 contents of file1 contents of file2 contents of file3 $ swap1 it1 it2 $ cat it1 it2 contents of file2 contents of file1

Slide 12 set The set command sets the args: $ cat set1 #!/bin/sh set yat yih saam echo "One is $1, two is $2, three is $3" $ set1 One is yat, two is yih, three is saam The set command is useful for moving the output of command substitution into the args: $ date Thu Sept 22 17:06:27 HKT 2006 $ cat day #!/bin/sh set `date` echo "Today is $3 $2 $6" $ day Today is 22 Sept 2006

Slide 13 $$ $$ is the process ID (PID) of the current process (the shell script PID, or the shell PID if interactive). $ cat pid #!/bin/sh echo $$ $ pid 1154 $ pid 1156 $ pid 1157 $ echo $$ 892 $ ps PID TTY TIME CMD 892 pts/0 0:01 csh

Slide 14 $$ l It can be used for temporary file names: $ cat swap2 #!/bin/sh file=/tmp/tmp$$ mv $1 $file mv $2 $1 mv $file $2 $ cat it1 it2 contents of file1 contents of file2 $ swap2 it1 it2 $ cat it1 it2 contents of file2 contents of file1 $

Slide 15 for The for statement executes a loop once for each of a list of possibilities: $ cat printall #!/bin/sh for file in * do if [ -f $file ] then echo "Print $file [y/n]? " read resp if [ $resp = "y" ] then lpr –Pcll2a $file fi done $ printall Print it1 [y/n]? y Print it2 [y/n]? n

Slide 16 for If the “ in ___ ” part is omitted, it defaults to $* : $ cat p #!/bin/sh for file do if [ -f $file ] then lpr –Pcll2a $file fi done $ p it1 it2 it3 $

Slide 17 for Example This program will do a recursive listing of all directories. The first argument is the directory to start from. An optional second argument controls the number of leading “:” characters. $ cat ldrec #!/bin/sh cd $1 if [ $2 ] then space=$2 else space=0 fi space1=`expr $space + 1`

Slide 18 for Example for f in * do if [ -d $f ] then i=1 while [ $i -le $space ] do echo –n ":" i=`expr $i + 1` done echo $f /homes/horner/111/ldrec $1/$f $space1 fi done The -n in echo will prevent a newline from printing.

Slide 19 for Example $ ls -F letter1 secret/ secret1/ secret2/ secret3/ secret4/ $ ldrec /homes/horner/111 secret :verysecret ::veryverysecret secret1 /homes/horner/111/ldrec: /homes/horner/111/secret1: permission denied secret2 /homes/horner/111/ldrec: /homes/horner/111/secret2: permission denied secret3 secret4 secretslink :verysecret ::veryverysecret $