Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Slides for Specifying Instructions to the Shell | CS 333, Study notes of Computer Science

Material Type: Notes; Class: UNIX Operating Sys Fundamental; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Fall 2006;

Typology: Study notes

2009/2010

Uploaded on 04/12/2010

koofers-user-vf5-1
koofers-user-vf5-1 🇺🇸

2

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS
CS-
-344
344 -
-Unix Operating
Unix Operating
System Fundamentals
System Fundamentals
Lecture 7
Specifying Instructions to the Shell
Based on slides created by
Dr. Bangalore for the
Spring 2005 offering of
the course
10/13/2006 3
Department of Com
puter and Informati
on Sciences
UAB
Declaring and Using Variables in the
Declaring and Using Variables in the
Shell
Shell
Declaring Variables: variable_name =
variable_name =
value
value
Display Variables: echo $variable_name
echo $variable_name
Examples:
a=Hello
b=date
$b
c=‘date’
10/13/2006 4
Department of Com
puter and Informati
on Sciences
UAB
Declaring and Using Variables in the
Declaring and Using Variables in the
Shell
Shell
Difference between single and double quotes
Double quotes ignore most special characters, but
command and variable substitution is performed
Single quotes, no substitution performed:
Examples:
d=“You are currently logged o n to `hostname`”
You are currently logged on to vulcan5.cis.uab.edu
e=“Today’s date is: $b” (b =date)
f=“Today’s date is: $c” (c=‘da te’)
echo $e $f
Today’s date is: date Today's date is: Fri Oct 13
10:03:37 CDT 2006
10/13/2006 5
Department of Com
puter and Informati
on Sciences
UAB
Overwriting Existing Files
Overwriting Existing Files
When output is redirected to a file, if the file
already exists it’s overwritten.
To avoid overwriting existing files set the
noclobber variable: set
set
o noclobber
o noclobber
When noclobber variable is set, the shell
complains that the file exists
To overwrite a file when noclobber variable is set,
use >!
>! instead of >
>for output redirection
Example: date >! filename
date >! filename
10/13/2006 6
Department of Com
puter and Informati
on Sciences
UAB
Avoiding Accidental Removal of Files
Avoiding Accidental Removal of Files
Unlike Windows, in UNIX when files are deleted
they cannot be undeleted
noclobber
noclobber feature is an instruction to the shell
not to the UNIX commands (
e.g., cp, mv, rm)
To avoid accidental removal of files use
i
ioption
with cp, mv, rm
set alias for cp, mv, rm, etc. to avoid using
i
i
every time that you use those commands
alias rm=’rm –i’
alias cp=’cp –i’
alias mv=’mv –i’
To override an alias use “\
\command
command instead of
command (e.g., \
\rm filename
rm filename)
pf3

Partial preview of the text

Download Slides for Specifying Instructions to the Shell | CS 333 and more Study notes Computer Science in PDF only on Docsity!

CS CS-- 344344 - - Unix OperatingUnix Operating

System Fundamentals System Fundamentals

Lecture 7

Specifying Instructions to the Shell

Based on slides created by

Dr. Bangalore for the

Spring 2005 offering of

the course

10/13/2006 3 Department of Com

puter and Informati

on Sciences

UAB

Declaring and Using Variables in theDeclaring and Using Variables in the

ShellShell

 Declaring Variables: variable_name =variable_name =

valuevalue

 Display Variables: echo $variable_nameecho $variable_name

 Examples:

 a=Hello

 b=date

 $b

 c=‘date’

10/13/2006 4 Department of Com

puter and Informati

on Sciences

UAB

Declaring and Using Variables in the Declaring and Using Variables in the

Shell Shell

 Difference between single and double quotes

 Double quotes ignore most special characters, but command and variable substitution is performed  Single quotes, no substitution performed:  Examples:  d=“You are currently logged on to hostname” You are currently logged on to vulcan5.cis.uab.edu

 e=“Today’s date is: $b” (b=date)  f=“Today’s date is: $c” (c=‘date’)  echo $e $f Today’s date is: date Today's date is: Fri Oct 13 10:03:37 CDT 2006

10/13/2006 5 Department of Com

puter and Informati

on Sciences

UAB

Overwriting Existing FilesOverwriting Existing Files

 When output is redirected to a file, if the file

already exists it’s overwritten.

 To avoid overwriting existing files set the

noclobber variable: setset – –o noclobbero noclobber

 When noclobber variable is set, the shell

