Introduction to Unix Shell & Scripting with csh/tcsh

Slides:



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

Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH Scripts.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
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.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
CS 497C – Introduction to UNIX Lecture 36: - Customizing the Environment Chin-Chih Chang
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.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – Shell Programming The activities of.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Welcome to CSE  Name: Di Cao   Classroom: DL357  Class Time: T 8:30am - 9:18am  Office.
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
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
CST8177 bash Scripting Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
July 17, 2003Serguei A. Mokhov, 1 Shells and Shell Scripts COMP 444/5201 Revision 1.3 January 25, 2005.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
LINUX System : Lecture 6 Shell Programming
Linux+ Guide to Linux Certification, Third Edition
CS465 - UNIX The Bourne Shell.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
Introduction to Unix Shell & Scripting with csh/tcsh  Brief Unix History  Unix Shell & Flavor  CSH/TCSH 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.
1 Applied CyberInfrastructure Concepts ISTA 420/520 Fall Nirav Merchant Bio Computing & iPlant Collaborative Eric Lyons.
ITR3 lecture 6: intoduction to UNIX Thomas Krichel
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Introduction to Programming Using C An Introduction to Operating Systems.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
Agenda Managing Processes (Jobs) Command Grouping Running jobs in background (bg) Bringing jobs to foreground (fg), Background job status (jobs) Suspending.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Lesson 8-Specifying Instructions to the Shell. Overview An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
Linux Administration Working with the BASH Shell.
Agenda Customizing a Unix/Linux account Environment Introduction to Start-up Files (.bash_profile,.bashrc,.profile,.kshrc) Safe Methods for Changing Start-up.
ULI101 Week 10. Lesson Overview ● Shell Start-up and Configuration Files ● Shell History ● Alias Statement ● Shell Variables ● Introduction to Shell Scripting.
1 Applied CyberInfrastructure Concepts 1 Nirav Merchant Bio Computing & iPlant Collaborative Eric Lyons
Implementation of a simple shell, xssh
Department of Computer Engineering
SUSE Linux Enterprise Desktop Administration
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Implementation of a simple shell, xssh (Section 1 version)
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Implementation of a simple shell, xssh
CAP652 Lecture - Shell Programming
Shell Environments.
CSE 303 Concepts and Tools for Software Development
LING 408/508: Computational Techniques for Linguists
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Chapter 3 The UNIX Shells
Bash Scripting CS 580U - Fall 2018.
Presentation transcript:

Introduction to Unix Shell & Scripting with csh/tcsh Brief Unix History Unix Shell BEFORE TEACHING THIS SEMINAR, LOGIN TO WILLOW ACCOUNT STUDENT and execute the file “./.clean” which will create 30 subdirectories (numbered) and copy the needed scripts into each one. Assign each student a number, and have them do their work in that numbered subdirectory. Don’t forget to change the willow student password after the seminar.

Unix Family Tree

Unix Architecture

Unix History and Motivation The first version of Unix came from AT&T in the early 1970s (Unix is old!). Unix was developed by programmers and for programmers. Unix is designed so that users can extend the functionality To build new tools easily and efficiently To customize the shell and user interface. To string together a series of Unix commands to create new functionality. To create custom commands that do exactly what we want.

What is Shell? Shell is Programming Shell can do Command Interpreter that turns text that you type (at the command line) in to actions: User Interface: take the command from user Programming Shell can do Customization of a Unix session Scripting

Customization of a Session Each shell supports some customization. User prompt Where to find mail Shortcuts (alias) The customization takes place in startup files Startup files are read by the shell when it starts up The Startup files can differ for different shell

Types of Shells Interactive vs. Non-interactive; login or not Interactive login shell started after login Non-interactive shell Present when shell script is running Just inherits parent’s environment Interactive non-login shell started Started from a command line Copies parent environment then invokes ~/.bash_rc (or ~/.cshrc or ~/.tcshrc)

Popular Shells sh Bourne Shell ksh Korn Shell csh,tcsh C Shell (for this course) bash Bourne-Again Shell

Families of Shells http://www.novell.com/coolsolutions/img/portable1.gif

Flavors of Unix Shells Two main flavors of Unix Shells To check shell: Bourne (or Standard Shell): sh, ksh, bash, zsh Fast $ for command prompt C shell : csh, tcsh better for user customization and scripting %, > for command prompt To check shell: % echo $SHELL (shell is a pre-defined variable) To switch shell: % exec shellname (e.g., % exec bash)

