Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

2/15/13

devices, terminals and csh

UNIX
Copyright B. Brown, 1988-2000. All rights reserved. March 2000.

Assignment 4: Terminals, Devices and csh /2 Using C Shell Aliases


This is used to simplify commands, supply default arguments, or perform transformations on commands and their arguments. The substitution of aliases is performed inside the shell immediately upon the command after input by the user. Type the following command to provide an alias for the 'ls' command. % alias ls ls -la This example provides a default argument of '-la' whenever the ls command is used. What would you type to provide an alias for the DOS commands dir and type _________________________________________________ _________________________________________________ Aliases can be defined that contain multiple commands or pipes. The following example lists the directory whenever the cd command is used. % alias cd 'cd \!* ; ls' Note that the command string is enclosed in quotes, and that the history command !* is prefixed with a slash to prevent interpretation by the shell.

Using the above example as a guide, write in the space provided below an alias for cd which prints the current directory whenever the cd command is used. _________________________________________________

Redirecting Input And Output


The C shell format is similar to that of the Bourne shell. Consider the following examples.
l > fl s & ie l | lr s & p l >!tm s & ep ;rdrc sdu adsdr t fl eiet tot n ter o ie ;pp sdu adsdr t ln-rne ie tot n ter o iepitr ;rdrc sdu adsdr t tm ee i eiet tot n ter o ep vn f
1/7

uva.ulb.ac.be/cit_courseware/unix/unix16.htm

2/15/13

devices, terminals and csh

l > tm1 s > ep l >!tm1 s > ep

;ncobri st olbe s e ;apn t tm1 ped o ep ;rdrc sdu t tm1ee we ncobri st eiet tot o ep vn hn olbe s e

Creating Background Jobs


The syntax is identical to the Bourne shell, using the & character.

Built-In Commands
The C shell supports a number of built-in commands. alias assign new aliases or display current aliases. Type alias to list all current aliases. What is the current alias for cd? _________________________________________________ env displays the current environment. What is the current setting for the shell variable 'LOGNAME'? _________________________________________________ exit terminates the C shell (or a csh script). Do not type this command yet! Some systems use the command logout. history displays the contents of the history list. What was the second to last command typed? _________________________________________________ rehash causes the C shell to recompute a table of command locations. repeat causes the command to execute a number of times. Its format is repeat number command-list To echo the message 'help' on the console ten times, the command you would use is, _________________________________________________ setenv
uva.ulb.ac.be/cit_courseware/unix/unix16.htm 2/7

2/15/13

devices, terminals and csh

sets variables in the environment. How would you type to set the TERMinal variable to vt100 using the setenv command? _________________________________________________ source forces the C shell to read commands from a file. Its format is source file time causes the command to be timed. Its format is time command-list Use this command to find out how long a directory listing (with -la) takes. Enter the time taken in the space below. _________________________________________________ unalias removes aliases from the C shell. The command to remove the alias for cd is _________________________________________________ unset removes the C shell variable definition.

Using The argv Variable


A csh command script is interpreted by typing % csh script argument ... The C shell places arguments in an array called argv. These arguments are accessed using this array. Some examples appear below.
$ $ $ # $rv1 ag[] (ag) $rv ;poesnme o cretsel rcs ubr f urn hl ;nme o agmns ubr f ruet ;vleo frtselagmn au f is hl ruet ;alteagmns l h ruet

C Shell Statements
The following summary lists some of the C shell statements available in the C shell.
CniinlTss odtoa et
uva.ulb.ac.be/cit_courseware/unix/unix16.htm 3/7

2/15/13

devices, terminals and csh

= = ! = & & | |

;eult ts qaiy et ;nteul o qas ;lgclad oia n ;lgclo oia r

