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 330 - The.

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

Chapter 9: The TC Shell Everything you thought you knew is now wrong.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
CHAPTER 2 THE UNIX SHELLS by U ğ ur Halıcı. layers in a unix system 1 Users Standard utility programs (shell, editors, compilers, etc.) Standard utility.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
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).
Guide To UNIX Using Linux Third Edition
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Guide To UNIX Using Linux Fourth Edition
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
Unix Shells Based on Glass & Abels’ Book CS240 Computer Science II.
An Introduction to Unix Shell Scripting
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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
CS 2061 Shells Also known as: Unix Command Interpreter.
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.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Linux+ Guide to Linux Certification, Third Edition
CS465 - UNIX The Bourne Shell.
Shell Programming. 222 Lecture Overview  Shell variables  Shell scripts  Control flow and Boolean operators  Shell programming tips  Shell programming.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
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.
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
Chapter Four I/O Redirection1 System Programming Shell Operators.
June 1, 1999Customizing the UNIX Environment1 Introduction to UNIX G. Customizing the UNIX Environment.
CSCI 330 T HE UNIX S YSTEM Bash shell. B ASH S HELL S PECIFICS Shell login and logout files Shell variables Prompt History IO redirection 2 CSCI 330 -
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
CSCI The UNIX System Shell Data Handling: Redirection and Piping
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
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.
Linux+ Guide to Linux Certification, Second Edition
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
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.
Linux Administration Working with the BASH Shell.
Unix Basics.
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
The UNIX Shell Learning Objectives:
CSE 303 Concepts and Tools for Software Development
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
John Carelli, Instructor Kutztown University
CSCI The UNIX System Shell Startup and Variables
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Linux Shell Script Programming
Lecture 4 Redirecting standard I/O & Pipes
Chapter 3 The UNIX Shells
Exploring Shell Commands, Streams, and Redirection
CSC 4630 Meeting 4 January 29, 2007.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
LPI Linux Certification
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

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 UNIX System

I NVOKING CSH On the command line: % csh % tcsh as login shell specified in /etc/passwd with file as argument file is csh-script % csh scriptfile 3 CSCI The UNIX System

C USTOMIZING CSH Initialization files ~/.cshrc ~/.tcshrc ~/.login /etc/csh.cshrc /etc/csh.login options: -fdon’t run initialization files -lrun as login shell also: ~/.logout /etc/csh.logout 4 CSCI The UNIX System

C S HELL V ARIABLES 5 CSCI The UNIX System NameContents cwdThe current working directory historyThe size of the history list ignoreeofPrevents the shell from terminating when pressing Control-D. Use the “logout” or “exit” command. noclobberPrevents existing files from being overridden by output redirection (>), and non-existent files from being appended by append (>>) promptThe shell primary prompt savehistThe number of commands to save in the history file “$HOME/.history” statusThe exit code of the last command

S ETTING SHELL V ARIABLES create shell variable and initialize it to “value” varname: 1-20 characters long, lower case by convention Letters, digits, and underscore First character cannot be a digit Should not be the same as one of the pre-defined variables 6 CSCI The UNIX System Syntax: set varname = value

E XAMPLES : SETTING V ARIABLES 7 CSCI The UNIX System CommandResultComment set x = 123x contains “123”x contains a number, but it is stored as string of digits set x = Hellox contains “Hello”Storing a character string set name = Jane Blackx contains “Jane”Only “Jane” is stored set name = "Jane Black"x contains “Jane Black” When a string contains spaces it must be quoted set x = "Go Huskies!"x contains “Go Huskies!” When a string contains spaces and special characters, it must be quoted

A CCESSING THE V ALUES OF A V ARIABLE $ symbol precedes variable name Examples: % set count = 5 % set number = $count % echo count contains: $count count contains: 5 % echo number contains: $number number contains: 5 8 CSCI The UNIX System

S ETTING E NVIRONMENT V ARIABLES create environment variable, initialize to “value” If envname does not exist, it is created; otherwise, it is overwritten Example: % setenv TERM vt100 % echo $TERM vt100 % setenv EDITOR nano 9 CSCI The UNIX System Syntax: setenv envname value

M ULTI - VALUED SHELL VARIABLE “list” shell variable allows multiple values Syntax: set listvar = (val1 val2 val3 … valN) variable can be used as whole single elements via index range of elements 10 CSCI The UNIX System

U SING “ LIST ” VARIABLES Examples: % set ml=(mary ann bruce linda dara) % echo $ml mary ann bruce linda dara % echo $ml[3] bruce % echo $ml[2-4] ann bruce linda 11 CSCI The UNIX System

R EMOVING A VARIABLE use the “unset” command Example: % set x = one % echo "x contains:" $x x contains: one % unset x % echo "x contains:" $x x: Undefined variable 12 CSCI The UNIX System

