S E C T I O N O V E R V I E W

Similar documents
S E C T I O N O V E R V I E W

S E C T I O N O V E R V I E W

System Programming. Unix Shells

S E C T I O N O V E R V I E W

Introduction to UNIX Shell Exercises

Introduction to Linux

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

ACS Unix (Winter Term, ) Page 21

UNIX shell scripting

Lecture # 2 Introduction to UNIX (Part 2)

5/20/2007. Touring Essential Programs

Assignment clarifications

The Online Unix Manual

5/8/2012. Specifying Instructions to the Shell Chapter 8

UNIX Quick Reference

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

Operating Systems, Unix Files and Commands SEEM

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

Exploring UNIX: Session 5 (optional)

Introduction to Linux

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

I/O and Shell Scripting

Project 1: Implementing a Shell

CSC UNIX System, Spring 2015

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

Vi & Shell Scripting

Introduction to Linux Organizing Files

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

The Unix Shell & Shell Scripts

IMPORTANT: Logging Off LOGGING IN

Oxford University Computing Services. Getting Started with Unix

Utilities. September 8, 2015

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

1Z Oracle Linux Fundamentals (Oracle Partner Network) Exam Summary Syllabus Questions

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Linux Command Line Interface. December 27, 2017

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

acmteam/unix.pdf How to manage your account (user ID, password, shell); How to compile C, C++, and Java programs;

Bamuengine.com. Chapter 7. The Process

CISC 220 fall 2011, set 1: Linux basics

Chapter-3. Introduction to Unix: Fundamental Commands

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

MODULE 5. PROCESS AND SYSTEM ADMINISTRATION

Introduction: What is Unix?

System Programming. Introduction to Unix

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

9.2 Linux Essentials Exam Objectives

CHAPTER 2 THE UNIX SHELLS

Command-line interpreters

Some useful UNIX Commands written down by Razor for newbies to get a start in UNIX

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

UNIX Tutorial Five

NOTE: From the engineering network, users may login directly to the desired computer server using the SSH connection.

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

File system Security (Access Rights)

Creating a Shell or Command Interperter Program CSCI411 Lab

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

Introduction to remote command line Linux. Research Computing Team University of Birmingham

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Lab 1 Introduction to UNIX and C

Self-test Linux/UNIX fundamentals

Useful Unix Commands Cheat Sheet

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Lab #1 Installing a System Due Friday, September 6, 2002

Computer Systems and Architecture

85321, Systems Administration Chapter 6: The shell

Shells and Shell Programming

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CS 460 Linux Tutorial

Operating Systems Lab 1 (Users, Groups, and Security)

Linux Tutorial #6. -rw-r csce_user csce_user 20 Jan 4 09:15 list1.txt -rw-r csce_user csce_user 26 Jan 4 09:16 list2.

Handling Ordinary Files

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Shells and Shell Programming

7. Archiving and compressing 7.1 Introduction

Computer Systems and Architecture

Exploring UNIX: Session 3

SOFTWARE ARCHITECTURE 3. SHELL

Unix Processes. What is a Process?

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

INSE Lab 1 Introduction to UNIX Fall 2017

Implementation of a simple shell, xssh

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

C Shell Tutorial. Section 1

Practical Session 0 Introduction to Linux

bash, part 3 Chris GauthierDickey

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Introduction to UNIX Command Line

Transcription:

PROGRAM CONTROL, FILE ARCHIVING, ENVIRONMENT AND SCRIPTS S E C T I O N O V E R V I E W Continuing from last section, we are going to learn about the following concepts: controlling programs; working with the UNIX environment and environment variables; introducing the notion of scripts; archiving and compressing files. 6.1 ARCHIVING FILES There are times we need to transfer files or even directories at once. Moreover, we may also need to send them as attachments to other users who are located all over the globe. Sometimes, we may also wish to store sensitive information in a safe place, just to make sure we can retrieve this information back, in case our computing system crashes. Although, this is easy there are some obstacles that stem from space limitations and interoperability issues. For example, we may not be allowed to attach large files in an email or we may need to transfer our files through a very slow logical connection. Moreover, different operating systems and machines store data in different formats. In order to address these issues, UNIX developers have been implemented two commands known as tar (tape archive) and cpio (copy in out). These commands are capable of archiving any file that exists in a directory and can store data used to describe its structure. The outcome of using these commands is a single file known as archive. Both commands can create archives; however these archives have different format and work with a PAGE 1

