240-491 Adv. UNIX:CShell/71 Advanced UNIX v Objectives of these slides: –introduce the C Shell –concentrate on features not in the Bourne Shell (but many.

Slides:



Advertisements
Similar presentations
1 Advanced UNIX Objectives of these slides: Introduce the C Shell Concentrate on features not in the Bourne Shell (but many are in Bash) The C Shell.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Chapter 9: The TC Shell Everything you thought you knew is now wrong.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Find Command Characteristics –Locate files descending from multiple starting points –Employs regular expressions Examples On entire system: >find / -name.
Understanding bash Prof. Chris GauthierDickey COMP 2400, Fall 2008.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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.
Scripting Languages and C-Shell. What is a scripting language ? Script is a sequence of commands written as plain text and run by an interpreter (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 Scripts 4 A shell usually interprets a single line of input, but we can also create a file containing a number of lines of commands to be interpreted.
Introduction to UNIX GPS Processing and Analysis with GAMIT/GLOBK/TRACK T. Herring, R. King. M. Floyd – MIT UNAVCO, Boulder - July 8-12, 2013 Directory.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter Nine Advanced Shell Scripting1 System Programming Advanced Shell Scripting.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter 6: Shell Programming
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.
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter 4 UNIX Common Shells Commands By C. Shing ITEC Dept Radford University.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
1 Shell Scripting (C shell) SungHo Maeung 10/27/2000 Tutorial section Computer Science Lab.
LINUX System : Lecture 6 Shell Programming
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
1 Homework / Exam HW7 is due next class Starting Glass chapter 4 and parts of 7 Exam 3 – Class 26 –Open Book / Open Notes –Up through End of K&R Chapter.
CS465 - UNIX The Bourne Shell.
Shell Programming. 222 Lecture Overview  Shell variables  Shell scripts  Control flow and Boolean operators  Shell programming tips  Shell programming.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSCI 330 T HE UNIX S YSTEM C Shell. C S HELL SPECIFICS Startup and initialization Shell variables Prompt Alias Redirections and pipe 2 CSCI The.
Adv. UNIX: Shell/21 Advanced UNIX v Objectives –to supplement the “Introduction to UNIX” slides with extra information about the Shell
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
UNIX/LINUX Shells Glass & Ables ch. 5 A picture of the relationship between UNIX shells Common Core Bourne Shell Korn Shell Common Core C Shell T Shell.
June 1, 1999Customizing the UNIX Environment1 Introduction to UNIX G. Customizing the UNIX Environment.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
Customizing the Shell Environment. UNIX Shells Two characteristics of shells –Interactive: prompts ($) and waits for your response/requests –Noninteractive:
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
1 Writing Shell Scripts Professor Ching-Chi Hsu 1998 年 4 月.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Foreground and background processes
Introduction to Shells
The UNIX Shell Learning Objectives:
Part 1: Basic Commands/Utilities
LINUX System : Lecture 5 (English-Only Lecture)
Shell Environments.
UNIX and Shell Programming (06CS36)
Basic UNIX OLC Training.
Shell Programming.
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

Adv. UNIX:CShell/71 Advanced UNIX v Objectives of these slides: –introduce the C Shell –concentrate on features not in the Bourne Shell (but many are in Bash) Special Topics in Comp. Eng. 1 Semester 2, The C Shell (Chapter 11, Sobell)

Adv. UNIX:CShell/72 Overview 1. Background 2. History 3. Alias 4. Job Control 5. Home Directory Short Forms continued

Adv. UNIX:CShell/73 6. Filename Completion 7. Directory Stacks 8. Variables 9. Automated Scripts 10.Script Programming

Adv. UNIX:CShell/74 1. Background v Originally part of Berkeley UNIX v More tolerant of mistakes than Bourne –e.g can avoid accidental logouts, overwrites v Easier to configure –e.g. alias, history, job control v Some scripting improvements over Bourne: –better variables, arrays, expressions continued

Adv. UNIX:CShell/75 v Almost all the good ideas from the C Shell have been added to Bash: –e.g. history, aliases, job control, directory stacks v I use Bash for scripting and as my command interpreter.

Adv. UNIX:CShell/76 Entering & leaving the C Shell  Check your default shell with ps  Change shell using chsh or type csh  Exit with: exit, logout ( control-D maybe) csh is actually tcsh on most Linuxes

Adv. UNIX:CShell/77 2. History v A list of recently used command lines (events) that can be reused.  $ set history = 25 (record 25 events) $ set savehist = 20 (remember 20 events after logout) $ history (see history list)

Adv. UNIX:CShell/78 Using the history list  $ !! (execute last event)  $ ! (execute event no. ) $ !3  $ ! (execute most recent command that starts with initial-text ) $ !gcc $ !loc

Adv. UNIX:CShell/79 Bash History v history [ number ] –lists the last number commands! v Bash supports the same history list features: –!!, !, ! –!!, !, ! v Relevant environment variables: –HISTFILE=/home/ad/.bash_history –HISTFILESIZE=500 –HISTSIZE=500 continued

Adv. UNIX:CShell/710 v In Bash, you can modify the prompt to include the history number.  Inside.bashrc : " \! includes the history number

Adv. UNIX:CShell/ Alias v Define new commands by using string substitution. v Format: $ alias new-command executed-command(s) v e.g. $ alias ll ls -lg $ ll

Adv. UNIX:CShell/712 Examples v $ alias ls ls -lg v $ alias mi mv -i v $ alias who ’who ; date’ redefining commands is okay

Adv. UNIX:CShell/713 Argument Substitution  \!^ the first argument of the new command  \!* all the arguments of the new command  $ alias wid ’who | fgrep \!^’  $ wid igor is the same as: $ who | fgrep igor

Adv. UNIX:CShell/714 Check your aliases  $ alias (list the current aliases)  $ alias hd (check hd alias)  $ unalias hd (cancel hd alias)  $ \ls (use original meaning of ls ) sysAdmin safety style

Adv. UNIX:CShell/715 Bash Alias  Bash alias works the same way at the command line as in csh : alias, unalias, \command  Defining an alias in.bashrc is different: alias name=value u alias ls='/bin/ls -F' u alias ll='ls -l' u alias h="history 30" continued

Adv. UNIX:CShell/716 v Aliases that require multiple commands or arguments are defined as functions: sgrep() { ps aux | grep $1 | grep -v grep } cs() { cd $1 ls }

Adv. UNIX:CShell/ Job Control v Move commands between foreground and background; suspend commands. v Each background job has a PID and a job number.

Adv. UNIX:CShell/718 Example v $ spell glossary > glossary.out & [1] $ date & [2] Fri Jun 6 16:56:11 GMT [2] Done date $ gcc big.c & [2] finished already continued

Adv. UNIX:CShell/719 v $ jobs [1] - Runningspell glossary.out > glo [2] + Runninggcc big.c v Other status messages: –Stopped –Stopped (tty input) –Done one before last the last one

Adv. UNIX:CShell/720 Background to Foreground v $ fg %job-number v Example: $ fg %2

Adv. UNIX:CShell/721 Foreground to Background  $ control-Z (suspends job and puts it into the background)  $ bg (resumes the job)  $ bg %job-number (resumes job-number )

Adv. UNIX:CShell/722 Stopping  $ stop %job-number  $ kill %job-number (kills job-number )  A job stopped for tty input must be brought to the foreground (with fg ).

Adv. UNIX:CShell/723 State Change v The C Shell prints a message at the next prompt when the state of a job changes.  Use notify to make the shell report a change immediately: $ notify %job-number

Adv. UNIX:CShell/ Home Directory Short Forms  ~ (your home directory)  ~name (home directory of name) e.g. ~ad v Use: $ cp idea.txt ~ $ ls ~ad/teach

Adv. UNIX:CShell/ Filename Completion  $ set filec (‘switch on’ file completion)  $ cat trig1A $ cat trig1A.txt

Adv. UNIX:CShell/726 Control-D for help v $ ls h* help.hist help.text help.trig.01  $ cat h $cat help. help.hist help.text help.trig.01 $ cat help. sometimes

Adv. UNIX:CShell/727 ~ 7. Directory Stacks v Store a list of directories you are using on a stack. v Switch between directories by referring to the stack. adv-unix cops_104

Adv. UNIX:CShell/728 ~ v v $ pwd /home/ad/ $ pushd ~/teach/adv-unix ~/teach/adv-unix ~ $ pwd /home/ad/teach/adv-unix $ pushd ~/cops_104 ~/cops_104 ~/teach/adv-unix ~ $ pwd /home/ad/cops_104 adv-unix cops_104 push the directory onto the stack and do a cd

Adv. UNIX:CShell/729 ~ Change directories quickly v v $ pushd ~/teach/adv-unix ~/cops_104 ~ $ pwd /home/ad/teach/adv-unix/ cops_104 adv-unix push the top directory down and do a cd

Adv. UNIX:CShell/730 v v $ pushd ~/cops_104 ~/teach/adv-unix ~ $ pwd /home/ad/cops_104 ~ adv-unix cops_104 push the top directory down and do a cd

Adv. UNIX:CShell/731 ~Popd   $ popd ~/teach/adv-unix ~ $ pwd /home/ad/teach/adv-unix/ adv-unix pop the top directory and cd to the new top

Adv. UNIX:CShell/732 Other Stack Commands  $ dirs (lists the directories on the stack)  $ pushd +number –move the directory in position number to the top (0), and cd to that directory –the stack is numbered 0, 1, 2,... continued

Adv. UNIX:CShell/733  $ popd +number –pop off the directory at position number –if number is not 0 then there is no change to the present directory

Adv. UNIX:CShell/ Variables 8.1. String Variables 8.2. Numerical Variables 8.3. Special Forms of User Variables 8.4. Shell Variables

Adv. UNIX:CShell/ String Variables  $ set name = fred (include spaces in older csh 's) $ echo $name fred $ set (list set variables) $ unset name (delete name var)

Adv. UNIX:CShell/736 setenv (environment vars)  $ setenv name fred $ echo $name fred $ setenv $ unsetenv name  setenv makes the variable visible to any scripts called from the shell (or from the script containing the setenv ). no =

Adv. UNIX:CShell/737 Arrays of String Vars v $ set colours = (red green blue orange) $ echo $colours red green blue orange $ echo $colours[3] blue $ echo $colours[2-4] green blue orange continued

Adv. UNIX:CShell/738 v $ set shapes = (’’ ’’ ’’ ’’ ’’) $ echo $shapes $ set shapes[4] = square $ echo $shapes[4] square

Adv. UNIX:CShell/739 Braces  Use {...} to distinguish a variable from surrounding text.  $ set prefix = Alex $ echo $prefix is short for {$prefix}ander. Alex is short for Alexander.  Can also write $prefix{ander}

Adv. UNIX:CShell/ Numeric Variables  Use command for variables holding numbers.  count = 0 (or set count = 0 ) $ echo $count 0 count = ( ) $ echo $count 7 result = ( $count < 5 ) $ echo $result 0 continued Remember the space after

Adv. UNIX:CShell/741  count = $count + 5 $ echo $count 12 v Could write: count += 5 v count++ $ echo $count 13

Adv. UNIX:CShell/742 Operators v Most of the C numerical operators: –Arithmetic:+ - * / % –Relational:> = = <= != == –Logical:&& || ! –etc.

Adv. UNIX:CShell/743 Arrays of Numeric Vars v $ set ages = ( ) ages[2] = 15 ages[3] = ( $ages[2] + 4 ) $ echo $ages[3] 19 $ echo $ages

Adv. UNIX:CShell/744 Bourne Shell Equivalents v The Bourne Shell only has string variables.  It uses expr to switch string variables into numbers: $ number=5 (Bourne) $ number=‘expr $number + 2‘ number = 5 (C Shell) number = $number + 2

Adv. UNIX:CShell/ Special Forms of User Variables  $#VAR (returns number of elements in VAR array)  $?VAR (returns 1 if VAR is declared; 0 otherwise)

Adv. UNIX:CShell/746 v $ set days = (mon tues wed thurs fri) $ echo $#days 5 $ echo $?days 1 $ unset days $ echo $?days 0 days is declared days is not declared

Adv. UNIX:CShell/ Shell Variables v Shell variables can be initialised in three ways: –by the shell –by the environment –by the user with set or setenv continued

Adv. UNIX:CShell/748 v There are two types of shell variable: –shell variables that take values; –shell variables that act as switches u they have the value 0 or 1

Adv. UNIX:CShell/749 Shell Vars that take Values  $argv[0] or $0 command name  $argv[1] or $ 1 first argument of command  Also $ 2, $3,... –no upper limit; no need for shift  $argv[*] or $* array of all arguments  $#argv number of arguments continued

Adv. UNIX:CShell/750  $HOME pathname to user’s home directory  $PATH array of pathnames to commands $ setenv PATH (/usr/bin /usr/ucb /bin ~/bin)  $status exit status of last command v etc...

Adv. UNIX:CShell/751 Shell Vars that act as Swiches  $filec turns on file completion  $ignoreeof disables ctrl-D as logout  $noclobber stops file overwriting with >  $notify immediate notification about background job changes v etc... Use:$ set ignoreeof

Adv. UNIX:CShell/ Automated Scripts v At login, the C Shell executes a range of scripts automatically: –/etc/csh.login, /etc/csh.cshrc –.login in your home directory –.cshrc in your home directory v At logout, it executes: –/etc/csh.logout –.logout in your home directory continued

Adv. UNIX:CShell/753  When a shell script is called, the shell will execute the cshrc scripts first.  The cshrc scripts are also executed whenever a new window is created in a X- Windows session.

Adv. UNIX:CShell/754 Examples v $ cat.login setenv TERM vt100 stty erase ‘^X’ kill ‘^U’ -lcase -tabs echo “This is who is on the machine:” who

Adv. UNIX:CShell/755 v $ cat.cshrc set noclobber set ignoreof set history = 100 set prompt = “! % “ set PATH = (/usr/bin /usr/ucb /bin ~/bin) alias h history alias ll ls -lg Only set is needed (not setenv ), since.cshrc is executed at the start of every script.

Adv. UNIX:CShell/756 v $ cat.logout echo Switch off the air-conditioner sleep 10

Adv. UNIX:CShell/ Script Programming Executing a C Shell Script Branching Looping Recursion Interrupt Handling

Adv. UNIX:CShell/ Executing a C Shell Script  Put #!/bin/csh as the first line –may need #!/bin/csh -f to switch off the default initial execution of the cshrc scripts v Or execute: $ csh script  Or make csh your default shell.

Adv. UNIX:CShell/ Branching v if-then v File Test Expressions v Switch

Adv. UNIX:CShell/ if-then v Formats: if (expression) then commands endif if (expression) then commands else commands endif continued

Adv. UNIX:CShell/761 if (expression) then commands else if (expression) then commands : else commands endif

Adv. UNIX:CShell/762 v $ cat if_else1 #!/bin/csh -f # Set class depending on argument value set number = $argv[1] if ($number = 0 && $number = 100 && $number = 0 && $number = 100 && $number < class = 2 class = 3 endif echo The number $number is in class $class

Adv. UNIX:CShell/763 Usage  Due to the #!/bin/csh line, the script can be executed from the Bourne/Bash command line: $ if_else1 55 The number 55 is in class 1 $

Adv. UNIX:CShell/ File Test Expressions  These occur in the expression part of if- then and other control structures. -d filefile is a directory -f filefile is a file -e filefile exists -z filefile is 0 bytes long -r filefile is readable -w file writable -x file executable

Adv. UNIX:CShell/ Switch v Format: switch (string variable) case pattern: commands breaksw case pattern: commands breaksw : default: commands breaksw endsw

Adv. UNIX:CShell/766 Example v $ cat switch_1 #!/bin/csh -f # See if first argument is yes or no. # Deal with a mix of upper and lower case. # Does argv[1] exist? if ($#argv == 0) then echo “Usage: switch_1 [yes | no]” exit 1 endif : continued Usage: $ switch_1 YeS Argument 1 is yes $

Adv. UNIX:CShell/767 switch ($argv[1]) case [yY][eE][sS]: echo Argument 1 is yes breaksw case [nN][oO]: echo Argument 1 is no breaksw default: echo Argument 1 is neither yes or no breaksw endsw

Adv. UNIX:CShell/ Looping v Foreach v While

Adv. UNIX:CShell/ foreach v Format: foreach loop-index (argument-list) commands end

Adv. UNIX:CShell/770 Example 1 v $ cat refname #!/bin/csh -f # Usage: rename arg1 arg2 # # Changes the string arg1 to arg2 # in the filenames of every file in # the current working directory. if ($#argv != 2) then echo Usage: refname arg1 arg2 exit 1 endif : continued Usage: $ refname CPP C

Adv. UNIX:CShell/771 foreach i (‘ls‘) set newname = ‘echo $i | sed s/$1/$2/‘ mv $i $newname end The text in i that matches $1 is changed to $2 VERY DANGEROUS

Adv. UNIX:CShell/772 v $ cat foo-six #!/bin/csh -f # Assign up to 6 command line args # to the buffer array. Execute foo # with buffer as its arguments set buffer = ( count = 1 if ($#argv > 6) then echo “Usage: foo-six [up to 6 args]” exit 1 endif : continued Example 2 Usage: $ foo-six a b c

Adv. UNIX:CShell/773 foreach arg ($argv[*]) set buffer[$count] = count++ end echo There were $count arguments exec foo $buffer[*] exit 0 expands to $argv[0], $argv[1], etc. expands to $buffer[0], $buffer[1], etc. VERY DANGEROUS

Adv. UNIX:CShell/ while v Format: while (expression) commands end

Adv. UNIX:CShell/775 v $cat while_1 # Sum the numbers between 1 and # the value in limit = index = sum = 0 while ($index <= sum += index++ end echo The sum is $sum Example Usage: $ while_1 20 The sum is 210

Adv. UNIX:CShell/ Recursion  The script cppc visits every directory below the one supplied as its argument and renames any files ending with “.CPP ” to end with “.c ” –e.g. foo.CPP --> foo.c –usage: $ cppc code/  cppc could be implemented with find

Adv. UNIX:CShell/777 cppc #!/bin/csh -f # cppc “ replace all.CPP extensions with.c if ($#argv != 1) then echo “Usage: $0 directory” exit(1) endif foreach file ($1/*) # files in directory if (-f $file) then if ($file:e == ‘CPP’) then mv $file $file:r.c echo Modifying $file to $file:r.c endif else if (-d $file) then $0 $file# recursive call endif end VERY DANGEROUS

Adv. UNIX:CShell/778 Notes  Pulling apart a filename that has an extension (e.g. foo.bar ) $ set var = foo.bar $ echo var:r # name without extension foo $ echo var:e # just the extension bar

Adv. UNIX:CShell/779 A Standard Shape v Most recursive scripts have the following 'shape': foreach file ($1/*) # files in directory if (-f $file) then # do something to the file else if (-d $file) then $0 $file# recursive call endif end

Adv. UNIX:CShell/780 Title Words in Web files  The wtitle script examines each “.html ” file in the directories below the one supplied as its argument. v If outputs the title words on each Web page –e.g if it sees: My Home Page then it prints: My Home Page

Adv. UNIX:CShell/781 wtitle #!/bin/csh -f # wtitle: list the title words in HTML files if ($#argv != 1) then echo Usage: $0 directory exit(1) endif foreach file ($1/*) if ((-f $file) && ($file:e == “html”)) then grep -i title $file | \ awk -F\> ’{print $2}’ | \ awk -F\ ’{print $2}’ | \ awk -F\< ’{print $1}’ | \ tr ’ ’ ’\n’ | tr ’A-Z’ ’a-z’ else if (-d $file) then $0 $file# recursive call endif end

Adv. UNIX:CShell/782 Notes  grep -i title $file gets a title tag line: My Home Page My Home Page  awk -F\> ’{print $2}’ separates the line using ‘ > ‘ and prints the second part: My Home Page My Home Page  awk -F\< ’{print $1}’ separates the line using ‘ < ‘ and prints the first part: My Home Page

Adv. UNIX:CShell/783 Usage v $ wtitle caine/ michael caine caine picture caine : v $ wtitle caine/ | sort | uniq caine michael picture : sorted and duplicates removed

Adv. UNIX:CShell/ Interrupt Handling v Not as good as Bourne –only interrupt is supported –the control flow is hard to understand –much improved in tcsh v Format: onintr label v The script must have a line: label:

Adv. UNIX:CShell/785 v $ cat onintr_1 #!/bin/csh -f onintr close while (1) echo Program is running. sleep 2 end close: echo End of Program Usage: $ onintr_1 Program is running Program is running : : End of Program ctrl-C typed