Friday, July 10, 2009

UNIX Operating System | C Programming Language

The UNIX Operating System

The following is a (very) brief summary of the UNIX (or Linux) Operating System. It describes a very small subset of the commands available under the shells sh (or its linux version, bash) and csh (or tcsh, an extended version of csh, with advanced command-line editing features). These shells offer a convenient syntax for executing interactive commands. The command structure in bash is in many ways more powerful, and is particularly useful in writing scripts where efficiency is important.

The File System

In UNIX (and Linux), physical disks are divided into logical ``partitions'' (blocks of space of specific size) with the format command. A file system is then imposed on these partitions, allowing a directory structure to exist. Directories (``folders'' in Windows or Macintosh terminology) can contain files of arbitrary types.

Some commands allowing a user to manipulate files are:

pwd
displays the name of the current directory (``.'')

ls
lists the files in the current directory

ls dir
lists the files in the directory ``dir''

ls
displays the contents of the current directory; ``ls -lF'' is a useful extension providing additional information about the files

cp file1 file2
copies file1 to file2

cd /dir1/dir2
go to specified directory; cd .. means go to the directory above the current directory; cd ../foo means go up one level, then down into directory foo. The current directory is always denoted by ``.''

mv file1 file2
rename file1 as file2, within the same directory (changing the name of the file), or move file1 into some other directory

cat file
display file onto standard output (the screen, usually)

more file
display file onto standard output (screen), one screenful at a time. For obscure UNIX reasons, the command "less" does much the same thing...

>
>>
redirect output to a file; e.g. cat file1 > file2 copies file file1 into file2, and cat file1 >> file2 appends file1 onto file2.

|
``pipe'' standard output to standard input e.g. cmd1 | cmd2 sends the output of command cmd1 into the input of cmd2

touch file
updates the ``date and time stamp'' of a file, making it believe that it has just been modified

rm file
delete a file

File name completion is supported under csh and tcsh (turn it on by issuing the command ``set filec.'' In csh, use the ESCAPE key; in tcsh, use the TAB key.

UNIX also supports ``wildcards.'' A ``?'' will be replaced by any matching single character, while a ``*'' will be replaced by any number of matching characters. For example, ls abc*.c will list all files whose name starts with ``abc'' and ends in ``.c''.

Multi-User, Multi-Tasking Environment

UNIX provides a multi-user and multi-tasking computing environment. This implies that it will allow the computer to share its resources among many users, and the various tasks that each user might be performing. This implies that UNIX must provide a security safegard against users peeking into each others' ``home directories.''

To gain access to a UNIX system, a user must first identify his/her account in response to the login prompt,

 Login:     username 
Password: password
then give a unique password. A user can change his/her password by issuing the command passwd, followed by the old and new passwords.

The user can exit the system with the command logout .

Help on any UNIX command can be obtained via the man command. The command ``man cmd'' prints to standard output the content of the manual pages concerning the UNIX command ``cmd''. You can also search by context with man -k cmd .

There are many commands which facilitate the users' interaction with the computer in small ways; for instance:

date
gives you the date
time
gives you the time
hostname
reminds you of the system name
finger
gives a list of users and what they are doing
ps
display the tasks running on the computer; try ps -aux
history
lists your last issued commands
!!
recalls the last command
!str
recalls the last command starting with the string str
!n
recalls the nth command
On login, UNIX starts up a new shell interpreter (e.g. the C-shell interpreter csh), and sets up the computing environment for the user by scanning the ``hidden'' (i.e. not normally listed by ls) set-up files, .login and .cshrc . The .cshrc file is also scanned each time a new ``task'' (e.g. a new window) is launched. These hidden files are how a user can customize his/her computing environment at will, typically with commands like
 alias name definition
in csh, or
        alias name=definition
in bash, which defines ``name'' (usually something short) to mean ``definition'' (often long and/or complicated).

Each file is marked for ownership by a user and as belonging to a group of users. Each file is also marked for ``reading'', ``writing'' and ``execute'' privileges by its owner, members of its group and by ``others'' The directory command ``ls -lF'' lists these privileges as ``r'', ``w'' and ``x'' if they are allowed. For instance, the following dialog

 > ls -lF
total 6
-rw-rw-r-- 1 steve 4322 Jan 10 12:30 file1
drwxrwxr-x 2 steve 512 Jan 10 12:29 file2/
>
shows that file1 has can be read and written by the owner and all group members, and read by all, while file2 is unprotected, except that it cannot be written by ``others''. Furthermore, file2 is a directory (name of a sub-directory within the current one -- indicated by a ``d'' in the first column).

The privileges of a file can be modified by the chmod command. The ownership of a file can be modified by the command chown ; this is a restricted command -- it can be executed only by the system ``super-user.''

Executing Commands: Running Programs

A file can be of the special ``executable'' type (e.g. resulting from a compilation of C code followed by linking to the appropriate libraries), meaning that it contains ``instructions'' for the computer to execute. For instance, each UNIX command is itself an ``executable'' residing on the disk in a directory accesible by all. These files are executed by specifying their name, followed by any necessary argument. For instance,

 ls -lFg file

will execute the ``ls'' command, with arguments ``-lFfg'' (a switch) and ``file'', resulting is a very complete directory listing for the file ``file''. The same goes for any executable file that you might create.

Automating Tasks: Scripts

Any set of UNIX commands can be put into a ``script'' file to be executed all at once as a single command. The language is very complete, including control structures and looping. As an example, consider the following script:

#!/bin/csh
#
# example of a C-shell script
# ---------------------------
#
# store the wordlist resulting from
# the ls command in variable files
#
set files = `ls`
#
# foreach is a loop; files takes on the
# value of each entry in files
#
# $variable_name is the content of
# that variable
#
foreach file ( $files )
echo 'File name:' $file
end

The script explicitly calls for the C-shell interpreter; the ``set'' command transfers the result of the ls command into the variable ``files'' as a wordlist. Then the ``foreach ... end'' loops over the wordlist, assigning each member to the variable ``file''. The command ``echo'' writes a message to standard output. The syntax ``$variable'' means the ``content'' of the variable; $var[i] means the content of the ith position in the variable.

At the script level, the differences between bash and csh become more evident. A bash version of the above script might look like:

#!/bin/bash
#
# example of a bash script
# ------------------------
#
# store the wordlist resulting from
# the ls command in variable files
#
files=`ls`
#
# for is a loop; files takes on the
# value of each entry in files
#
# $variable_name is the content of
# that variable
#
for file in $files; do
echo 'File name:' $file
done

No comments:

Blog List