different way. This may generate problems to users. For instance, some of these users may be familiar with tar and some others with cpio. Despite the fact that tar has been used more frequently than cpio, developers decided to develop a new archiving command that combines functionalities from both programs. This resulted in the development of pax (portable archive exchange). The syntax of these archiving commands is described in syntax 6.1 section. The syntax of cpio command which is rarely used and therefore it is omitted. SYNTAX tar, pax pax option(s) <directory> <arguments> tar option(s) <directory> <arguments> SYNTAX 6.1: tar and pax SYNTAX In addition to the syntax of these UNIX commands. PAX TAR DESCRIPTION w c Create file f f Filename follows t Lists contents in a tar file v v Verbose - display contents as it is tarring or extracting r x Extract the contents of the TAR file z Uncompress it before extracting (gunzip). Typically used with files ending in.tar.gz or.tgz u Copies a file only if it is newer p Preserves file access o Assigns ownership to the user running tar command TABLE 5.1: TAR AND PAX: SOME AVAILABLE OPTIONS Although, there are more than these options listed in table 6.1, we provide some that are used regularly. Users may also like to have a look into the manual pages for these commands available by executing man with given argument the name of the command. Moreover, note that instead of using the f option, we can use the input and output redirection characters, as well. This will forward output or input to a file with a given filename or ouput can be redirected to a device such as a floppy drive or another kind of storage. PAGE 2

6.1 ARCHIVING WITH tar and pax Now, let us assume that we need to archive a directory that contains important information. In the following example, we initially use the pax command and then we continue with tar command. EXAMPLE tar, pax 10 tiger% ls -l total 31 -rw------- 2 svigkoi staff 129 May 24 16:59 hmyfile -rw------- 2 svigkoi staff 129 May 24 16:59 myfile lrwxrwxrwx 1 svigkoi staff 6 May 24 18:33 smyfile -> myfile45 tiger% pax -wf archive.pax./ 11 tiger% pax -wf archive.pax./ 12 tiger% ls l total 32 -rw------- 1 svigkoi staff 4096 May 24 18:33 archive.pax -rw------- 2 svigkoi staff 129 May 24 16:59 hmyfile -rw------- 2 svigkoi staff 129 May 24 16:59 myfile lrwxrwxrwx 1 svigkoi staff 6 May 24 18:33 smyfile -> myfile45 tiger% pax -wf archive.pax./ 48 tiger% wc -c archive.pax 4096 archive.pax SAMPLE OUTPUT 6.1: pax ARCHIVING COMMANDS In the example provided, we used the command pax -wf archive.pax./ in order to archive the contents of our current directory. The option w instructed pax to create a new archive while f indicated that the archive should be a file. The argument archive.pax is the file name of the archive that was created. The last argument entered is./ and indicated pax command the directory we wish to archive. Note that we also introduced a suffix to the given filename. Although, it is not necessary, we intentionally did that in order to indicate this is an archive file. However, any suffix is optional. Similarly, we provide an example for tar command. The options used are different in comparison to pax. Moreover, in order to archive our current directory, we move out of it as this is recommended. PAGE 3

