Parent Directory
|
Revision Log
Revision 1 - (view) (download)
| 1 : | root | 1 | This is unix.info, produced by makeinfo version 4.0 from unix.texinfo. |
| 2 : | |||
| 3 : | |||
| 4 : | File: unix.info, Node: Top, Next: Comment, Prev: (dir), Up: (dir) | ||
| 5 : | |||
| 6 : | UNIX | ||
| 7 : | **** | ||
| 8 : | |||
| 9 : | Foreword | ||
| 10 : | ******** | ||
| 11 : | |||
| 12 : | This is a revised version of the UNIX compendium which is available | ||
| 13 : | in printed form and online via the WWW and info hypertext readers. It | ||
| 14 : | forms the basis for a one or two semester course in UNIX. The most | ||
| 15 : | up-to-date version of this manual can be found at | ||
| 16 : | |||
| 17 : | http://www.iu.hioslo.no/~mark/unix/unix.html. | ||
| 18 : | |||
| 19 : | It is a reference guide which contains enough to help you to find | ||
| 20 : | what you need from other sources. It is not (and probably can never be) | ||
| 21 : | a complete and self-contained work. Certain topics are covered in more | ||
| 22 : | detail than others. Some topics are included for future reference and | ||
| 23 : | are not intended to be part of an introductory course, but will | ||
| 24 : | probably be useful later. The chapter on X11 programming has been | ||
| 25 : | deleted for the time being. | ||
| 26 : | |||
| 27 : | Comments to Mark.Burgess@iu.hioslo.no | ||
| 28 : | |||
| 29 : | Oslo, June 1996 | ||
| 30 : | |||
| 31 : | * Menu: | ||
| 32 : | |||
| 33 : | * Comment:: | ||
| 34 : | * Overview:: | ||
| 35 : | * Getting started:: | ||
| 36 : | * The login environment:: | ||
| 37 : | * Files and access:: | ||
| 38 : | * C shell:: | ||
| 39 : | * Bourne shell:: | ||
| 40 : | * Perl:: | ||
| 41 : | * WWW and CGI programming:: | ||
| 42 : | * C programming:: | ||
| 43 : | * Network programming:: | ||
| 44 : | * Programming idioms:: | ||
| 45 : | * Variable Index:: | ||
| 46 : | * Concept Index:: | ||
| 47 : | |||
| 48 : | |||
| 49 : | File: unix.info, Node: Comment, Next: Overview, Prev: Top, Up: Top | ||
| 50 : | |||
| 51 : | Welcome | ||
| 52 : | ******* | ||
| 53 : | |||
| 54 : | If you are coming to unix for the first time, from a Windows or | ||
| 55 : | MacIntosh environment, be prepared for a rather different culture than | ||
| 56 : | the one you are used to. Unix is not about `products' and off-the-shelf | ||
| 57 : | software, it is about open standards, free software and the ability to | ||
| 58 : | change just about everything. | ||
| 59 : | |||
| 60 : | * What you personally might perceive as user friendliness in other | ||
| 61 : | systems, others might perceive as annoying time wasting. Unix | ||
| 62 : | offers you just about every level of friendliness and | ||
| 63 : | unfriendliness, if you choose your programs right. In this book, | ||
| 64 : | we take the programmer's point of view. | ||
| 65 : | |||
| 66 : | * Unix is about functionality, not about simplicity. Be prepared for | ||
| 67 : | powerful, not necessarily `simple' solutions. | ||
| 68 : | |||
| 69 : | |||
| 70 : | You should approach Unix the way you should approach any new system: | ||
| 71 : | with an open mind. The journey begins... | ||
| 72 : | |||
| 73 : | |||
| 74 : | File: unix.info, Node: Overview, Next: Getting started, Prev: Comment, Up: Top | ||
| 75 : | |||
| 76 : | Overview | ||
| 77 : | ******** | ||
| 78 : | |||
| 79 : | _In this manual the word "host" is used to refer to a single computer | ||
| 80 : | system - i.e. a single machine which has a name termed its "hostname"._ | ||
| 81 : | |||
| 82 : | * Menu: | ||
| 83 : | |||
| 84 : | * What is unix?:: | ||
| 85 : | * Flavours of unix:: | ||
| 86 : | * How to use this reference guide:: | ||
| 87 : | * NEVER-DOs in UNIX:: | ||
| 88 : | * What you should know before starting:: | ||
| 89 : | * The superuser and nobody:: | ||
| 90 : | * The file hierarchy :: | ||
| 91 : | * Symbolic links:: | ||
| 92 : | * Hard links:: | ||
| 93 : | |||
| 94 : | |||
| 95 : | File: unix.info, Node: What is unix?, Next: Flavours of unix, Prev: Overview, Up: Overview | ||
| 96 : | |||
| 97 : | What is unix? | ||
| 98 : | ============= | ||
| 99 : | |||
| 100 : | Unix is one of the most important operating system in use today, | ||
| 101 : | perhaps even _the_ most important. Since its invention around the | ||
| 102 : | beginning of the 1970s it has been an object of continual research and | ||
| 103 : | development. UNIX is not popular because it is the best operating | ||
| 104 : | system one could imagine, but because it is an extremely flexible | ||
| 105 : | system which is easy to extend and modify. It is an ideal platform for | ||
| 106 : | developing new ideas. | ||
| 107 : | |||
| 108 : | Much of the success of UNIX may be attributed to the rapid pace of | ||
| 109 : | its development (a development to which all of its users have been able | ||
| 110 : | to contribute) its efficiency at running programs and the many powerful | ||
| 111 : | tools which have been written for it over the years, such as the C | ||
| 112 : | programming language, `make', shell, `lex' and `yacc' and many others. | ||
| 113 : | UNIX was written by programmers for programmers. It is popular in | ||
| 114 : | situations where a lot of computing power is required and for database | ||
| 115 : | applications, where timesharing is critical. In contrast to some | ||
| 116 : | operating systems, UNIX performs equally well on large scale computers | ||
| 117 : | (with many processors) and small computers which fit in your suitcase! | ||
| 118 : | |||
| 119 : | All of the basic mechanisms required of a multi-user operating system | ||
| 120 : | are present in UNIX. During the last few years it has become ever more | ||
| 121 : | popular and has formed the basis of newer, though less mature, systems | ||
| 122 : | like NT. One reason for this that now computers have now become | ||
| 123 : | powerful enough to run UNIX effectively. UNIX places burdens on the | ||
| 124 : | resources of a computer, since it expects to be able to run potentially | ||
| 125 : | many programs simultaneously. | ||
| 126 : | |||
| 127 : | If you are coming to UNIX from DOS you may well be used to using | ||
| 128 : | applications software or helpful interactive utilities to solve every | ||
| 129 : | problem. UNIX is not usually like this: the operating system has much | ||
| 130 : | greater functionality and provides the possibilities for making your | ||
| 131 : | own, so it is less common to find applications software which implements | ||
| 132 : | the same things. UNIX has long been in the hands of academics who are | ||
| 133 : | used to making their own applications or writing their own programs, | ||
| 134 : | whereas as the DOS world has been driven by businesses who are willing | ||
| 135 : | to spend money on software. For that reason commerical UNIX software is | ||
| 136 : | often very expensive and therefore not available at this college. On the | ||
| 137 : | other hand, the flexibility of UNIX means that it is easy to write | ||
| 138 : | programs and it is possible to fetch gigabytes of _free software_ from | ||
| 139 : | the internet to suit your needs. It may not look like what you are used | ||
| 140 : | to on your PC, but then you have to remember that UNIX users are a | ||
| 141 : | different kind of animal altogether | ||
| 142 : | |||
| 143 : | Like all operating systems, UNIX has many faults. The biggest problem | ||
| 144 : | for any operating system is that it evolves without being redesigned. | ||
| 145 : | Operating systems evolve as more and more patches and hacks are applied | ||
| 146 : | to solve day-to-day problems. The result is either a mess which works | ||
| 147 : | somehow (like UNIX) or a blank refusal to change (like DOS or | ||
| 148 : | MacIntosh). From a practical perspective, Unix is important and | ||
| 149 : | successful because it is a multi-process system which | ||
| 150 : | |||
| 151 : | * has an enormous functionality built in, and the capacity to adapt | ||
| 152 : | itself to changing technologies, | ||
| 153 : | |||
| 154 : | * is relatively portable, | ||
| 155 : | |||
| 156 : | * is good at sharing resources (but not so good at security), | ||
| 157 : | |||
| 158 : | * has tools which are each developed to do _one thing well_, | ||
| 159 : | |||
| 160 : | * allows these tools to be combined in every imaginable way, using | ||
| 161 : | pipes and channeling of data streams, | ||
| 162 : | |||
| 163 : | * incorporates networking almost trivially, because all the right | ||
| 164 : | mechanisms are already there for providing services and sharing, | ||
| 165 : | building client-server pairs etc,. | ||
| 166 : | |||
| 167 : | * it is very adaptable and is often used to develop new ideas | ||
| 168 : | because of the rich variety of tools it possesses. | ||
| 169 : | |||
| 170 : | |||
| 171 : | Unix has some problems: it is old, it contains a lot of rubbish | ||
| 172 : | which no one ever bothered to throw away. Although it develops quickly | ||
| 173 : | (at light speed compared to either DOS or MacIntosh) the user interface | ||
| 174 : | has been the slowest thing to change. Unix is not user-friendly for | ||
| 175 : | beginners, it is user-friendly for advanced users: it is made for users | ||
| 176 : | who _know_ about computing. It sometimes makes simple things difficult, | ||
| 177 : | but above all it makes things possible! | ||
| 178 : | |||
| 179 : | The aim of this introduction is to | ||
| 180 : | |||
| 181 : | * introduce the unix system basics and user interface, | ||
| 182 : | |||
| 183 : | * develop the unix philosophy of using and combining tools, | ||
| 184 : | |||
| 185 : | * learn how to make new tools and write software, | ||
| 186 : | |||
| 187 : | * learn how to understand existing software. | ||
| 188 : | |||
| 189 : | |||
| 190 : | To accomplish this task, we must first learn something about the shell | ||
| 191 : | (the way in which UNIX starts programs). Later we shall learn how to | ||
| 192 : | solve more complex problems using Perl and C. Each of these is a | ||
| 193 : | language which can be used to put UNIX to work. We must also learn when | ||
| 194 : | to use which tool, so that we do not waste time and effort. Typical | ||
| 195 : | uses for these different interfaces are | ||
| 196 : | |||
| 197 : | _shell_ | ||
| 198 : | Command line interaction, making scripts which perform simple jobs | ||
| 199 : | such as running programs in batch, installing new software, simple | ||
| 200 : | system configuration and administration. | ||
| 201 : | |||
| 202 : | _perl_ | ||
| 203 : | Text interpretation, text formatting, output filters, mail robots, | ||
| 204 : | WWW cgi (common gateway interface) scripts in forms, password | ||
| 205 : | testing, simple database manipulation, simple client-server | ||
| 206 : | applications. | ||
| 207 : | |||
| 208 : | _C_ | ||
| 209 : | Nearly all of UNIX is written in C. Any program which cannot be | ||
| 210 : | solved quickly using shell or perl can be written in C. One | ||
| 211 : | advantage is that C is a compiled language and many simple errors | ||
| 212 : | can be caught at compile time. | ||
| 213 : | |||
| 214 : | Much of UNIX's recent popularity has been a result of its networking | ||
| 215 : | abilities: unix is the backbone of the internet. No other widely | ||
| 216 : | available system could keep the internet alive today. | ||
| 217 : | |||
| 218 : | Once you have mastered the unix interface and philosophy you will | ||
| 219 : | find that i) the PC and MacIntosh window environments seem to be easy | ||
| 220 : | to use, but simplistic and primitive by comparison; ii) UNIX is far | ||
| 221 : | from being the perfect operating system--it has a whole different set | ||
| 222 : | of problems and flaws. | ||
| 223 : | |||
| 224 : | The operating system of the future will not be UNIX as we see it | ||
| 225 : | today, nor will is be DOS or MacIntosh, but one thing is for certain: | ||
| 226 : | it will owe a lot to the UNIX operating system and will contain many of | ||
| 227 : | the tools and mechanisms we shall describe below. | ||
| 228 : | |||
| 229 : | |||
| 230 : | File: unix.info, Node: Flavours of unix, Next: How to use this reference guide, Prev: What is unix?, Up: Overview | ||
| 231 : | |||
| 232 : | Flavours of unix | ||
| 233 : | ================ | ||
| 234 : | |||
| 235 : | Unix is not a single operating system. It has branched out in many | ||
| 236 : | different directions since it was introduced by AT&T. The most | ||
| 237 : | important `fork()' in its history happened early on when the university | ||
| 238 : | of Berkeley, California created the BSD (Berkeley Software | ||
| 239 : | Distribution), adding network support and the C-shell. | ||
| 240 : | |||
| 241 : | Here are some of the most common implementations of unix. | ||
| 242 : | |||
| 243 : | _BSD:_ | ||
| 244 : | Berkeley, BSD | ||
| 245 : | |||
| 246 : | _SunOS:_ | ||
| 247 : | Sun Microsystems, BSD/sys 5 | ||
| 248 : | |||
| 249 : | _Solaris:_ | ||
| 250 : | Sun Microsystems, Sys 5/BSD | ||
| 251 : | |||
| 252 : | _Ultrix:_ | ||
| 253 : | Digital Equipment Corperation, BSD | ||
| 254 : | |||
| 255 : | _OSF 1:_ | ||
| 256 : | Digital Equipment Corperation, BSD/sys 5 | ||
| 257 : | |||
| 258 : | _HPUX:_ | ||
| 259 : | Hewlett-Packard, Sys 5 | ||
| 260 : | |||
| 261 : | _AIX:_ | ||
| 262 : | IBM, Sys 5 / BSD | ||
| 263 : | |||
| 264 : | _IRIX:_ | ||
| 265 : | Silicon Graphics, Sys 5 | ||
| 266 : | |||
| 267 : | _GNU/Linux:_ | ||
| 268 : | GNU, BSD/Posix | ||
| 269 : | |||
| 270 : | |||
| 271 : | File: unix.info, Node: How to use this reference guide, Next: NEVER-DOs in UNIX, Prev: Flavours of unix, Up: Overview | ||
| 272 : | |||
| 273 : | How to use this reference guide | ||
| 274 : | =============================== | ||
| 275 : | |||
| 276 : | This programming guide is something between a user manual and a | ||
| 277 : | tutorial. The information contained here should be sufficient to get | ||
| 278 : | you started with the unix system, but it is far from complete. | ||
| 279 : | |||
| 280 : | To use this programming guide, you will need to work through the | ||
| 281 : | basics from each chapter. You will find that there is much more | ||
| 282 : | information here than you need straight away, so try not to be | ||
| 283 : | overwhelmed by the amount of material. Use the contents and the | ||
| 284 : | indices at the back to find the information you need. If you are | ||
| 285 : | following a one-semester UNIX course, you should probably concentrate | ||
| 286 : | on the following: | ||
| 287 : | |||
| 288 : | * The remainder of this introduction | ||
| 289 : | |||
| 290 : | * The detailed knowledge of the C shell | ||
| 291 : | |||
| 292 : | * An appreciation of the Bourne shell | ||
| 293 : | |||
| 294 : | * A detailed knowledge of Perl, guided by chapter 6. This chapter | ||
| 295 : | provides pointers on how to get started in perl. It is not a | ||
| 296 : | substitute for the perl book. | ||
| 297 : | |||
| 298 : | * Everything in chapter 7 about C programming. This chapter is | ||
| 299 : | written in note form, since it is assumed that you know a lot | ||
| 300 : | about C programming already. | ||
| 301 : | |||
| 302 : | * A sound appreciation of chapter 8 on network programming. | ||
| 303 : | |||
| 304 : | |||
| 305 : | The _only_ way to learn UNIX is to sit down and try it. As with any | ||
| 306 : | new thing, it is a pain to get started, but once you are started, you | ||
| 307 : | will probably come to agree that UNIX contains a wealth of | ||
| 308 : | possibilities, perhaps more than you had ever though was possible or | ||
| 309 : | useful! | ||
| 310 : | |||
| 311 : | One of the advantages of the UNIX system is that the entire UNIX | ||
| 312 : | manual is available on-line. You should get used to looking for | ||
| 313 : | information in the online manual pages. For instance, suppose you do | ||
| 314 : | not remember how to create a new directory, you could do the following: | ||
| 315 : | |||
| 316 : | nexus% man -k dir | ||
| 317 : | |||
| 318 : | dir ls (1) - list contents of directories | ||
| 319 : | dirname dirname (1) - strip non-directory suffix from file name | ||
| 320 : | dirs bash (1) - bash built-in commands, see bash(1) | ||
| 321 : | find find (1) - search for files in a directory hierarchy | ||
| 322 : | ls ls (1) - list contents of directories | ||
| 323 : | mkdir mkdir (1) - make directories | ||
| 324 : | pwd pwd (1) - print name of current/working directory | ||
| 325 : | rmdir rmdir (1) - remove empty directories | ||
| 326 : | |||
| 327 : | The `man -k' command looks for a keyword in the manual and lists all | ||
| 328 : | the references it finds. The command `apropos' is completely equivalent | ||
| 329 : | to `man -k'. Having discovered that the command to create a directory | ||
| 330 : | is `mkdir' you can now look up the specific manaul page on `mkdir' to | ||
| 331 : | find out how to use it: | ||
| 332 : | |||
| 333 : | man mkdir | ||
| 334 : | |||
| 335 : | Some but no all of the UNIX commands also have a help option which is | ||
| 336 : | activated with the `-h' or `--help' command-line option. | ||
| 337 : | |||
| 338 : | dax% mkdir --help | ||
| 339 : | Usage: mkdir [OPTION] DIRECTORY... | ||
| 340 : | |||
| 341 : | -p, --parents no error if existing, make parent directories as needed | ||
| 342 : | -m, --mode=MODE set permission mode (as in chmod), not 0777 - umask | ||
| 343 : | --help display this help and exit | ||
| 344 : | --version output version information and exit | ||
| 345 : | dax% | ||
| 346 : | |||
| 347 : | |||
| 348 : | File: unix.info, Node: NEVER-DOs in UNIX, Next: What you should know before starting, Prev: How to use this reference guide, Up: Overview | ||
| 349 : | |||
| 350 : | NEVER-DO's in UNIX | ||
| 351 : | ================== | ||
| 352 : | |||
| 353 : | There are some things that you should never do in UNIX. Some of these | ||
| 354 : | will cause you more serious problems than others. You can make your own | ||
| 355 : | list as you discover more. | ||
| 356 : | |||
| 357 : | * You should NEVER EVER switch off the power on a Unix computer | ||
| 358 : | unless you know what you are doing. A Unix machine is not like a | ||
| 359 : | PC running DOS. Even when you are not doing anything, the system | ||
| 360 : | is working in the background. If you switch off the power, you | ||
| 361 : | could interrupt the system while it is writing to the disk drive | ||
| 362 : | and destroy your disk. You must also remember that several users | ||
| 363 : | might be using the system even though you cannot see them: they do | ||
| 364 : | not have to be sitting at the machine, they could be logged in | ||
| 365 : | over the network. If you switch off the power, you might ruin | ||
| 366 : | their valuable work. | ||
| 367 : | |||
| 368 : | * Once you have deleted a UNIX file using `rm' it is impossible to | ||
| 369 : | recover it! Don't use wildcards with `rm' without thinking quite | ||
| 370 : | carefully about what you are doing! It has happened to very many | ||
| 371 : | users throughout the history of UNIX that one tries to type | ||
| 372 : | rm *~ | ||
| 373 : | but instead, by a slip of the hand, one writes | ||
| 374 : | rm * ~ | ||
| 375 : | Unix then takes these wildcards in turn, so that the first command | ||
| 376 : | is `rm *' which deletes all of your files! BE CAREFUL! | ||
| 377 : | |||
| 378 : | * Don't ever call a program or an important file `core'. Many | ||
| 379 : | scripts go around deleting files called `core' because the, when a | ||
| 380 : | program crashes, UNIX dumps the entire kernel image to a file | ||
| 381 : | called `core' and these files use up a lot of disk space. If you | ||
| 382 : | call a file `core' it might get deleted! | ||
| 383 : | |||
| 384 : | * Don't call test programs `test'. There is a UNIX command which is | ||
| 385 : | already called test and chances are that when you try to run your | ||
| 386 : | program you will start the UNIX command instead. This can cause a | ||
| 387 : | lot of confusion because the UNIX command doesn't seem to do very | ||
| 388 : | much at all! | ||
| 389 : | |||
| 390 : | |||
| 391 : | File: unix.info, Node: What you should know before starting, Next: The superuser and nobody, Prev: NEVER-DOs in UNIX, Up: Overview | ||
| 392 : | |||
| 393 : | What you should know before starting | ||
| 394 : | ==================================== | ||
| 395 : | |||
| 396 : | * Menu: | ||
| 397 : | |||
| 398 : | * One library:: | ||
| 399 : | * Unix commands are files:: | ||
| 400 : | * Kernel and shell:: | ||
| 401 : | * The role of C:: | ||
| 402 : | * Stdin:: | ||
| 403 : | |||
| 404 : | |||
| 405 : | File: unix.info, Node: One library, Next: Unix commands are files, Prev: What you should know before starting, Up: What you should know before starting | ||
| 406 : | |||
| 407 : | One library: several interfaces | ||
| 408 : | ------------------------------- | ||
| 409 : | |||
| 410 : | The core of unix is the library of functions (written in C) which | ||
| 411 : | access the system. Everything you do on a unix system goes through this | ||
| 412 : | set of functions. However, you can choose your own interface to these | ||
| 413 : | library functions. Unix has very many different interfaces to its | ||
| 414 : | libraries in the form of languages and command interpreters. | ||
| 415 : | |||
| 416 : | You can use the functions directly in C, or you can use command | ||
| 417 : | programs like `ls', `cd' etc. These functions just provide a simple | ||
| 418 : | user interface to the C calls. You can also use a variety of `script' | ||
| 419 : | languages: C-shell, Bourne shell, Perl, Tcl, scheme. You choose the | ||
| 420 : | interface which solves your problem most easily. | ||
| 421 : | |||
| 422 : | * Menu: | ||
| 423 : | |||
| 424 : | * Unix commands are files:: | ||
| 425 : | * Kernel and shell:: | ||
| 426 : | * The role of C:: | ||
| 427 : | * Stdin:: | ||
| 428 : | |||
| 429 : | |||
| 430 : | File: unix.info, Node: Unix commands are files, Next: Kernel and shell, Prev: One library, Up: What you should know before starting | ||
| 431 : | |||
| 432 : | Unix commands are files | ||
| 433 : | ----------------------- | ||
| 434 : | |||
| 435 : | With the exception of a few simple commands which are built into the | ||
| 436 : | command interpreter (shell), all unix commands and programs consist of | ||
| 437 : | executable files. In other words, there is a separate executable file | ||
| 438 : | for each command. This makes it extremely simple to add new commands to | ||
| 439 : | the system. One simply makes a program with the desired name and places | ||
| 440 : | it in the appropriate directory. | ||
| 441 : | |||
| 442 : | Unix commands live in special directories (usually called `bin' for | ||
| 443 : | binary files). The location of these directories is recorded in a | ||
| 444 : | variable called `path' or `PATH' which is used by the system to search | ||
| 445 : | for binaries. We shall return to this in more detail in later chapters. | ||
| 446 : | |||
| 447 : | |||
| 448 : | File: unix.info, Node: Kernel and shell, Next: The role of C, Prev: Unix commands are files, Up: What you should know before starting | ||
| 449 : | |||
| 450 : | Kernel and Shell | ||
| 451 : | ---------------- | ||
| 452 : | |||
| 453 : | Since users cannot command the kernel directly, UNIX has a command | ||
| 454 : | language known as the _shell_. The word shell implies a layer around the | ||
| 455 : | kernel. A shell is a user interface, or command interpreter. | ||
| 456 : | |||
| 457 : | There are two main versions of the shell, plus a number of | ||
| 458 : | enhancements. | ||
| 459 : | |||
| 460 : | _/bin/sh_ | ||
| 461 : | The Bourne Shell. The shell is most often used for writing system | ||
| 462 : | scripts. It is part of the original unix system. | ||
| 463 : | |||
| 464 : | _/bin/csh_ | ||
| 465 : | The C-shell. This was added to unix by the Berkeley workers. The | ||
| 466 : | commands and syntax resemble C code. C-shell is better suited for | ||
| 467 : | interactive work than the Bourne shell. | ||
| 468 : | |||
| 469 : | The program `tcsh' is a public-domain enhancement of the csh and is | ||
| 470 : | in common use. Two improved versions of the Bourne shell also exist: | ||
| 471 : | `ksh', the Korn shell and `bash', the Bourne-again shell. | ||
| 472 : | |||
| 473 : | Although the shells are mainly tools for typing in commands (which | ||
| 474 : | are excutable files to be loaded and run), they contain features such as | ||
| 475 : | aliases, a command history, wildcard-expansions and job control | ||
| 476 : | functions which provide a comfortable user environment. | ||
| 477 : | |||
| 478 : | |||
| 479 : | File: unix.info, Node: The role of C, Next: Stdin, Prev: Kernel and shell, Up: What you should know before starting | ||
| 480 : | |||
| 481 : | The role of C | ||
| 482 : | ------------- | ||
| 483 : | |||
| 484 : | Most of the unix kernel and daemons are written in the C programming | ||
| 485 : | language (1). Calls to the kernel and to services are made through | ||
| 486 : | functions in the standard C library. The commands like `chmod', `mkdir' | ||
| 487 : | and `cd' are all C functions. The binary files of the same name | ||
| 488 : | `/bin/chmod', `/bin/mkdir' etc. are just trivial "wrapper" programs for | ||
| 489 : | these C functions. | ||
| 490 : | |||
| 491 : | Until _Solaris 2_, the C compiler was a standard part of the UNIX | ||
| 492 : | operating system, thus C is the most natural language to program in in | ||
| 493 : | a UNIX environment. Some tools are provided for C programmers: | ||
| 494 : | |||
| 495 : | _dbx_ | ||
| 496 : | A symbolic debugger. Also _gdb_, _xxgdb_ _ddd_. | ||
| 497 : | |||
| 498 : | _make_ | ||
| 499 : | A development tool for compiling large programs. | ||
| 500 : | |||
| 501 : | _lex_ | ||
| 502 : | A `lexer'. A program which generates C code to recognize words of | ||
| 503 : | text. | ||
| 504 : | |||
| 505 : | _yacc_ | ||
| 506 : | A `parser'. This is a tool which generates C code for checking the | ||
| 507 : | syntax of groups of textual words. | ||
| 508 : | |||
| 509 : | _rpcgen_ | ||
| 510 : | A protocol compiler which generates C code from a higher level | ||
| 511 : | langauge, for programming RPC applications. | ||
| 512 : | |||
| 513 : | ---------- Footnotes ---------- | ||
| 514 : | |||
| 515 : | (1) In particular they are written in Kernighan and Ritchie C, not | ||
| 516 : | the newer ANSI standard C. | ||
| 517 : | |||
| 518 : | |||
| 519 : | File: unix.info, Node: Stdin, Prev: The role of C, Up: What you should know before starting | ||
| 520 : | |||
| 521 : | Stdin, stdout, stderr | ||
| 522 : | --------------------- | ||
| 523 : | |||
| 524 : | Unix has three logical _streams_ or _files_ which are always open | ||
| 525 : | and are available to any program. | ||
| 526 : | |||
| 527 : | _stdin_ | ||
| 528 : | The standard input - file descriptor 0. | ||
| 529 : | |||
| 530 : | _stdout_ | ||
| 531 : | The standard output - file descriptor 1. | ||
| 532 : | |||
| 533 : | _stderr_ | ||
| 534 : | The standard error - file descriptor 2. | ||
| 535 : | |||
| 536 : | The names are a part of the C language and are defined as pointers | ||
| 537 : | of type `FILE'. | ||
| 538 : | |||
| 539 : | #include <stdio.h> | ||
| 540 : | |||
| 541 : | /* FILE *stdin, *stdout, *stderr; */ | ||
| 542 : | |||
| 543 : | fprintf(stderr,"This is an error message!\n"); | ||
| 544 : | |||
| 545 : | The names are `logical' in the sense that they do not refer to a | ||
| 546 : | particular device, or a particular place for information to come from | ||
| 547 : | or go. Their role is analogous to the `.' and `..' directories in the | ||
| 548 : | filesystem. Programs can write to these files without worrying about | ||
| 549 : | where the information comes from or goes to. The user can personally | ||
| 550 : | define these places by _redirecting standard I/O_. This is discussed in | ||
| 551 : | the next chapter. | ||
| 552 : | |||
| 553 : | A separate stream is kept for error messages so that error output | ||
| 554 : | does not get mixed up with a program's intended output. | ||
| 555 : | |||
| 556 : | |||
| 557 : | File: unix.info, Node: The superuser and nobody, Next: The file hierarchy, Prev: What you should know before starting, Up: Overview | ||
| 558 : | |||
| 559 : | The superuser (root) and _nobody_ | ||
| 560 : | ================================= | ||
| 561 : | |||
| 562 : | When logged onto a UNIX system directly, the user whose name is | ||
| 563 : | `root' has unlimited access to the files on the system. `root' can also | ||
| 564 : | become any other user without having to give a password. `root' is | ||
| 565 : | reserved for the system administrator or _trusted users_. | ||
| 566 : | |||
| 567 : | Certain commands are forbidden to normal users. For example, a | ||
| 568 : | regular user should not be able to halt the system, or change the | ||
| 569 : | ownership of files (see next paragraph). These things are reserved for | ||
| 570 : | the `root' or _superuser_. | ||
| 571 : | |||
| 572 : | In a networked environment, `root' has no automatic authority on | ||
| 573 : | remote machines. This is to prevent the system administrator of one | ||
| 574 : | machine in Canada from being able to edit files on another in China. He | ||
| 575 : | or she must log in directly and supply a password in order to gain | ||
| 576 : | access privileges. On a network where files are often accessible in | ||
| 577 : | principle to anyone, the username `root' gets mapped to the user | ||
| 578 : | `nobody', who has no rights at all. | ||
| 579 : | |||
| 580 : | |||
| 581 : | File: unix.info, Node: The file hierarchy, Next: Symbolic links, Prev: The superuser and nobody, Up: Overview | ||
| 582 : | |||
| 583 : | The file hierarchy | ||
| 584 : | ================== | ||
| 585 : | |||
| 586 : | Unix has a hierachical filesystem, which makes use of directories and | ||
| 587 : | sub-directories to form a tree. The root of the tree is called the root | ||
| 588 : | filesystem or `/'. Although the details of where every file is located | ||
| 589 : | differ for different versions of unix, some basic features are the | ||
| 590 : | same. The main sub-directories of the root directory together with the | ||
| 591 : | most important file are shown in the figure. Their contents are as | ||
| 592 : | follows. | ||
| 593 : | |||
| 594 : | `/bin' | ||
| 595 : | Executable (binary) programs. On most systems this is a separate | ||
| 596 : | directory to /usr/bin. In SunOS, this is a pointer (link) to | ||
| 597 : | /usr/bin. | ||
| 598 : | |||
| 599 : | `/etc' | ||
| 600 : | Miscellaneous programs and configuration files. This directory has | ||
| 601 : | become very messy over the history of UNIX and has become a dumping | ||
| 602 : | ground for almost anything. Recent versions of unix have begun to | ||
| 603 : | tidy up this directory by creating subdirectories `/etc/mail', | ||
| 604 : | `/etc/services' etc! | ||
| 605 : | |||
| 606 : | `/usr' | ||
| 607 : | This contains the main meat of UNIX. This is where application | ||
| 608 : | software lives, together with all of the basic libraries used by | ||
| 609 : | the OS. | ||
| 610 : | |||
| 611 : | `/usr/bin' | ||
| 612 : | More executables from the OS. | ||
| 613 : | |||
| 614 : | `/usr/local' | ||
| 615 : | This is where users' custom software is normally added. | ||
| 616 : | |||
| 617 : | `/sbin' | ||
| 618 : | A special area for statically linked system binaries. They are | ||
| 619 : | placed here to distinguish commands used solely by the system | ||
| 620 : | administrator from user commands and so that they lie on the | ||
| 621 : | system root partition where they are guaranteed to be accessible | ||
| 622 : | during booting. | ||
| 623 : | |||
| 624 : | `/sys' | ||
| 625 : | This holds the configuration data which go to build the system | ||
| 626 : | kernel. (See below.) | ||
| 627 : | |||
| 628 : | `/export' | ||
| 629 : | Network servers only use this. This contains the disk space set | ||
| 630 : | aside for client machines which do not have their own disks. It is | ||
| 631 : | like a `virtual disk' for diskless clients. | ||
| 632 : | |||
| 633 : | `/dev, /devices' | ||
| 634 : | A place where all the `logical devices' are collected. These are | ||
| 635 : | called `device nodes' in unix and are created by `mknod'. Logical | ||
| 636 : | devices are UNIX's official entry points for writing to devices. | ||
| 637 : | For instance, `/dev/console' is a route to the system console, | ||
| 638 : | while `/dev/kmem' is a route for reading kernel memory. Device | ||
| 639 : | nodes enable devices to be treated as though they were files. | ||
| 640 : | |||
| 641 : | `/home' | ||
| 642 : | (Called _/users_ on some systems.) Each user has a separate login | ||
| 643 : | directory where files can be kept. These are normally stored under | ||
| 644 : | `/home' by some convention decided by the system administrator. | ||
| 645 : | |||
| 646 : | `/var' | ||
| 647 : | System 5 and mixed systems have a separate directory for spooling. | ||
| 648 : | Under old BSD systems, `/usr/spool' contains spool queues and | ||
| 649 : | system data. `/var/spool' and `/var/adm' etc are used for holding | ||
| 650 : | queues and system log files. | ||
| 651 : | |||
| 652 : | `/vmunix' | ||
| 653 : | This is the program code for the unix _kernel_ (see below). On | ||
| 654 : | HPUX systems with file is called `hp-ux'. On linux it is called | ||
| 655 : | `linux'. | ||
| 656 : | |||
| 657 : | `/kernel' | ||
| 658 : | On newer systems the kernel is built up from a number of modules | ||
| 659 : | which are placed in this directory. | ||
| 660 : | |||
| 661 : | Every unix directory contains two `virtual' directories marked by a | ||
| 662 : | single dot and two dots. | ||
| 663 : | |||
| 664 : | ls -a | ||
| 665 : | . .. | ||
| 666 : | |||
| 667 : | The single dot represents the directory one is already in (the current | ||
| 668 : | directory). The double dots mean the directory one level up the tree | ||
| 669 : | from the current location. Thus, if one writes | ||
| 670 : | |||
| 671 : | cd /usr/local | ||
| 672 : | cd .. | ||
| 673 : | |||
| 674 : | the final directory is `/usr'. The single dot is very useful in C | ||
| 675 : | programming if one wishes to read `the current directory'. Since this | ||
| 676 : | is always called `.' there is no need to keep track of what the current | ||
| 677 : | directory really is. | ||
| 678 : | |||
| 679 : | `.' and `..' are `hard links' to the true directories. | ||
| 680 : | |||
| 681 : | |||
| 682 : | File: unix.info, Node: Symbolic links, Next: Hard links, Prev: The file hierarchy, Up: Overview | ||
| 683 : | |||
| 684 : | Symbolic links | ||
| 685 : | ============== | ||
| 686 : | |||
| 687 : | A symbolic link is a pointer or an alias to another file. The command | ||
| 688 : | ln -s fromfile /other/directory/tolink | ||
| 689 : | |||
| 690 : | makes the file `fromfile' appear to exist at `/other/directory/tolink' | ||
| 691 : | simultaneously. The file is not copied, it merely appears to be a part | ||
| 692 : | of the file tree in two places. Symbolic links can be made to both | ||
| 693 : | files and directories. | ||
| 694 : | |||
| 695 : | A symbolic link is just a small file which contains the name of the | ||
| 696 : | real file one is interested in. It cannot be opened like an ordinary | ||
| 697 : | file, but may be read with the C call `readlink()' *Note lstat and | ||
| 698 : | readlink::. If we remove the file a symbolic link points to, the link | ||
| 699 : | remains - it just points nowhere. | ||
| 700 : | |||
| 701 : | |||
| 702 : | File: unix.info, Node: Hard links, Prev: Symbolic links, Up: Overview | ||
| 703 : | |||
| 704 : | Hard links | ||
| 705 : | ========== | ||
| 706 : | |||
| 707 : | A _hard link_ is a duplicate _inode_ in the filesystem which is in | ||
| 708 : | every way equivalent to the original file inode. If a file is pointed | ||
| 709 : | to by a hard link, it cannot be removed until the link is removed. If | ||
| 710 : | a file has n hard links - all of them must be removed before the file | ||
| 711 : | can be removed. The number of hard links to a file is stored in the | ||
| 712 : | filesystem _index node_ for the file. | ||
| 713 : | |||
| 714 : | |||
| 715 : | File: unix.info, Node: Getting started, Next: The login environment, Prev: Overview, Up: Top | ||
| 716 : | |||
| 717 : | Getting started | ||
| 718 : | *************** | ||
| 719 : | |||
| 720 : | If you have never met unix, or another multiuser system before, then | ||
| 721 : | you might find the idea daunting. There are several things you should | ||
| 722 : | know. | ||
| 723 : | |||
| 724 : | * Menu: | ||
| 725 : | |||
| 726 : | * Logging in:: | ||
| 727 : | * Mouse buttons:: | ||
| 728 : | * Reading mail:: | ||
| 729 : | * Simple commands:: | ||
| 730 : | * Text editing and word processing:: | ||
| 731 : | |||
| 732 : | |||
| 733 : | File: unix.info, Node: Logging in, Next: Mouse buttons, Prev: Getting started, Up: Getting started | ||
| 734 : | |||
| 735 : | Logging in | ||
| 736 : | ========== | ||
| 737 : | |||
| 738 : | Each time you use unix you must log on to the system by typing a | ||
| 739 : | username and a password. Your login name is sometimes called an | ||
| 740 : | `account' because some unix systems implement strict quotas for | ||
| 741 : | computer resources which have to be paid for with real money(1). | ||
| 742 : | |||
| 743 : | |||
| 744 : | login: mark | ||
| 745 : | password: | ||
| 746 : | |||
| 747 : | Once you have typed in your password, you are `logged on'. What | ||
| 748 : | happens then depends on what kind of system you are logged onto and | ||
| 749 : | how. If you have a colour monitor and keyboard in front of you, with a | ||
| 750 : | graphical user interface, you will see a number of windows appear, | ||
| 751 : | perhaps a menu bar. You then use a mouse and keyboard just like any | ||
| 752 : | other system. | ||
| 753 : | |||
| 754 : | This is not the only way to log onto unix. You can also log in | ||
| 755 : | remotely, from another machine, using the `telnet' or `rlogin' | ||
| 756 : | programs. If you use these programs, you will normally only get a text | ||
| 757 : | or command line interface (though graphics can still be arranged). | ||
| 758 : | |||
| 759 : | Once you have logged in, a short message will be printed (called | ||
| 760 : | Message of the Day or motd) and you will see the C-shell prompt: the | ||
| 761 : | name of the host you are logged onto followed by a percent sign, e.g. | ||
| 762 : | |||
| 763 : | |||
| 764 : | SunOS Release 5.5 Version Generic [UNIX(R) System V Release 4.0] | ||
| 765 : | Copyright (c) 1983-1995, Sun Microsystems, Inc. | ||
| 766 : | |||
| 767 : | Please report problems to sysadm@iu.hioslo.no | ||
| 768 : | |||
| 769 : | dax% | ||
| 770 : | |||
| 771 : | Remember that every unix machine is a separate entity: it is not like | ||
| 772 : | logging onto a PC system where you log onto the `network' i.e. the PC | ||
| 773 : | file server. Every unix machine is a server. The network, in unix-land, | ||
| 774 : | has lots of players. | ||
| 775 : | |||
| 776 : | The first thing you should do once you have logged on is to set a | ||
| 777 : | reliable password. A poor password might be okay on a PC which is not | ||
| 778 : | attached to a large network, but once you are attached to the internet, | ||
| 779 : | you have to remember that the whole world will be trying to crack your | ||
| 780 : | password. Don't think that no one will bother: some people really have | ||
| 781 : | nothing better to do. A password should not contain any word that could | ||
| 782 : | be in a list of words (in any language), or be a simple concatenation of | ||
| 783 : | a word and a number (e.g. mark123). It takes seconds to crack such a | ||
| 784 : | password. Choose instead something which is easy to remember. Feel free | ||
| 785 : | to use the PIN number from your bankers card in your password! This will | ||
| 786 : | leave you with fewer things to remember. e.g. Ma9876rk). Passwords can | ||
| 787 : | be up to eight characters long. | ||
| 788 : | |||
| 789 : | Some sites allow you to change your password anywhere. Other sites | ||
| 790 : | require you to log onto a special machine to change your password: | ||
| 791 : | |||
| 792 : | |||
| 793 : | dax% | ||
| 794 : | dax% passwd | ||
| 795 : | Change your password on host nexus | ||
| 796 : | You cannot change it here | ||
| 797 : | dax% rlogin nexus | ||
| 798 : | password: ****** | ||
| 799 : | |||
| 800 : | nexus% passwd | ||
| 801 : | Changing password for mark | ||
| 802 : | Enter login password: ******** | ||
| 803 : | Enter new password: ******** | ||
| 804 : | Reenter new passwd: ******** | ||
| 805 : | |||
| 806 : | You will be prompted for your old password and your new password twice. | ||
| 807 : | If your network is large, it might take the system up to an hour or two | ||
| 808 : | to register the change in your password, so don't forget the old one | ||
| 809 : | right away! | ||
| 810 : | |||
| 811 : | * Menu: | ||
| 812 : | |||
| 813 : | * Mouse buttons:: | ||
| 814 : | |||
| 815 : | ---------- Footnotes ---------- | ||
| 816 : | |||
| 817 : | (1) This is seldom true these days. | ||
| 818 : | |||
| 819 : | |||
| 820 : | File: unix.info, Node: Mouse buttons, Next: Reading mail, Prev: Logging in, Up: Getting started | ||
| 821 : | |||
| 822 : | Mouse buttons | ||
| 823 : | ============= | ||
| 824 : | |||
| 825 : | Unix has three mouse buttons. On some PC's running GNU/Linux or some | ||
| 826 : | other PC unix, there are only two, but the middle mouse button can be | ||
| 827 : | simulated by pressing both mouse buttons simultaneously. The mouse | ||
| 828 : | buttons have the following general functions. They may also have | ||
| 829 : | additional functions in special software. | ||
| 830 : | _index finger_ | ||
| 831 : | This is used to select and click on objects. It is also used to | ||
| 832 : | mark out areas and copy by dragging. This is the button you | ||
| 833 : | normally use. | ||
| 834 : | |||
| 835 : | _middle finger_ | ||
| 836 : | Used to pull down menus. It is also used to paste a marked area | ||
| 837 : | somewhere at the mouse position. | ||
| 838 : | |||
| 839 : | _outer finger_ | ||
| 840 : | Pulls down menus. | ||
| 841 : | |||
| 842 : | On a left-handed system right and left are reversed. | ||
| 843 : | |||
| 844 : | |||
| 845 : | File: unix.info, Node: Reading mail, Next: Simple commands, Prev: Mouse buttons, Up: Getting started | ||
| 846 : | |||
| 847 : | |||
| 848 : | ====== | ||
| 849 : | |||
| 850 : | Reading electronic mail on unix is just like any other system, but | ||
| 851 : | there are many programs to choose from. There are very old programs | ||
| 852 : | from the seventies such as | ||
| 853 : | |||
| 854 : | |||
| 855 : | |||
| 856 : | and there are fully graphical mail programs such as | ||
| 857 : | tkrat | ||
| 858 : | mailtool | ||
| 859 : | Choose the program you like best. Not all of the programs support | ||
| 860 : | modern multimedia extensions because of their age. Some programs like | ||
| 861 : | `tkrat' have immediate mail notification alerts. To start a mail | ||
| 862 : | program you just type its name. If you have an icon-bar, you can click | ||
| 863 : | on the mail-icon. | ||
| 864 : | |||
| 865 : | |||
| 866 : | File: unix.info, Node: Simple commands, Next: Text editing and word processing, Prev: Reading mail, Up: Getting started | ||
| 867 : | |||
| 868 : | Simple commands | ||
| 869 : | =============== | ||
| 870 : | |||
| 871 : | Inexperienced computer users often prefer to use file-manager | ||
| 872 : | programs to avoid typing anything. With a mouse you can click your way | ||
| 873 : | through directories and files without having to type anything (e.g. the | ||
| 874 : | `fmgr' or `tkdesk' programs). More experienced users generally find | ||
| 875 : | this to be slow and tedious after a while and prefer to use written | ||
| 876 : | commands. Unix has many short cuts and keyboard features which make | ||
| 877 : | typed commands extremely fast and much more powerful than use of the | ||
| 878 : | mouse. | ||
| 879 : | |||
| 880 : | If you come from a DOS environment, the unix commands can be a little | ||
| 881 : | strange. Because they stem from an era when keyboards had to be hit with | ||
| 882 : | hammer force, and machines were very slow, the command names are | ||
| 883 : | generally as short as possible, so they seem pretty cryptic. Some | ||
| 884 : | familar ones which DOS borrowed from unix include, | ||
| 885 : | |||
| 886 : | cd | ||
| 887 : | mkdir | ||
| 888 : | |||
| 889 : | which change to a new directory and make a new directory respectively. | ||
| 890 : | To list the files in the current directory you use, | ||
| 891 : | ls | ||
| 892 : | |||
| 893 : | To rename a file, you `move' it: | ||
| 894 : | mv OLD-NAME NEW-NAME | ||
| 895 : | |||
| 896 : | |||
| 897 : | File: unix.info, Node: Text editing and word processing, Prev: Simple commands, Up: Getting started | ||
| 898 : | |||
| 899 : | Text editing and word processing | ||
| 900 : | ================================ | ||
| 901 : | |||
| 902 : | Text editing is one of the things which people spend most time doing | ||
| 903 : | on any computer. It is important to distingiush text editing from word | ||
| 904 : | processing. On a PC or MacIntosh, you are perhaps used to Word or | ||
| 905 : | WordPerfect for writing documents. | ||
| 906 : | |||
| 907 : | Unix has a Word-like program called `lyx', but for the most part | ||
| 908 : | Unix users do not use word processors. It is more common in the unix | ||
| 909 : | community to write all documents, regardless of whether they are | ||
| 910 : | letters, books or computer programs, using a non-formatting text | ||
| 911 : | editor. (Unix word processors like `Framemaker' do exist, but they are | ||
| 912 : | very expensive. A version of MS-Word also exists for some unices.) | ||
| 913 : | Once you have written a document in a normal text editor, you call up a | ||
| 914 : | text formatter to make it pretty. You might think this strange, but the | ||
| 915 : | truth of the matter is that this two-stage process gives you the most | ||
| 916 : | power and flexibilty--and that is what most unix folks like. | ||
| 917 : | |||
| 918 : | For writing programs, or anything else, you edit a file by typing: | ||
| 919 : | |||
| 920 : | emacs myfile | ||
| 921 : | |||
| 922 : | `emacs' is one of dozens of text-editors. It is not the simplest or | ||
| 923 : | most intuitive, but it is the most powerful and if you are going to | ||
| 924 : | spend time learning an editor, it wouldn't do any harm to make it this | ||
| 925 : | one. You could also click on emacs' icon if you are relying on a window | ||
| 926 : | system. Emacs is almost certainly the most powerful text editor that | ||
| 927 : | exists on any system. It is not a word-processor, it not for formatting | ||
| 928 : | printed documents, but it can be linked to almost any other program in | ||
| 929 : | order to format and print text. It contains a powerful programming | ||
| 930 : | language and has many intelligent features. We shall not go into the | ||
| 931 : | details of document formatting in this book, but only mention that | ||
| 932 : | programs like `troff' and `Tex' or `Latex' are used for this purpose to | ||
| 933 : | obtain typset-quality printing. Text formatting is an area where Unix | ||
| 934 : | folks do things differently to PC folks. | ||
| 935 : | |||
| 936 : | |||
| 937 : | File: unix.info, Node: The login environment, Next: Files and access, Prev: Getting started, Up: Top | ||
| 938 : | |||
| 939 : | The login environment | ||
| 940 : | ********************* | ||
| 941 : | |||
| 942 : | Unix began as a timesharing mainframe system in the seventies, when | ||
| 943 : | the only terminals available were text based _teletype_ terminals or | ||
| 944 : | _tty_-s. Later, the Massachusetts Institute of Technology (MIT) | ||
| 945 : | developed the X-windows interface which is now a standard across UNIX | ||
| 946 : | platforms. Because of this history, the X-window system works as a | ||
| 947 : | front end to the standard UNIX shell and interface, so to understand | ||
| 948 : | the user environment we must first understand the shell. | ||
| 949 : | |||
| 950 : | * Menu: | ||
| 951 : | |||
| 952 : | * Shells:: | ||
| 953 : | * UNIX command overview:: | ||
| 954 : | * Terminals:: | ||
| 955 : | * The X window system:: | ||
| 956 : | * Multiple screens:: | ||
| 957 : | |||
| 958 : | |||
| 959 : | File: unix.info, Node: Shells, Next: UNIX command overview, Prev: The login environment, Up: The login environment | ||
| 960 : | |||
| 961 : | Shells | ||
| 962 : | ====== | ||
| 963 : | |||
| 964 : | A shell is a command interpreter. In the early days of unix, a shell | ||
| 965 : | was the only way of issuing commands to the system. Nowadays many | ||
| 966 : | window-based application programs provide menus and buttons to perform | ||
| 967 : | simple commands, but the UNIX shell remains the most powerful and | ||
| 968 : | flexible way of interacting with the system. | ||
| 969 : | |||
| 970 : | After logging in and entering a password, the unix process _init_ | ||
| 971 : | starts a shell for the user logging in. Unix has several different kinds | ||
| 972 : | of shell to choose from, so that each user can pick his/her favourite | ||
| 973 : | command interface. The type of shell which the system starts at login is | ||
| 974 : | determined by the user's entry in the _passwd_ database. On most | ||
| 975 : | systems, the standard login shell is a variant of the C-shell. | ||
| 976 : | |||
| 977 : | Shells provide facilities and commands which | ||
| 978 : | |||
| 979 : | * Start and stop processes (programs) | ||
| 980 : | |||
| 981 : | * Allow two processes to communicate through a _pipe_ | ||
| 982 : | |||
| 983 : | * Allow the user to redirect the flow of input or output | ||
| 984 : | |||
| 985 : | * Allow simple command line editing and command history | ||
| 986 : | |||
| 987 : | * Define aliases to frequently used commands | ||
| 988 : | |||
| 989 : | * Define global "environment" variables which are used to configure | ||
| 990 : | the default behaviour of a variety of programs. These lie in an | ||
| 991 : | "associated array" for each process and may be seen with the `env' | ||
| 992 : | command. Environment variables are inherited by all processes | ||
| 993 : | which are started from a shell. | ||
| 994 : | |||
| 995 : | * Provide wildcard expansion (joker notation) of filenames using | ||
| 996 : | `*,?,[]' | ||
| 997 : | |||
| 998 : | * Provide a simple script language, with tests and loops, so that | ||
| 999 : | users can combine system programs to create new programs of their | ||
| 1000 : | own. | ||
| 1001 : | |||
| 1002 : | * Change and remember the location of the current working directory, | ||
| 1003 : | or location within the file hierarchy. | ||
| 1004 : | |||
| 1005 : | |||
| 1006 : | The shell does not contain any more specific functions--all other | ||
| 1007 : | commands, such as programs which list files or create directories etc., | ||
| 1008 : | are executable programs which are independent of the shell. When you | ||
| 1009 : | type `ls', the shell looks for the executable file called `ls' in a | ||
| 1010 : | special list of directories called _the command path_ and attempts to | ||
| 1011 : | start this program. This allows such programs to be developed and | ||
| 1012 : | replaced independently of the actual command interpreter. | ||
| 1013 : | |||
| 1014 : | Each shell which is started can be customized and configured by | ||
| 1015 : | editing a setup file. For the C-shell and its variants this file is | ||
| 1016 : | called `.cshrc', and for the Bourne shell and its variants it is called | ||
| 1017 : | `.profile'. (Note that files which begin with leading dots are not | ||
| 1018 : | normally visible with the `ls' command. Use `ls -a' to view these.) | ||
| 1019 : | Any commands which are placed in these files are interpreted by the | ||
| 1020 : | shell before the first command prompt is issued. These files are | ||
| 1021 : | typically used to define a command search path and terminal | ||
| 1022 : | characteristics. | ||
| 1023 : | |||
| 1024 : | _On each new command line you can use the cursor keys to edit the | ||
| 1025 : | line. The up-arrow browses back through earlier commands. `CTRL-a' | ||
| 1026 : | takes you to the start of the line. `CTRL-e' takes you to the end of the | ||
| 1027 : | line. The <TAB> can be used to save typing with the `completion' | ||
| 1028 : | facility *Note Command/filename completion::._ | ||
| 1029 : | |||
| 1030 : | * Menu: | ||
| 1031 : | |||
| 1032 : | * Shell commands generally:: | ||
| 1033 : | * Environment and shell variables:: | ||
| 1034 : | * Wildcards:: | ||
| 1035 : | * Regular expressions:: | ||
| 1036 : | * Nested shells and reverse apostrophes:: | ||
| 1037 : | |||
| 1038 : | |||
| 1039 : | File: unix.info, Node: Shell commands generally, Next: Environment and shell variables, Prev: Shells, Up: Shells | ||
| 1040 : | |||
| 1041 : | Shell commands generally | ||
| 1042 : | ------------------------ | ||
| 1043 : | |||
| 1044 : | Shell commands are commands like `cp', `mv', `passwd', `cat', | ||
| 1045 : | `more', `less', `cc', `grep', `ps' etc.. | ||
| 1046 : | |||
| 1047 : | Very few commands are actually built into the shell command line | ||
| 1048 : | interpreter, in the way that they are in DOS - commands are mostly | ||
| 1049 : | programs which exist as files. When we type a command, the shell | ||
| 1050 : | searches for a program with the same name and tries to execute it. The | ||
| 1051 : | file must be executable, or a `Command not found' error will result. To | ||
| 1052 : | see what actually happens when you type a command like `gcc', try | ||
| 1053 : | typing in the following C-shell commands _directly into a C-shell_. (We | ||
| 1054 : | shall discuss these commands soon.) | ||
| 1055 : | |||
| 1056 : | foreach dir ( $path ) # for every directory in the list path | ||
| 1057 : | if ( -x $dir/gcc ) then # if the file is executable | ||
| 1058 : | echo Found $dir/gcc # Print message found! | ||
| 1059 : | break # break out of loop | ||
| 1060 : | else | ||
| 1061 : | echo Searching $dir/gcc | ||
| 1062 : | endif | ||
| 1063 : | end | ||
| 1064 : | |||
| 1065 : | The output of this command is something like | ||
| 1066 : | |||
| 1067 : | Searching /usr/lang/gcc | ||
| 1068 : | Searching /usr/openwin/bin/gcc | ||
| 1069 : | Searching /usr/openwin/bin/xview/gcc | ||
| 1070 : | Searching /physics/lib/framemaker/bin/gcc | ||
| 1071 : | Searching /physics/motif/bin/gcc | ||
| 1072 : | Searching /physics/mutils/bin/gcc | ||
| 1073 : | Searching /physics/common/scripts/gcc | ||
| 1074 : | Found /physics/bin/gcc | ||
| 1075 : | If you type | ||
| 1076 : | echo $path | ||
| 1077 : | you will see the entire list of directories which are searched by | ||
| 1078 : | the shell. If we had left out the `break' command, we might have | ||
| 1079 : | discovered that UNIX often has several programs with the same name, in | ||
| 1080 : | different directories! For example, | ||
| 1081 : | /bin/mail | ||
| 1082 : | /usr/ucb/mail | ||
| 1083 : | /bin/Mail | ||
| 1084 : | |||
| 1085 : | /bin/make | ||
| 1086 : | /usr/local/bin/make. | ||
| 1087 : | Also, different versions of unix have different conventions for | ||
| 1088 : | placing the commands in directories, so the path list needs to be | ||
| 1089 : | different for different types of unix machine. In the C-shell a few | ||
| 1090 : | basic commands like `cd' and `kill' are built into the shell (as in | ||
| 1091 : | DOS). | ||
| 1092 : | |||
| 1093 : | You can find out which directory a command is stored in using the | ||
| 1094 : | which | ||
| 1095 : | command. For example | ||
| 1096 : | nexus% which cd | ||
| 1097 : | cd: shell built-in command. | ||
| 1098 : | nexus% which cp | ||
| 1099 : | /bin/cp | ||
| 1100 : | nexus% | ||
| 1101 : | `which' only searches the directories in `$path' and quits after the | ||
| 1102 : | first match, so if there are several commands with the same name, you | ||
| 1103 : | will only see the first of them using which. | ||
| 1104 : | |||
| 1105 : | Finally, in the C-shell, the `which' command is built in. In the | ||
| 1106 : | Bourne shell it is a program: | ||
| 1107 : | nexus% which which | ||
| 1108 : | which: shell built-in command. | ||
| 1109 : | nexus% sh | ||
| 1110 : | $ which which | ||
| 1111 : | /bin/which | ||
| 1112 : | $ exit | ||
| 1113 : | nexus% | ||
| 1114 : | Take a look at the script `/usr/ucb/which'. It is a script written | ||
| 1115 : | in the C-shell. | ||
| 1116 : | |||
| 1117 : | |||
| 1118 : | File: unix.info, Node: Environment and shell variables, Next: Wildcards, Prev: Shell commands generally, Up: Shells | ||
| 1119 : | |||
| 1120 : | Environment and shell variables | ||
| 1121 : | ------------------------------- | ||
| 1122 : | |||
| 1123 : | Environment variables are variables which the shell keeps. They are | ||
| 1124 : | normally used to configure the behaviour of utility programs like `lpr' | ||
| 1125 : | (which sends a file to the printer) and `mail' (which reads and sends | ||
| 1126 : | mail) so that special options do not have to be typed in every time you | ||
| 1127 : | run these programs. | ||
| 1128 : | |||
| 1129 : | Any program can read these variables to find out how you have | ||
| 1130 : | configured your working environment. We shall meet these variables | ||
| 1131 : | frequently. Here are some important variables | ||
| 1132 : | |||
| 1133 : | PATH # The search path for shell commands (sh) | ||
| 1134 : | TERM # The terminal type (sh and csh) | ||
| 1135 : | DISPLAY # X11 - the name of your display | ||
| 1136 : | LD_LIBRARY_PATH # Path to search for object and shared libraries | ||
| 1137 : | HOST # Name of this unix host | ||
| 1138 : | PRINTER # Default printer (lpr) | ||
| 1139 : | HOME # The path to your home directory (sh) | ||
| 1140 : | |||
| 1141 : | path # The search path for shell commands (csh) | ||
| 1142 : | term # The terminal type (csh) | ||
| 1143 : | noclobber # See below under redirection | ||
| 1144 : | prompt # The default prompt for csh | ||
| 1145 : | home # The path to your home directory (csh) | ||
| 1146 : | These variables fall into two groups. Traditionally the first group | ||
| 1147 : | always have names in uppercase letters and are called _environment | ||
| 1148 : | variables_, whereas variables in the second group have names with | ||
| 1149 : | lowercase letters and are called _shell variables_- but this is only a | ||
| 1150 : | convention. The uppercase variables are _global variables_, whereas the | ||
| 1151 : | lower case variables are _local variables_. Local variables are not | ||
| 1152 : | defined for programs or sub-shells started by the current shell, while | ||
| 1153 : | global variables are inherited by all sub-shells. | ||
| 1154 : | |||
| 1155 : | The Bourne-shell and the C-shell use these conventions differently | ||
| 1156 : | and not always consistently. You will see how to define these below. | ||
| 1157 : | For now you just have to know that you can use the following commands | ||
| 1158 : | from the C-shell to list these variables. The command `env' can be used | ||
| 1159 : | in either C-shell or Bourne shell to see all of the defined environment | ||
| 1160 : | variables. | ||
| 1161 : | |||
| 1162 : | |||
| 1163 : | File: unix.info, Node: Wildcards, Next: Regular expressions, Prev: Environment and shell variables, Up: Shells | ||
| 1164 : | |||
| 1165 : | Wildcards | ||
| 1166 : | --------- | ||
| 1167 : | |||
| 1168 : | Sometimes you want to be able to refer to several files in one go. | ||
| 1169 : | For instance, you might want to copy all files ending in `.c' to a new | ||
| 1170 : | directory. To do this one uses _wildcards_. Wildcards are characters | ||
| 1171 : | like `* ?' which stand for any character or group of characters. In | ||
| 1172 : | card games the joker is a `wild card' which can be substituted for any | ||
| 1173 : | other card. Use of wildcards is also called _filename substitution_ in | ||
| 1174 : | the unix manuals, in the sections on `sh' and `csh'. | ||
| 1175 : | |||
| 1176 : | The wildcard symbols are, | ||
| 1177 : | |||
| 1178 : | `?' | ||
| 1179 : | Match single character. e.g. `ls /etc/rc.????' | ||
| 1180 : | |||
| 1181 : | `*' | ||
| 1182 : | Match any number of characters. e.g. `ls /etc/rc.*' | ||
| 1183 : | |||
| 1184 : | `[...]' | ||
| 1185 : | Match any character in a list enclosed by these brackets. e.g. `ls | ||
| 1186 : | [abc].C' | ||
| 1187 : | |||
| 1188 : | Here are some examples and explanations. | ||
| 1189 : | |||
| 1190 : | `/etc/rc.????' | ||
| 1191 : | Match all files in /etc whose first three characters are `rc.' and | ||
| 1192 : | are 7 characters long. | ||
| 1193 : | |||
| 1194 : | `*.c' | ||
| 1195 : | Match all files ending in `.c' i.e. all C programs. | ||
| 1196 : | |||
| 1197 : | `*.[Cc]' | ||
| 1198 : | List all files ending on `.c' or `.C' i.e. all C and C++ programs. | ||
| 1199 : | |||
| 1200 : | `*.[a-z]' | ||
| 1201 : | Match any file ending in .a, .b, .c, ... up to .z etc. | ||
| 1202 : | |||
| 1203 : | It is important to understand that _the shell expands wildcards_. When | ||
| 1204 : | you type a command, the program is not invoked with an argument that | ||
| 1205 : | contains `*' or `?'. The shell expands the special characters first and | ||
| 1206 : | invokes commands with the entire list of files which match the | ||
| 1207 : | patterns. The programs never see the wildcard characters, only the list | ||
| 1208 : | of files they stand for. To see this in action, you can type | ||
| 1209 : | echo /etc/rc.* | ||
| 1210 : | which gives | ||
| 1211 : | /etc/rc /etc/rc.boot /etc/rc.ip /etc/rc.local /etc/rc.local% | ||
| 1212 : | /etc/rc.local~ /etc/rc.single /etc/rc~ | ||
| 1213 : | All shell commands are invoked with a command line of this form. This | ||
| 1214 : | has an important corollary. It means that multiple renaming _cannot | ||
| 1215 : | work_! | ||
| 1216 : | |||
| 1217 : | Unix files are renamed using the `mv' command. In many microcomputer | ||
| 1218 : | operating systems one can write | ||
| 1219 : | rename *.x *.y | ||
| 1220 : | |||
| 1221 : | which changes the file extension of all files ending in `.x' to the same | ||
| 1222 : | name with a `.y' extension. This cannot work in unix, because the | ||
| 1223 : | shell tries expands everything before passing the arguments to the | ||
| 1224 : | command line. | ||
| 1225 : | |||
| 1226 : | _The local shell variable `noglob' switches off wildcard expansion in | ||
| 1227 : | the C shell, but you still cannot rename multiple files using `mv'. | ||
| 1228 : | Some free-software programs make this possible._ | ||
| 1229 : |
| Administrator | ViewVC Help |
| Powered by ViewVC 1.0.3 |