S PECIAL BUILT - IN V ARIABLES Examples: % set days=(mon tue wed thr fri sat sun) % echo There are $#days days in a week There are 7 days in a week % echo $?days 1 13 CSCI The UNIX System Variable NameContents/Meaning $#listvarNumber of elements in list variable $?varDetermine if a variable has been declared Return 1, if declared; 0, if not declared

A RITHMETIC V ARIABLES 14 CSCI The UNIX System num = 10echo $r r = $num + 4 r = $num - 5 r = $num * 10 r = $num / 4 r = $num % 3 varname = number Where number is an integer

P ATHNAME MODIFIERS used on variables that contain pathnames four parts to a path: head (h): everything from the beginning of the path up to the last slash in the path tail (t): the last directory or file in the path root (r): the filename without an extension extension (e): the filename name extension use a colon to separate the pathname modifier from the variable name 15 CSCI The UNIX System

E XAMPLE : PATHNAME MODIFIER % set cf="/home/turing/ege/unix/assign1.txt" 16 CSCI The UNIX System h r t /home/turing/ege/unix/assign1.txt e % echo $cf:t assign1.txt

L ISTING ALL V ARIABLES To list shell variables: set To list environment variables: printenv 17 CSCI The UNIX System

C S HELL V ARIABLES - S UMMARY 18 CSCI The UNIX System To …Use the command: Create an environment variable setenv VARNAME value Create a shell variableset varname=value Create a numeric varname = number Create a list variableset listname=(value1 … valueN) List all variablesset or printenv

C S HELL P ROMPT default prompt: % can be set via “prompt” shell variable special character “!” shows current event number beware: use \! to avoid other meanings Example: % set prompt = "$USER-\!: " z : 19 CSCI The UNIX System

C SHELL A LIASES defines abbreviation for a command Syntax: alias name ‘command-list’ use alias like any other command can rename existing commands can reference shell variables 20 CSCI The UNIX System

C SHELL ALIAS SPECIALTIES alias can have arguments default: all arguments are appended use special character “!” to refer to arguments beware: use \! to avoid other meanings \!*all arguments \!^first argument \!:nn-th argument Examples: % alias ll "ls -al" % alias dir "ls -l" % alias count "ls -al \!* | wc -l" 21 CSCI The UNIX System

I/O R EDIRECTION Input Redirection (<) Syntax: command < file Command will read (take input) from file, instead of from terminal Output Redirection (>) Syntax: command > file Sends output of command to file, instead of to terminal Appending Output (>>) Syntax: command >> file Appends output of command to existing file 22 CSCI The UNIX System

“ NOCLOBBER ” VARIABLE if set, then does not allow output redirection to existing file does not allow appending to non-existing file Examples: % set noclobber % who > current-users current-users: File exists % date >> usage-status status: No such file or directory 23 CSCI The UNIX System

O VERRIDE THE E XTRA P ROTECTION Add ! To the redirection ! Means “do what I mean!” Examples: % set noclobber % who >! current-users Allows redirection even if ‘current-users’ exists % date >>! usage-status Creates the file named “usage-status” even if it does not already exist 24 CSCI The UNIX System

H ANDLING S TANDARD E RRORS Sometimes a command has special output to inform of problems % gcc gets.c > compile.log "gets.c":8: syntax error Syntax: command >& file Redirect stdout and stderr to file Syntax: command >>& file Append stdout and stderr to file 25 CSCI The UNIX System

E XAMPLES : R EDIRECT /A PPEND STDERR % gcc gets.c >& compile.log % cat compile.log “gets.c”:8: syntax error % gcc gets.c >>& compile.log 26 CSCI The UNIX System

P IPE : STDOUT AND STDERR Syntax: command1 |& command2 Sends stdout and stderr of command1 to be stdin for command2 Example: % gcc gets.c |& more Sends stdout and stderr to ‘more’ for convenient viewing % gcc gets.c |& lpr Produces a hardcopy of the messages 27 CSCI The UNIX System

S UMMARY : R EDIRECTIONS AND P IPE 28 CSCI The UNIX System Command SyntaxMeaning command < fileRedirect input from file to command command > fileRedirect output from command to file command >& fileRedirect output and errors to file command >> fileRedirect output of command and appends it to file command1 | command2Take/pipe output of command1 as input to command2 command1 |& command2Take/pipe output and errors of command1 as input to command2

S UMMARY : R EDIRECTIONS AND P IPE 29 CSCI The UNIX System Command SyntaxMeaning command >! fileIf the ‘noclobber’ variable is set, override its effects for this command and either open or overwrite file command >>! fileOverride ‘noclobber’ variable; if file does not exist, it is created and output from command is appended to it command >>&! fileOverride ‘noclobber’ variable; if file does not exist, it is created and both output and errors are appended to it