17 tiger% tar -cvf archive.tar./tar_example./tar_example/./tar_example/myfile./tar_example/smyfile./tar_example/hmyfile 18 tiger% ls l -rw------- 1 svigkoi staff 10240 May 24 19:44 archive.tar drwx------ 3 svigkoi staff 512 Dec 19 2002 ns_imap drwx------ 2 svigkoi staff 512 Dec 19 2002 nsmail drwx------ 3 svigkoi staff 512 May 19 11:01 one 19 tiger% wc c mytar.tar 10240 mytar.tar 20 tiger% SAMPLE OUTPUT 6.2: tar ARCHIVING COMMAND The command found in sample output 6.2 allowed us to create an archive. As shown, tar is the command used, -cvf are options, we used; archive.tar corresponds to the name of the archive that we wished to create while the./tar_example refers to the directory, we want to back up. Checking carefully the options entered we observe that c indicates that we wish to create an archive. The second option used v allowed us to have information about files that were being archived. This information was being displayed on the terminal window. The last option indicates our instructions to create a file instead for example sending tar s output to a device such as a floppy drive. 6.1.2 EXTRACTING ARCHIVE FILES USING tar and pax After archiving files, we may also need to extract their contents for future use. This action also applies if we receive an archive file from someone else. In order to extract archive files we use exactly the same commands elaborated previously. However, any options entered should be slightly different. In this subsection, we describe the process of extracting contents from archive files using pax and tar, respectively. PAGE 4

31 tiger% pax -rf../archive.pax 32 tiger% ls -l total 8 -rw------- 1 svigkoi staff 0 May 24 20:47 archive.pax -rw------- 1 svigkoi staff 165 May 24 20:45 file1 33 tiger% SAMPLE OUTPUT 6.3: EXTRACTING FILES FROM ARCHIVES USING pax In sample output 6.3, we extracted into our current directory the contents of the archive file archive.pax. The options used rf indicate that r stands for read and f indicated that the following argument is an archive file. Typing ls l we can see that the contents of the archive archive.pax were extracted. These are archive.pax and file1. The archive file used was located in our parent directory. One important observation here is that our archive file includes the archive file, we wished to extract. This generated a problem resulting in having a file (archive.pax) with zero bytes (empty file). Therefore, it is a common practice we should never archive a directory we are currently in or extracting archives that also contain our archive file in. A common practice for the avoidance of such as these problems is that if we wish to archive our current directory we move one directory up to our parent directory and then we perform the archiving process. In a similar fashion, we also provide some examples that correspond to tar command. 56 tiger% tar -xvf../my_dir_archive.tar./my_dir_archive/./my_dir_archive/file1./my_dir_archive/archive.pac 57 tiger% ls my_dir_archive 58 tiger% SAMPLE OUTPUT 6.4: EXTRACTING FILES FROM ARCHIVES USING pax The most important point here is the options used, namely xvf. The first character from left stands for extract. In other words indicated tar command to extract an archive. The second stands for verbose and dictates tar PAGE 5

to provide a list of all contents being extracted while option f indicates that a file given as argument follows. Note that our archive is located in our parent directory. 6.2 COMPRESSING AND UNCOMPRESSING FILES In an attempt to find out the size of the archived files created by the pax and tar commands, we can easily understand their usability. However, we can also conclude that files created are bigger in size than the size of the all files contained in the directory, we archived. This is because of that these archiving commands store additional information in archive files that reflect the structure of files and sub-directories included in a directory. At this point, we may also have an obvious question like Can we compress an archive file in order to reduce its output?. The answer, of course, is positive. 6.1 COMPRESSING FILES The command for compressing files is known as compress and is used to reduce the size of archives. The syntax is shown bellow: compress [-c] [-f] [-v] <filename(s)> SYNTAX 6.2: tar and pax SYNTAX table: The available options of this command are described in the following OPTION DESCRIPTION w f v It writes to the standard output and no files are changed. No.Z files are created. When compressing, force compression of file. Does not consider if file does not actually reduce the size or if there is an existed filename suffixed with.z. In case -f option is not given and provided that the process is not running in the background, compress prompts to verify whether an existing file.z file should be overwritten. Verbose output that Shows you how much the file shrank in size. TABLE 5.2: UNCOMPRESS: AVAILABLE OPTIONS We provide an example to describe how compress command works. Consider the following output: PAGE 6

