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 />

PS1=$<br />

shell prompt<br />

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

UBUNTU,SOLARIS: $prompt<br />

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

PS2=><br />

MAIL=/var/mail//grad0777<br />

SHELL=/usr/local/bin/tcsh<br />

TERM=xterm<br />

H<strong>OS</strong>T=kronos<br />

quoted string prompt<br />

mail box<br />

shell program<br />

terminal type<br />

computer host name<br />

Use <strong>the</strong> command "set" <strong>with</strong> no arguments to display <strong>the</strong> values of all shell environment variables.<br />

All environment variables are strings. To change value or create a new variable, use a shell command of <strong>the</strong> form:<br />

=<br />

Example:<br />

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

PS1="Please give me a command? "<br />

Note that <strong>the</strong>re is no space beside <strong>the</strong> equals sign.<br />

If <strong>the</strong> string does not contain any characters that need quoting, <strong>the</strong>n <strong>the</strong> quotes are not needed. i.e. no white space,<br />

or pattern characters<br />

Example:<br />

TERM=vtl00 set terminal type<br />

TMP=/tmp/junk<br />

TMP="<br />

set to null string<br />

TMP=<br />

Each process has its own copy of <strong>the</strong>se environment variables. When a command is executed and a new process is<br />

created by <strong>the</strong> shell, only <strong>the</strong> variables marked to be exported are copied into <strong>the</strong> new process.<br />

Example:<br />

export EXINIT TERM<br />

Most variables set up by <strong>the</strong> system are already marked as exported. These exported variables are accessible <strong>with</strong>in<br />

C programs by using <strong>the</strong> library function getenv().<br />

Example:<br />

char *termtype;<br />

termtype = getenv("TERM");<br />

Note however that ano<strong>the</strong>r program cannot modify <strong>the</strong> environment variables <strong>with</strong>in <strong>the</strong> original shell. This is<br />

because <strong>the</strong>y exist in a separate process and only copies of <strong>the</strong>se variables are available <strong>with</strong>in <strong>the</strong> program.<br />

Shell variables can also be used <strong>with</strong>in <strong>the</strong> shell. Any shell command may contain variable names preceded by<br />

dollar "$" to substitute it's value.<br />

Example:<br />

echo $HOME displays home directory<br />

If a shell variable contains special characters such as white space or pattern characters <strong>the</strong>n <strong>the</strong>se are interpreted<br />

after <strong>the</strong> variable is substituted.<br />

Example:<br />

LIB="curses"<br />

CCFILES="yesno.o *.c"<br />

CC=cc -o myprog -l$LIB $CCFILES<br />

$CC assl kit<br />

Is this <strong>the</strong> same as?<br />

cc -o myprog -lcurses yesno.o assl.c kit.c<br />

Command<br />

Output<br />

echo $CCFILES yesno.o ass1.c kit.c<br />

echo "$CCFILES" yesno.o *.c<br />

echo '$CCFILES' $CCFILES<br />

Vi modes<br />

7

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

Saved successfully!

Ooh no, something went wrong!