CSCI 330 T HE UNIX S YSTEM C Shell. C S HELL SPECIFICS Startup and initialization Shell variables...
-
Upload
alisha-powers -
Category
Documents
-
view
229 -
download
5
Transcript of CSCI 330 T HE UNIX S YSTEM C Shell. C S HELL SPECIFICS Startup and initialization Shell variables...
C SHELL SPECIFICS
Startup and initialization Shell variables Prompt Alias Redirections and pipe
2
CS
CI 330 - T
he UN
IX S
ystem
INVOKING 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
CS
CI 330 - T
he UN
IX S
ystem
CUSTOMIZING CSH
Initialization files ~/.cshrc ~/.tcshrc ~/.login /etc/csh.cshrc /etc/csh.login
options: -f don’t run initialization files -l run as login shell
also: ~/.logout /etc/csh.logout
4
CS
CI 330 - T
he UN
IX S
ystem
C SHELL VARIABLES
5
CS
CI 330 - T
he UN
IX S
ystem
Name Contentscwd The current working directory
history The size of the history list
ignoreeof Prevents the shell from terminating when pressing Control-D. Use the “logout” or “exit” command.
noclobber Prevents existing files from being overridden by output redirection (>), and non-existent files from being appended by append (>>)
prompt The shell primary prompt
savehist The number of commands to save in the history file “$HOME/.history”
status The exit code of the last command
SETTING SHELL VARIABLES
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
CS
CI 330 - T
he UN
IX S
ystem
Syntax: set varname = value
EXAMPLES: SETTING VARIABLES
7
CS
CI 330 - T
he UN
IX S
ystem
Command Result Comment
set x = 123 x contains “123” x contains a number, but it is stored as string of digits
set x = Hello x contains “Hello” Storing a character string
set name = Jane Black x 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
ACCESSING THE VALUES OF A VARIABLE
$ 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
CS
CI 330 - T
he UN
IX S
ystem
SETTING ENVIRONMENT VARIABLES
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
CS
CI 330 - T
he UN
IX S
ystem
Syntax: setenv envname value
MULTI-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
CS
CI 330 - T
he UN
IX S
ystem
USING “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
CS
CI 330 - T
he UN
IX S
ystem
REMOVING 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
CS
CI 330 - T
he UN
IX S
ystem
SPECIAL BUILT-IN VARIABLES
Examples:% set days=(mon tue wed thr fri sat sun)% echo There are $#days days in a weekThere are 7 days in a week% echo $?days1 13
CS
CI 330 - T
he UN
IX S
ystem
Variable Name Contents/Meaning$#listvar Number of elements in list variable
$?var Determine if a variable has been declared
Return 1, if declared; 0, if not declared
ARITHMETIC VARIABLES
14
CS
CI 330 - T
he UN
IX S
ystem
Symbol Meaning Example
@ num = 10 echo $r
+ Addition @ r = $num + 4
- Subtraction @ r = $num - 5
* Multiplication @ r = $num * 10
/ Division @ r = $num / 4
% Modulo @ r = $num % 3
Syntax: @ varname = numberWhere number is an integer
14
5
100
2
1
PATHNAME 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
CS
CI 330 - T
he UN
IX S
ystem
EXAMPLE: PATHNAME MODIFIER
% set cf="/home/turing/ege/unix/assign1.txt"
16
CS
CI 330 - T
he UN
IX S
ystem
h
r
t
/home/turing/ege/unix/assign1.txt
e
% echo $cf:tassign1.txt
LISTING ALL VARIABLES
To list shell variables: set or @
To list environment variables: printenv
17
CS
CI 330 - T
he UN
IX S
ystem
C SHELL VARIABLES - SUMMARY
18
CS
CI 330 - T
he UN
IX S
ystem
To … Use the command:
Create an environment variable
setenv VARNAME value
Create a shell variable set varname=value
Create a numeric variable @ varname = number
Create a list variable set listname=(value1 … valueN)
List all variables set or @ or printenv
C SHELL PROMPT
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
CS
CI 330 - T
he UN
IX S
ystem
C SHELL ALIASES
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
CS
CI 330 - T
he UN
IX S
ystem
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\!:n n-th argument
Examples:% alias ll "ls -al"% alias dir "ls -l"% alias count "ls -al \!* | wc -l" 21
CS
CI 330 - T
he UN
IX S
ystem
I/O REDIRECTION
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
CS
CI 330 - T
he UN
IX S
ystem
“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 directory23
CS
CI 330 - T
he UN
IX S
ystem
OVERRIDE THE EXTRA PROTECTION
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
CS
CI 330 - T
he UN
IX S
ystem
HANDLING STANDARD ERRORS
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
CS
CI 330 - T
he UN
IX S
ystem
EXAMPLES: REDIRECT/APPEND STDERR
% gcc gets.c >& compile.log
% cat compile.log
“gets.c”:8: syntax error
% gcc gets.c >>& compile.log
26
CS
CI 330 - T
he UN
IX S
ystem
PIPE: 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
CS
CI 330 - T
he UN
IX S
ystem
SUMMARY: REDIRECTIONS AND PIPE
28
CS
CI 330 - T
he UN
IX S
ystem
Command Syntax Meaning
command < file Redirect input from file to command
command > file Redirect output from command to file
command >& file Redirect output and errors to file
command >> file Redirect output of command and appends it to file
command1 | command2 Take/pipe output of command1 as input to command2
command1 |& command2 Take/pipe output and errors of command1 as input to command2
SUMMARY: REDIRECTIONS AND PIPE
29
CS
CI 330 - T
he UN
IX S
ystem
Command Syntax Meaning
command >! file If the ‘noclobber’ variable is set, override its effects for this command and either open or overwrite file
command >>! file Override ‘noclobber’ variable; if file does not exist, it is created and output from command is appended to it
command >>&! file Override ‘noclobber’ variable; if file does not exist, it is created and both output and errors are appended to it