83 tiger% ls -ld arc* drwx------ 2 svigkoi staff 512 May 24 22:07 archive -rw------- 1 svigkoi staff 10240 May 24 22:14 archive.tar 84 tiger% compress archive.tar 85 tiger% ls -ld arc* drwx------ 2 svigkoi staff 512 May 24 22:07 archive -rw------- 1 svigkoi staff 745 May 24 22:14 archive.tar.z 86 tiger% SAMPLE OUTPUT 6.5: COMPRESSING ARCHIVES USING compress As shown in the sample output 6.5, we first used ls ld arc* to display all files that match the expression entered. As we as interested in compressing a tar file, we executed the command compress archive.tar. By executing ls ld arc*, we can see that a new file was created with filename archive.tar.z. The file given as argument to the compress command was deleted automatically. This is the way compress command works. After compressing a file, compress deletes it and outputs a file which is compressed with filename the name of the old file concatenated with the suffix.z. Of course, we can also see that the compressed file is much more less in size in comparison with the original uncompressed one. 6.2 UNCOMPRESSING FILES Compressed files can be uncompressed with uncompress command. We omit the description of its syntax and options available as they are almost identical with compress command. However, we provide an example to demonstrate its functionality. 87 tiger% ls ld arc* -rw------- 1 svigkoi staff 745 May 24 22:14 archive.tar.z 88 tiger% uncompress archive.tar.z 89 tiger% ls -ld arc* -rw------- 1 svigkoi staff 10240 May 24 22:14 my_dir_archive.tar 90 tiger% wc -c my_dir_archive.tar 10240 my_dir_archive.tar 91 tiger% SAMPLE OUTPUT 6.6: UNCOMPRESSING ARCHIVES USING uncompress PAGE 7

Initially, we instructed the shell to display information regarding archive.tar.z. As shown, the size of the compressed file is 745 bytes. Then, we used the uncompressed archive.tar.z command and we uncompressed the corresponding file. The wc c archive showed us the size of the uncompressed file which is significantly larger that the.z one. SAMPLE OUTPUT 6.5: COMPRESSING ARCHIVES USING compress 6.3 WORKING WITH ENVIRONMENT In UNIX and other operating systems, there is a notion known as environment. This is a communication mechanism that allows the system itself and users to locate various commands that are dispersed across directories. Moreover, it is also used for other settings. In general, the environment is just a set of variable names that usually contain values. However, the notion of type (i.e. integer, string etc) is missing. In fact, variables are considered as sequences of strings while they can be interpreted as numbers in some circumstances and when this is necessary. 6.2.1 ENVIRONMENT VARIABLES So far, we have seen the use of some environment variables. Moreover, we also had the opportunity to work with them by assigning different values into them. Perhaps, you remember that by changing the value of environment variable prompt we can modify our shell command prompt. Environment variables are structured by block capital letters for convention while as we already know filenames and directories names are expressed by small case letters. There are several environment variables we may need to consider. Some of them may have been already defined while others may have been not. Moreover, some of them are used in specific shells while in others they are not. VARIABLE NAME DESCRIPTION DISPLAY EDITOR HOME LOGNAME MANPATH PAGER It indicated where the X-Windows output is forwarded. It is usually set up based on the machine s IP you log in. It reflects the editor which is used whenever a program calls for a file to be edited. It can be set up for using vi or emacs or others such as pico. It corresponds to the absolute pathname of your home directory. It refers to a user s login name. It corresponds to the search path that is used when locating a man page. It has a similar purpose like PATH but it is used only for man pages. It refers to the program that is used by some other commands i.e. the man PAGE 8

