Sunteți pe pagina 1din 4

Essentials for Scientic Computing: Introduction to UNIX and Linux Day 2

Ershaad Ahamed TUE-CMS, JNCASR

May 2012 0.1 Command History

By default, the bash shell maintains a history of commands that have by run from the prompt. The up and down arrow keys on the keyboard can be used to retrieve commands previously executed. The history command will display the entire stored history of commands. In order to do an incremental search for a command, use Ctrl-r. To abort an incremental search press Ctrl-g and to make the command line that is found as the current command use the Esc key.

0.2

Terminating Commands

A command which is being executed in the current shell session can be terminated by the Ctrl-C key combination.

0.3

Job Control

Commands that are executed from the command line are executed in the foreground. that means the shell prompt will not be available until the command completes. In case the program that is running does not require input from the user through the terminal, it can be run in the background with the following syntax command & Now the prompt will be available and the shell is ready to accept new commands while the previous command continues executing in the background. This is how multitasking is achieved in the Bash shell. All background jobs in the current shell can be viewed with the jobs command. The output of the jobs command may look like. [1] [2] [3]+ [4]Running Running Stopped Running cat cat cat cat /dev/urandom > /dev/null & /dev/zero > /dev/null & /dev/random > /dev/null /dev/zero > /dev/null &

A background job can be brought into the foreground by fg [jobspec]. A foreground job can be stopped/suspended by using the Ctrl-z key. A stopped foreground job can be resumed in the background using bg [jobspec]. Where jobspec is the % character followed by the job number that is displayed in the output of the jobs command. Some examples. fg %2 bg %3 0.3.1 Terminating a Background Job

The shell built-in kill command can be used to terminate background jobs that are running or stopped. For example to terminate the second job in the jobs output. kill %2

Environment Variables

Environment variables are essentially shell variables (which will be explained in the chapter on shell scripting) that are marked for export. That is, an environment variable is visible to any program that is started from the shell session within which the variable was exported. In order to set an environment variable, the syntax used is export VAR_NAME=value Environment variables are conventionally given names using ALLCAPS. Similar to command line options, environment variables are a convenient way to modify the behaviour of programs. The man pages of programs list the environment variables whose values aect the programs behaviour. An advantage of environment variables over command line options is that they can be set in startup les so that they are eective for the entire duration of the shell session, whereas command line options need to be specied each time a command is executed. An example is that the -N option to the less command will cause it display line numbers for the le being viewed. less -N program.txt You can place the -N option into an environment variable named LESS which is documented in the less manpage. export LESS=-N With this variable exported, whenever you run the less command from that session of the shell, it will behave as though the -N option was included in the command line. In order to remove an environment variable the unset command can be used. For example. unset LESS To view all environment variables that are set in the current shell session, use. env 2

Editing Text Files with Vim

Editing les means modifying its contents in an interactive manner. Vi is an editor that has been in use since early UNIX systems. In traditional word processors (like MS Word) or text editors (like Notepad), entry of text, movement within the le, and operations on textual data are performed by using the keyboard, mouse and special key combinations. Vi on the other hand is a modal editor. This means that the characters on the keyboard (like h, j, k, l) are used to manipulate text data and to move around within a le. When literal text needs to be entered, the editor is switched to insert mode, where the keys lose their special functions and behave the same as in a regular editor. Vim stands for Vi iMproved and has several extended features in addition to those of Vi. One notable addition is the visual mode where blocks of text can be highlighted and operations performed on them. The operation (like replacing one word with another) operates only on the highlighted text.

2.1

Getting Started with Vim

In order to open and existing le or an empty le for editing, you use. vim [FILENAME] This will display the le and vim will be in command mode. Although Vi has an extremely rich set of commands that allows one to perform complex text manipulations in a few keystrokes (where its true advantage over traditional editors becomes quickly evident), we will present below only the basic keys required to navigate and perform routine text manipulation. Once the le has been opened, basic movement within the le can be done with the keys below in command mode. h Cursor left j Cursor down k Cursor up l Cursor right b Cursor left one word w Cursor right one word To delete text in command mode, the following are the commonly used keys. x Delete character under cursor X Delete character to the left of cursor (backspace) 3

dd Delete an entire line Undo (thankfully) is available u Undo last operation Ctrl-r Redo In order to insert (type) literal text, rst you must enter insert mode by pressing i in command mode. Once in insert mode, arbitrary text can be entered. To return to command mode hit Esc. Once youre done with editing the le, you will want to write it out to disk. in order to do this, you rst Esc into command mode. Then type :w followed by the enter key. To quit from Vim after writing the le, type :q in command mode. In case the le is modied and you want to quit the editor discarding the changes you made since the last write, use :q! to exit without writing the le.