Download presentation
Presentation is loading. Please wait.
Published byAlisha Powers Modified over 9 years ago
1
CSCI 330 T HE UNIX S YSTEM C Shell
2
C S HELL SPECIFICS Startup and initialization Shell variables Prompt Alias Redirections and pipe 2 CSCI 330 - The UNIX System
3
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 330 - The UNIX System
4
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 330 - The UNIX System
5
C S HELL V ARIABLES 5 CSCI 330 - 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
6
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 330 - The UNIX System Syntax: set varname = value
7
E XAMPLES : SETTING V ARIABLES 7 CSCI 330 - 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
8
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 330 - The UNIX System
9
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 330 - The UNIX System Syntax: setenv envname value
10
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 330 - The UNIX System
11
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 330 - The UNIX System
12
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 330 - The UNIX System
13
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 330 - 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
14
A RITHMETIC V ARIABLES 14 CSCI 330 - The UNIX System SymbolMeaningExample @ num = 10echo $r +Addition@ r = $num + 4 -Subtraction@ r = $num - 5 *Multiplication@ r = $num * 10 /Division@ r = $num / 4 %Modulo@ r = $num % 3 Syntax: @ varname = number Where number is an integer 14 5 100 2 1
15
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 330 - The UNIX System
16
E XAMPLE : PATHNAME MODIFIER % set cf="/home/turing/ege/unix/assign1.txt" 16 CSCI 330 - The UNIX System h r t /home/turing/ege/unix/assign1.txt e % echo $cf:t assign1.txt
17
L ISTING ALL V ARIABLES To list shell variables: set or @ To list environment variables: printenv 17 CSCI 330 - The UNIX System
18
C S HELL V ARIABLES - S UMMARY 18 CSCI 330 - The UNIX System To …Use the command: Create an environment variable setenv VARNAME value Create a shell variableset varname=value Create a numeric variable@ varname = number Create a list variableset listname=(value1 … valueN) List all variablesset or @ or printenv
19
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-\!: " z036473-26: 19 CSCI 330 - The UNIX System
20
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 330 - The UNIX System
21
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 330 - The UNIX System
22
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 330 - The UNIX System
23
“ 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 330 - The UNIX System
24
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 330 - The UNIX System
25
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 330 - The UNIX System
26
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 330 - The UNIX System
27
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 330 - The UNIX System
28
S UMMARY : R EDIRECTIONS AND P IPE 28 CSCI 330 - 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
29
S UMMARY : R EDIRECTIONS AND P IPE 29 CSCI 330 - 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.