PATH PRINTER PS1 PS2 SHELL TERM VISUAL command. The actual command referred actually displays the output of a file or a command. For example, one of these pager commands is the more command while others include the less as an alternative etc. It is the search path that is used for locating a command or a program. Each entry is by colons (:). This variable is set automatically as it serves an important role. However, it can be altered according to user s need. However, any changes planned are not recommended for inexperienced users. Changes can be performed only for a login session or permanently. The latter case corresponds to changes should be made in cshrc. It refers to your default printer It refers to the shell command prompt. It refers to the shell command continuation prompt. It corresponds to the pathname of the shell you use. It hodls a value that reflects the terminal window you use. It has the same purpose like the EDITOR environment variable. TABLE 5.3: DETAILED LIST: ENVIRONMENT VARIABLES One of the most important is the PATH variable. The reason behind this claim is that every command used is found through this indexing notion. In fact, after typing a command the operating system consults the PATH variable and checks each entry known as pathname component. If the corresponding program or command is found in these path components then is executed otherwise an error is generated. We can see the contents of each variable individually or we can have a list that contains all of them. So far, we have seen the first approach which is carried out by entering in our command prompt: echo $PATH This results in displaying the value of PATH variable. The second approach is to type: env more By entering this command, you will be able to see all environment variables set for you. Note that in the above example, we also used the pager more just to make sure any given output that does not fit in the terminal, will be displayed after pressing a button. A possible output you can get is similar to the following: PAGE 9

2 tiger% env USER=tom LOGNAME=tom HOME=/home/impala/u2/staff001/tom SHELL=/bin/csh TZ=GB-Eire SSH_CLIENT=161.74.139.68 1470 22 SSH_TTY=/dev/pts/41 TERM=vt100 DISPLAY=tiger:10.0 PWD=/home/impala/u2/staff001/tom MANPATH=/usr/local/man:/usr/man:/usr/share/man:/usr/openwin/man:/opt/SUNWspro/man:/opt/SUNWs pro_4.0/man:/opt/gnu/man:/opt/epc/man LD_LIBRARY_PATH=/opt/SUNWspro/lib:/usr/openwin/lib:/usr/dt/lib:/usr/local/lib:/usr/local/rvplayer5. 0 OPENWINHOME=/usr/openwin PATH=/usr/local/bin:/home/impala/u1/staff001/tom/bin:/bin:/usr/ccs/bin:/opt/SUNWspro/bin:/opt/SUNW spro_4.0/bin:/usr/openwin/bin:/opt/gnu/bin:/usr/ucb:/usr/local/rvplayer5.0 SAMPLE OUTPUT 6.7: UNCOMPRESSING ARCHIVES USING uncompress We can observe that there are some environment variables that we have not described so far. However, this is out of the scope of this document. Some of these variables may be set up by the organisation which uses UNIX and may serve some specific purposes for it. Note that we can also introduce our environment variables as well. Perhaps, some of them can be used by our shell scripts or applications we will create. 6.2.2 GLOBAL AND LOCAL VARIABLES Environment variables have a global scope. However, if a user or a process assigns a value to a variable, this value has a local scope. This is due to that these kind of values cannot be passed to the parent process i.e. processes that initialised a process. Of course, these values may be passed to child processes i.e. processes that a given process has initialised. Typically, when we set up a variable by assigning a value to it, we use the set command. However, this command refers to a variable with local scope. The solution is the use of setenv command. Any variable introduced can be used by parent and child processes. PAGE 10

5 tiger% set MY_NUMBER_ONE = "Helena" 6 tiger% echo $MY_NUMBER_ONE Helena 7 tiger% sh $ echo $MY_NUMBER_ONE $ csh 1 tiger% echo $MY_NUMBER_ONE MY_NUMBER_ONE: Undefined variable 2 tiger% setenv MY_NUMBER_ONE "Helena" 3 tiger% echo $MY_NUMBER_ONE Helena 4 tiger% sh $ echo $MY_NUMBER_ONE Helena $ csh 1 tiger% echo $MY_NUMBER_ONE Helena 2 tiger% SAMPLE OUTPUT 6.8: WORKING WITH GLOBAL AND LOCAL VARIABLES By setting MY_NUMBER_ONE = "Helena", we defined a new variable called MY_NUMBER_ONE and assigned into it the value Helena. Then we used echo in order to echo its value in the terminal display. However, after switching to Bourne shell by typing sh we realised that our variable does not exist. This is the reason, we obtained an empty line. We switched back to C Shell and tried to echo our previously defined variable. At this point, we received an error message MY_NUMBER_ONE: Undefined variable. The problem generated because MY_NUMBER_ONE had a local scope. We redefined our variable by using setenv MY_NUMBER_ONE "Helena" and repeated the previously elaborated steps. Since our variable now has a global scope it can be used by other processes. SCOPE C SHELL BOURNE SHELL Local Scope set <Variable_name>=value; Global Scope setenv <VARIABLE_NAME>=value; export <VARIABLE_NAME> (Korn or Bourne shell) TABLE 5.4: SETTING UP GLOBAL AND LOCAL VARIABLES PAGE 11