complains that the file exists

 To overwrite a file when noclobber variable is set,

use >!>! instead of >> for output redirection

 Example: date >! filenamedate >! filename

10/13/2006 6 Department of Com

puter and Informati

on Sciences

UAB

Avoiding Accidental Removal of Files Avoiding Accidental Removal of Files

 Unlike Windows, in UNIX when files are deleted

they cannot be undeleted

 noclobbernoclobber feature is an instruction to the shell

not to the UNIX commands (e.g., cp, mv, rm)

 To avoid accidental removal of files use – –ii option

with cp, mv, rm

 set alias for cp, mv, rm, etc. to avoid using – –ii

every time that you use those commands

 alias rm=’rm –i’  alias cp=’cp –i’  alias mv=’mv –i’

 To override an alias use “\commandcommand” instead of

command (e.g., \rm filenamerm filename)

10/13/2006 7 Department of Com

puter and Informati

on Sciences

UAB

Redirecting Error MessagesRedirecting Error Messages

 Utilities write to the error stream when an error occurs during their execution   (^) lsls – –ll filename (if the filename does not exist or the file permissions are not sufficient)

 We have seen output redirection using “>>” and “>>>>”. To redirect error messages use “2>2>” or “2>>2>>” (in bash)  The shell assigns a 1 for the standard output stream and 2 for the standard error stream (“>>” is same as “1>1>”)

 To redirect both error and output together use “2>&12>&1” after the filename   PrintPrint stdoutstdout to screen,to screen, stderrstderr toto outerrouterr lsls^ – –l myfile xyz 2>l myfile xyz 2>^ outerrouterr

  PrintPrint stdoutstdout ANDAND stderrstderr toto outerrouterr lsls – –ll myfilemyfile xyz >xyz > outerrouterr 2>&12>& 10/13/2006 8 Department of Com

puter and Informati

on Sciences

UAB

Communicative Shell Communicative Shell

 The expansion and modification made by the

shell when multiple commands or special

characters are used can be viewed by starting the

shell with – –xx option

 We can start a new shell by typing “bashbash – –xx” or

execute a script “bashbash – –x myscriptx myscript” from the

current shell

$ bash -x $ tr '\t' ':' < quizscores | sort

  • tr '\t' :
  • sort …… ……

$ bash -x $ c="You are currently logged on to hostname" ++ hostname

  • c=You are currently logged on to blazer $ echo $c
  • echo You are currently logged on to blazer You are currently logged on to blazer

10/13/2006 9 Department of Com

puter and Informati

on Sciences

UAB

Shell Command Line Expansion (I)Shell Command Line Expansion (I)

 Wildcard character – **

 Matches zero or more characters  List all files that end with .java – ls .javals .java  List all files that start with cs – (^) ls csls cs

 Matching character – ??

 Matches one character  List all files that start with chapter followed by any character – (^) ls chapter?ls chapter?

 ** and ?? are special characters called meta-

characters. These characters are interpreted by

the shell and should not be used in filenames

10/13/2006 10 Department of Com

puter and Informati

on Sciences

UAB

Shell Command Line Expansion (II) Shell Command Line Expansion (II)

 To specify filename within a specified range use [][]

 Only one character is matched  List all files that start with chapter and has a number following it within the range 1-5 – ls chapter[1ls chapter[1--5]5]

 To match and create multiple filenames from one

pattern use {}{}

 Curly braces match files specified in the braces, but will NOT expand ranges  List all files that start with chapter and has specific numbers following it – ls chapter{2,3,4}ls chapter{2,3,4}

 All meta-characters can be combined together

 ls *[0ls *[0--9]?{java,c,cpp}9]?{java,c,cpp}

10/13/2006 11 Department of Com

puter and Informati

on Sciences

UAB

Local & Environment VariablesLocal & Environment Variables

 Use “export variable_nameexport variable_name”

  a=Helloa=Hello

  export aexport a

  export b=Worldexport b=World

  export PATH=${PATH}:~/binexport PATH=${PATH}:~/bin

 Remove variable – unsetunset

  unset aunset a

 List all variables – setset

 List environment variables – envenv

10/13/2006 12 Department of Com

puter and Informati

on Sciences

UAB

Miscellaneous Miscellaneous

  viewview: similar to vi with the read-only flag set

  splitsplit: split long files into smaller files

 splitsplit – –l linecount filename suffixl linecount filename suffix  splitsplit – –l 20 longfile file+l 20 longfile file+