Startup files sh,ksh: /etc/profile (out-of-the-box login shell settings) /etc/profile.local (addtnl. local system settings) ~/.profile (addtnl. user customized settings) ~/.kcshrc (non-login shell user customization) bash: /etc/profile (out-of-the-box login shell settings) /etc/bash.bashrc (out-of-box non-login settings) /etc/bash.bashrc.local (global non-login settings) ~/.bash_profile (login shell user customization) ~/.bashrc (non-login shell user customization) ~/.bash_logout (user exits from interactive login shell) csh/tcsh: /etc/login (out-of-the-box login shell settings) /etc/csh.login (non-login shell customizations) /etc/csh.login.local (global non-login settings) ~/.login: (login shell user customizations) ~/.cshrc: (non-login shell user customizations) ~/.cshrc.logout: (non-login shells at logout) ~/.logout: (read by login shells at logout)

Some Special Keys Under tcsh Ctrl-U = Delete everything on the command-line Ctrl-A = Move cursor to the front Ctrl-E = Move cursor to the end Ctrl-P = Set the current command-line to the previous command Ctrl-N = Set the current command-line to the next command TAB = Filename completion

Don’t forget your Best Friend % man command (e.g., % man ls) shows information about the command usually space or Enter for more information q to quit % man man

Create a shell script Creating a simple shell script A shell script is a file that contains commands that the shell can execute. Any commands you enter in response to a shell prompt. A utility A compiled program Another shell script Control flow commands Run a shell script Enter the script filename on the command line The shell interprets and execute the commands one after another Why shell script? Simply and quickly initiate a complex series of tasks or a repetitive procedure.

Shell programming Make the file executable When you create a shell script using a editor, does it have execute permission typically? Example: (Make sure you are using tcsh/csh script!...) willow> echo $SHELL /bin/tcsh willow> ./test ./test: Permission denied. willow> ls -l test -rw-r--r-- 1 student ums 33 Sep 18 16:33 test willow> chmod +x test This is Test! Login to willow student account. 2. Cd to numbered working directory 3. Execute the script: “test”

Invoking a Shell script Give the shell a command on the command line The shell forks a process Which creates a non-interactive duplicate of the shell process The newly forked process attempts to exec the command If the command is an executable program Exec succeeds System overlays the newly created subshell with the executable program The command is a shell script Exec failed The command is assumed to be a shell script The subshell runs the commands in the shell.

Invoking a Shell script The shell itself is program It can be run as a command in a shell and also accepts arguments. Note: Let’s find your default shell executing “echo $SHELL” willow> echo $SHELL /bin/tcsh To run a shell script Which does not have executable permission Ex: willow>tcsh test Run the script with different shell other than your interactive shell Ex: willow>sh test

Invoking a Shell script Put special characters on the first line of a shell script To tell OS checks what kind of file it is before attempting to exec it To tell which utility to use (sh, csh, tcsh, …) Special sequence The firsts two character of a script are #! Then followed by the absolute pathname of the program that should execute the script Ex: willow> more test #!/bin/tcsh # This line will not run since it is commented out... echo 'This is Test!‘

Make a comment # Comments make shell scripts easier to read and maintain Pound sign (#) start a comment line until the end of that line as second line in previous example, except #! In the first line. Or inside quotes

Parameters and Variables A shell parameter is associated with a value that is accessible to the user. Shell variables Names consist of letters, digits and underscores By convention, environment variables use UPPERCASE User created variables (create and assign value) Keyword shell variables Have special meaning to the shell Being created and initialized by the startup file Positional parameters Allow you to access command line arguments Special parameters Such as The name of last command The status of most recently executed command The number of command-line arguments

Positional Parameters The command name and arguments are the positional parameters. Because you can reference them by their position on the command line $0 : Name of the calling program $1 - $9 : Command-line Arguments The first argument is represented by $1 The second argument is represented by $2 And so on up to $9 The rest of arguments have to be shifted to be able to use $1- $9 parameters.

Positional Parameters Example: Change directory to your assigned numbered subdirectory willow> cd 1 List the directory contents, confirming display_5args willow> ls -l display_5args Change mode of display_5args to executable willow> chmod +x display_5args Execute the script willow> ./display_5args 1 2 3 4 5 you are running script ./display_5args with parameter 1 2 3 4 5 Cd to (or confirm that you are in) your assigned numbered subdirectory on willow Change execution mode to executable via “chmod +x display_5args”. Run display_5args. Have them look at this script, too.