Table 5.4 summarises any commands related with setting variables with global or local scope for Bourne and C shells. The syntax of these commands is explained below. Moreover, note that we also describe the syntax of the command unset. This command that the opposite effects when used, foe example if there is a variable already set, unset will delete a variable with a name given as argument. C Shell set myvar = place value here setenv <variable_name> <value> unsetenv VARIABLE_NAME value Bourne Shell <viariable_name>=<value> export <variable_name> unset VARIABLE_NAME SYNTAX 6.2: set, setenv, unset SYNTAX Note that if we use the Bourne shell, there is no need to enter a command in order to set up a variable and assign a value in it. Moreover, make sure you use block capitals when you wish to define global and environment variables. While, small case letters should be used otherwise. 6.4 WRITING AND EXECUTING SIMPLE SCRIPTS Executing single commands is an efficient way to interact with the UNIX operating system. For example, you can get a list of files contained in a directory or get a list of all processes running in the background. Moreover, we can execute programs for text processing and so on. After learning all these basic concepts, we moved forward by understanding the notion of pipes and input/output redirection. However, sometimes so far, we may have thought if there is a way to execute several commands at once. In this section, we will start experimenting with writing simple scripts. Suppose that I wish to execute a command that prints out Hello Ioannis, welcome back. The date today is and so on. First, we need to type PAGE 12

my script and save it into a file. At this stage, we can use a text editor such as vi or emacs or we can use cat command and the file output redirections. Then, we need to execute it using the command csh that corresponds to the C Shell process. In this case, csh will act as an interpreter of the given script. In other words, csh will go through the given script line by line and execute every command contained in it. 16 tiger% cat > myhello echo Hello Ioannis... echo Welcome back! echo At this moment, the time and date are date echo Have a nice day experimenting with UNIX! 17 tiger% SAMPLE OUTPUT 6.9: WRITING SIMPLE SCRIPTS In sample output 6.9, we can see that we used car > myhello in order to generate our script. Every input entered went to the file myhello. Then, we invoked the interpreter csh in order to execute the script. The output of the script is described in sample output 6.10. 17 tiger% csh myhello Hello Ioannis... Welcome back! At this moment, the time and date are Wed May 25 18:09:01 BST 2005 Have a nice day experimenting with UNIX! 18 tiger% SAMPLE OUTPUT 6.10: EXECUTING SIMPLE SCRIPTS Note that in order to execute a script, you can run any shell simply by typing its name. For example, if I am currently running csh and wish to execute it using sh, I need to type sh myhello. Of course, if we would like to we can switch to another shell by typing ksh (Korn) or sh (Bourne). However, if I wish to logout by typing exit, I will be first moved in my default shell and then by typing exit, we can logout from UNIX. Although, we used the interpreter csh in order to execute the previously presented script, there is also another way. The second approach PAGE 13