Fl Tss ie et - fl ;ts wehrfl eit (aea Bun sel seapni frmr) e ie et hte ie xss sm s ore hl, e pedx o oe Flnm/xeso ieaeEtnin : r ;etatro o teflnm xrc ot f h ieae : e ;etatetnino flnm xrc xeso f ieae Fo Cnrl lw oto i (epeso )te f xrsin hn cmad omns .. . edf ni i (epeso )te f xrsin hn cmad omns .. . es i (epeso )te le f xrsin hn cmad omns .. . edf ni wie epeso ) hl( xrsin cmad omns ed n sic(wr ) wth od cs sr: ae t1 cmad omns bekw ras cs sr: ae tn cmad omns bekw ras dfut eal: cmad omns bekw ras edw ns frahi(ag) oec $rv cmad omns ed n bek ra; cniu; otne ei; xt lo: op cmad omns gt lo oo op

C Shell Scripts
uva.ulb.ac.be/cit_courseware/unix/unix16.htm 4/7

2/15/13

devices, terminals and csh

C Shell scripts must begin with the # symbol. Enter the following C shell script, naming it cscript1. Use vi editor to create the file.
#ccit,cpe ag[]t ag[]i ag[]de ntarayeit srp1 ois rv1 o rv2 f rv2 os o led xs i ($ag ! 2)te f #rv = hn eh "sg:cit sciedtie co uae ls1 rfl sfl" ei xt es le i (- $rv2 )te f e ag[] hn eh "ag[]arayeit.Fl ntcpe" co $rv2 led xss ie o oid es le c $rv1 $rv2 p ag[] ag[] eh "ag[]cpe t $rv2" co $rv1 oid o ag[] edf ni edf ni

Test the above script by typing the following commands. % csh cscript1 .profle cscript1 % csh cscript1 cscript1 csrcipt2 What was the message printed when the first command was typed? _________________________________________________ What was the message printed when the second command was typed? _________________________________________________

Exercise 2: csh Script 2


Write a C shell script which prints out whether the argument(s) supplied are files or directories or non-existent (see appendix reference for file tests, hint: use cscript1 as a basis for writing this script). Attach a printout of the script to this exercise booklet before handing in for assessment.

Handling Interrupts
The following example shows how to catch interrupts in a C shell script file.
#ccit cthn itrut srp3 acig nerps oircthn nt acit #cet vral iadeut t 1 rae aibe n qae o sti1 e = #mk atmoayfl ae eprr ie c srp3> tm$ p cit ! ep$ #etranvredn wielo ne ee nig hl op wie(i= 1) hl = :d ntigbtwi fra itrut o ohn u at o n nerp ed n
uva.ulb.ac.be/cit_courseware/unix/unix16.htm 5/7

2/15/13

devices, terminals and csh

#hr i itruthnlr rmv tm fl,ei srp ee s nerp ade, eoe ep ie xt cit cthn: acit r tm$ m ep$ ei() xt1

Review 5: csh
What is the output of the following C shell script. set i='/mnt/foo.bar' echo $i $i:r $i:e _________________________________________________ What does the following command assign to the variable 'pd'. set pd=`pwd` _________________________________________________ What does the 'repeat' command do? _________________________________________________ How do you exit the C shell if the variable 'ignoreeof' has been set? _________________________________________________

To Finish
Exit the C shell. Attach copies of your shell scripts to this booklet. Remove all your work files created for these modules (except any files beginning with a dot, eg, .profile). Logout of the Unix system. Attach a front declaration sheet and place this assignment in the assignment box.

C Shell Command Reference


Snatcmtcaatr ytci eahrces ; sprtscmad t b eeue sqetal eaae omns o e xctd eunily | sprtscmad i appln eaae omns n ieie ( ) bakt epesosadvral vle rces xrsin n aibe aus & floscmad t b eeue wtotwiigfrcmlto olw omns o e xctd ihu atn o opein

uva.ulb.ac.be/cit_courseware/unix/unix16.htm

6/7

2/15/13

devices, terminals and csh

Flnm mtcaatr ieae eahrces / sprtscmoet o aflnmspt eaae opnns f ieae ah . sprtsro prso aflnm fo etnin eaae ot at f ieae rm xesos ? epnincaatrmthn aysnl caatr xaso hrce acig n ige hrce * epnincaatrmthn aysqec o caatr xaso hrce acig n eune f hrces [ ] epninsqec mthn aysnl caatrfo asto caatr xaso eune acig n ige hrce rm e f hrces ~ ue a tebgnigo aflnm t idct hm drcois sd t h einn f ieae o niae oe ietre { } ue t seiygop o agmnswt cmo prs sd o pcf rus f ruet ih omn at

Qoainmtcaatr utto eahrces \ peet mt-enn o tefloigsnl caatr rvns eamaig f h olwn ige hrce ' peet mt-enn o agopo caatr rvns eamaig f ru f hrces " lk ' btalw vral adcmadepeso ie , u los aibe n omn xrsin

Iptotu mtcaatr nu/upt eahrces < idctsrdrce ipt niae eietd nu > idctsrdrce otu niae eietd upt

Epninsbttto mtcaatr xaso/usiuin eahrces $ idctsvral sbttto niae aibe usiuin ! idctshsoysbttto niae itr usiuin : peee sbttto mdfes rcds usiuin oiir ^ ue i seilfrso hsoysbttto sd n pca om f itr usiuin ` idctscmadsbttto niae omn usiuin

Ohrmtcaatr te eahrces # bgn srthflnms idctsCselcmet eis cac ieae; niae hl omns peie oto (lg agmnst cmad rfxs pin fa) ruet o omns

Fl Tss ie et e fl eit ie xss d drcoy ietr f panfl li ie o onrhp wesi r ra acs ed ces w wieacs rt ces x eeueacs xct ces z zr lnt sz eo egh ie

Copyright Brian Brown, 1988-2000. All rights reserved.

uva.ulb.ac.be/cit_courseware/unix/unix16.htm

7/7

You might also like