Positional Parameters $1-$9 allows you to access 10 arguments How to access others? Promote command-line arguments: shift Built-in command shift promotes each of the command-line arguments. The first argument ( which was $1) is discarded The second argument ( which was $2) becomes $1 The third becomes the second And so on Makes additional arguments available Repeatedly using shift is a convenient way to loop over all the command-line arguments Could use an animation for this

Positional Parameters Example: willow> more demo_shift #!/bin/tcsh echo $1 $2 $3 shift echo $1 $2 echo $1 willow> ./demo_shift 1 2 3 1 2 3 2 3 3 Change execution mode to executable via “chmod +x demo_shift”. Run demo_shift.

willow> more demo_shift #!/bin/tcsh echo $1 $2 $3 shift echo $1 $2 willow> ./demo_shift 1 2 3 4 1 2 3 2 3 3 shift: No more words. Modify demo_shift file and save it by adding the last 6 lines as shown in slide 20. Then reexecute

Special Parameters Useful values Command-line arguments Execution of shell commands Can not change the value directly, like positional parameters Value of Command-line arguments: $* and $@ $* and $@represent all the command_line arguments ( not just the first nine) “$*” : treats the entire list of arguments as a single argument “$@” : produce a list of separate arguments (Only bash/ksh/sh)

BASH SCRIPT WITH $*and $@ willow> more for_test.bash #!/bin/bash echo "using \$* " for arg in "$*" do echo "$arg" done echo "using \$@ " for arg in "$@" willow> ./for_test.bash 1 2 3 using $* 1 2 3 using $@ 1 2 3 TCSH SCRIPT WITH $*and $@ willow> more for_test #!/bin/tcsh echo 'using $*' foreach arg ($*) echo "$arg" end echo 'using $@' foreach arg ($@) willow> ./for_test 1 2 3 using $* 1 2 3 using $@ Illegal variable name. Change execution mode to executable for both files. Run both as shown in the slide…

Special Parameters The number of arguments: $# Return a decimal number Use the test to perform logical test on this number willow> more num_args echo this script is called with $# arguments. willow> chmod +x num_args willow> ./num_args this script is called with 0 arguments. willow> ./num_args 1 this script is called with 1 arguments. willow> ./num_args 2 willow> ./num_args 0 Change execution mode to executable. Run num_args as shown in slide.

Special Parameters Exit status: $? When a process stops executing for any reason, it returns an exit status to its parent process. By convention, Nonzero represents a false value that the command failed. A zero value is true and means that the command was successful You can specify the exit status that a shell script returns by using the exit built-in followed by a number Otherwise, the exit status of the script is the exit status of the last command the script ran.

willow> more exit_status willow> ls a a: No such file or directory willow> echo $? 2 willow> echo olemiss olemiss willow> more exit_status echo this program will have the exit code of 8. exit 8 willow> ./exit_status this program will have the exit code of 8. 8 1 Change execution mode to executable. Run file as shown in slide.

Summary A shell is both a command interpreter and a programming language. Job control Control-z/fg/bg/& Variables Local and environment variables Declare and initialize a variable ( no type) Export unset Command line expansion Parameter expansion/variable expansion/command/substitution/pathname expansion Quote ( ‘ ‘ “ “ \ ) “ “ all but parameter, variable expansion and \ ‘ ‘ suppress all types of expansion \ escaping the following special character

Basic Script Example willow> more basic_script #!/bin/tcsh echo 'Listing the files in long format appending due date/time' echo ls -lrtah df -k # Using diff to find two files differences and writing them to another file diff -c for_test.bash for_test >> file_differences & echo 'sleeping mode for 4 seconds. Please wait!' sleep 4 echo 'GO REBELS' echo 'To find out the differences of files for_test and for_test.bash, ' echo 'Please open file_differences via using cat command as shown below:' echo 'cat file_differences’ Change execution mode to executable. Run the file and then follow the last line instructions from the output… Be creative and show more examples if you have time!...

Killing BAD Processes The “kill” command: The “killall” command: kill [-<signal>] <pid> Send <signal> to process <pid> The “killall” command: killall [-<signal>] <command> Send <signal> to all processes that start with <command> Useful signals (kill –l for the complete list): TERM the default, “terminate”, kills things nicely KILL will kill anything, but not nicely HUP “hangup”, used to reload configurations STOP stops (suspends) a running process

Summary Shell parameters HOME PATH PS1 SHELL $0 $n $* $@ $# $$ $! $?

Summary Special Characters NEWLINE ; () & | > >> < << * ? \ ‘ “ ` ` [] $ . # && || !