refers to that we can execute the script directly. However, we first need to change its access rights and make it executable. 3 tiger%./myhello./myhello: Permission denied 4 tiger% ls -l myhello -rw------- 1 svigkoi staff 136 May 25 18:08 myhello 5 tiger% csh myhello Hello Ioannis... Welcome back! At this moment, the time and date are Wed May 25 20:36:29 BST 2005 Have a nice day experimenting with UNIX! 6 tiger% chmod 777 myhello 7 tiger%./myhello Hello Ioannis... Welcome back! At this moment, the time and date are Wed May 25 20:36:45 BST 2005 Have a nice day experimenting with UNIX! 8 tiger% SAMPLE OUTPUT 6.10: EXECUTING SIMPLE SCRIPTS Initially, we attempted to execute our script without changing its access rights. However, the operating system did not allow us to execute this script. We changed, its access rights and the execution of the script was performed successfully. In order to execute our scripts, we also need to enter the prefix./ that denotes our current directory. 6.5 CONTROLLING PROGRAMS In the previous section, we considered the execution of simple scripts. In general when a script is executed more than one processes will be generated. One corresponds to the invocation of the interpreter i.e. csh and the others to commands contained in the script. The interpreter is known as the parent process and any commands incorporated in the script are known as child processes. PAGE 14

6.6.1 JOB CONTROL When we execute pipelines or scripts the execution of several commands takes place. For example, if we are going to execute the command cat myhello wc c & by sending it in the background, we are going to get a reference from the shell like [1] 1523. This is regular but the process identifier given refers to the second command wc c. Therefore, we need a mean to control the termination of a group of commands. Observe that the reference given by the shell also contains a number in square brackets. In this case, the number is [1]. Therefore, in a similar fashion like terminating single processes, we can use the kill command. In this case we need to type kill %1. The command jobs allows us to display all jobs i.e pipelines or scripts that are currently executing. 40 tiger% csh myshellscript& [1] 29303 41 tiger% jobs [1] + Running csh myshellscript 42 tiger% SAMPLE OUTPUT 6.11: USING JOBS COMMAND jobs command displayed that there is a job running in the background. The job reference number is [1] and its name is csh myshellscript. The cross (+) symbol indicates the default job which refers to the most recently created job. All other jobs are denoted by (-). The default job is known as %%, as well. We can also move processes from the background to the foreground and vice versa. The commands used are known as fg (foreground) and bg (background). As an argument, we use the job identifier prefixed with a %. PAGE 15

52 tiger% mailx -s "Experimenting" tom & [1] 416 53 tiger% jobs [1] + Stopped (tty input) mailx -s Experimenting tom 54 tiger% fg %% mailx -s Experimenting tom (continue) Here is my message to Tom... <Ctrl-D> 53 tiger% SAMPLE OUTPUT 6.12: USING FG AND BG COMMANDS We sent mailx process in the background. We recalled it back by typing fg %% and then we continued working with it by typing an email message (content) to Tom. 6.6.2 EXECUTING JOBS AT A CERTAIN TIME Commands entered in the command prompt are executed almost immediately. However, sometimes we also need to execute commands later as they may require lots of processing power or they are likely to take a long time. We may also schedule the execution of commands for later. The corresponding command is known as at. 62 tiger% at 0330 at> echo It is 3:30 time to wake up! at> <EOT> commands will be executed using /bin/csh job 1117074600.a at Thu May 26 03:30:00 2005 63 tiger% SAMPLE OUTPUT 6.12: USING AT Using at, we scheduled the execution of an echo command for 3:30am. The system confirmed that by giving us a notification. This includes which commands will be executed by csh C-Shell and their job number identifier. In this case the job identifier is 1117074600. PAGE 16

We can also schedule the execution of several commands, at once. This can be carried out by submitting a file that contains a sequence of commands that should be executed. In this case, we also need to user the option f to indicate that. 64 tiger% at -f mycommands 0330 commands will be executed using /bin/csh job 1117074601.a at Thu May 26 03:30:00 2005 65 tiger% ps PID TTY TIME CMD 28620 pts/27 0:00 csh 1074 pts/27 0:00 ps 66 tiger% at -l 1117074600.a Thu May 26 03:30:00 2005 1117074601.a Thu May 26 03:30:01 2005 67 tiger% SAMPLE OUTPUT 6.13: USING AT Similarly, using this option we instructed the system to execute commands included in the file mycommands and we were notified about that. Next, in order to get a list of all scheduled processes, we used the same command with option l. As shown, there are two scheduled jobs at 3:30. The date details entered can have various forms that reflect future time. Although, we provide some, more information can be found in manual pages. 0330 Noon 06pm August 15 2005-05-26 now + 1 month etc. Jobs schedules for future execution are placed in a queue. In order to remove any of them we use the same command with r option that stands for remove. Moreover, we also need to give an argument which is the job number of the job we wish to remove. PAGE 17

