16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introduction to <strong>Unix</strong><br />

* matches 0 or more characters<br />

? matches 1 character<br />

[ccc]<br />

matches a set of characters<br />

[c-c]<br />

set contains a range of chars<br />

ls -l [a-z]*<br />

echo ???<br />

cat ex[0-l]<br />

matches all files starting <strong>with</strong> a lower case letter<br />

matches 3 char file names<br />

matches ex0 and exl<br />

Filename Patterns<br />

One exception is <strong>the</strong> dot "." character which must be explicitly matched if it is <strong>the</strong> first character.<br />

echo *<br />

all files except those starting <strong>with</strong> a dot character<br />

echo .*<br />

all files starting <strong>with</strong> a dot<br />

O<strong>the</strong>rwise <strong>the</strong> special directory files . and .. and many o<strong>the</strong>r normally hidden files would match "*".<br />

Unlike MS-D<strong>OS</strong>, all unix commands can use patterns like <strong>the</strong>se to generate file names, even your own programs.<br />

The reason is that pattern matching is built into <strong>the</strong> shell, instead of being duplicated in every program.<br />

Argument Quoting<br />

Often we wish to send a program which may contain some of <strong>the</strong> special characters like "*", ">", "|" or "&". How<br />

<strong>the</strong>y can we prevent <strong>the</strong> shell from interpreting <strong>the</strong>se as special?<br />

The answer is to use quoting.<br />

There are several ways to quote characters. The first method quotes just one character and is done by preceding it<br />

by a back-slash "\".<br />

Example: echo Now for <strong>the</strong> \* of <strong>the</strong> show...<br />

Outputs: Now for <strong>the</strong> * of <strong>the</strong> show...<br />

Now <strong>the</strong> back-slash is a special character and it can be quoted using ano<strong>the</strong>r back-slash.<br />

Example: echo Slash me back <strong>with</strong> a \\<br />

Outputs: Slash me back <strong>with</strong> a \<br />

Ano<strong>the</strong>r way to quote a string of characters is to place <strong>the</strong>m inside single or double quotes.<br />

Example: echo 'Please enter a letter: [a-z]?'<br />

Outputs: Please enter a letter: [a-z]?<br />

Note : In what it follows we are working at <strong>the</strong> Bourne Shell ( give <strong>the</strong> command sh in <strong>the</strong> prompt ).<br />

Normally spaces and tabs would be separate each argument and <strong>the</strong> key would indicate <strong>the</strong> end of a<br />

command. These special characters can also be quoted so that all characters <strong>with</strong>in a quoted string would be treated<br />

as one argument to <strong>the</strong> program<br />

Example: echo "Line1 Outputs: Line1<br />

Line2<br />

Line2<br />

Line3"<br />

Line3<br />

Note that when typing a multi-line quoted argument, shell will prompt you <strong>with</strong> a ">" instead of "$" to indicate that<br />

<strong>the</strong> string is not yet complete.<br />

Environment Variables<br />

EDITOR=/usr/bin/vi<br />

preferred editor<br />

EXINIT="set redraw aw ai wm=0" vi options<br />

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />

UBUNTU,SOLARIS: Not Defined using any of <strong>the</strong> shells<br />

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />

HOME=/home/users/grad0777 home directory<br />

LOGNAME=grad0777<br />

login name<br />

PATH=/home/newapps/SUNWspro/bin command search path<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!