66 tiger% at -l 1117074600.a Thu May 26 03:30:00 2005 1117074601.a Thu May 26 03:30:01 2005 67 tiger% at -r 1117074601.a 68 tiger% at -l 1117074600.a Thu May 26 03:30:00 2005 71 tiger% SAMPLE OUTPUT 6.14: REMOVING SCHEDULED JOBS Similar to this command is the batch. However, the system decides about the scheduling of a submitted job. Typically, a batch job is schedules by the system according to some principles like the system s workload. 7 tiger% batch < mycommands commands will be executed using /bin/csh job 1117104003.b at Thu May 26 11:40:03 2005 8 tiger% SAMPLE OUTPUT 6.14: REMOVING SCHEDULED JOBS We used input redirection in order to schedule the execution of commands that are included in file mycommands. As shown, we were informed by the system that the execution of commands included in mycommands file will take place on Thu May 26 11:40:03 2005. 6.6.2 EXECUTING LARGE PROGRAMS When we need to execute programs that are big in size or they contain algorithms which require lots of computational power, we may need to assign them a low execution priority and send them in the background. This is carried out by: nice <command> & While if a process is already running we can change its priority by using the command renice followed by its process identifier. 6.6.3 RUNNING PROGRAMS WHILE BEING LOGGED OFF PAGE 18

When we are in process of logging out, we may wish to allow some processes running. Moreover, sometimes we are remotely connected through unreliable connections and we might be disconnected without any warnings. In cases such as these, we can use the command: nohup <acommand> & Any standard output and error output will be redirected into a file called nohup.out. Moreover, the use of this command will assign to a command given as argument low execution priority. 6.6 EXERCISES 1. Create a directory called archive. a. Copy some files into this directory. b. Move into this directory and create a directory called dir1. c. From your $HOME copy some files into $HOME/archive/dir1. d. Make sure you are in your $HOME. e. Use pax to create an archive of archive directory with name archive.pax. f. Check its size by using an appropriate command. g. Compress this archive h. Find out the size of the compressed file. i. Remove directory archive and all sub-directories of it. j. Uncompress archive.pax. k. Extract archive.pax l. Check if every file or directory has been restored back. 2. Repeat exercise one and use tar command. 3. Develop a script called lls that has the following functionality: After execution it displays the contents of a directory in a long listing format. Use for execution the: a. csh interpreter b../ notation [Make sure you have changed lls access rights] PAGE 19

4. Develop a script that gives as output: Welcome <your log name goes here> Your terminal name is: <terminal name> Today s date is: <DATE> Have a nice day! and execute it using both approaches described in exercise 3. 5. Define a variable called MY_INFO and assign to it a value that corresponds to your name. The scope of this variable should be local. Display its contents on the terminal window. 6. Delete variable MY_INFO. Repeat exercise 5. Give this variable global scope. Change shell and use BOURNE shell. Display its contents on the terminal display. Return back to your C Shell. 7. Write a script that uses variable MY_INFO and its execution displays on the terminal window: Hello <your name> Welcome back! 8. Display the contents of each variable found on page 8. 9. Develop a script called alarm that contains a sleep command with duration 120sec. It also should display that The time is: <time and date> Alarm has been set up <At this point you should put the sleep command> Alarm stopped a. Execute this script in the background. b. Kill this job 10. Execute script developed in exercise 9 and assigned to it a low priority. 11. Execute alarm script again and schedule its execution after an hour. Check the list of all scheduled processes. PAGE 20

12. Execute alarm and allow the system to schedule its execution. Then execute it again indicating the system that you wish to execute it while you have to log off. PAGE 21