Chapter 1. Getting started with UNIX

Similar documents
Outline. Structure of a UNIX command

Unix Introduction to UNIX

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

Introduction to Linux

IMPORTANT: Logging Off LOGGING IN

Working with Basic Linux. Daniel Balagué

CS4350 Unix Programming. Outline

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Files and Directories

Unix Handouts. Shantanu N Kulkarni

Welcome to getting started with Ubuntu Server. This System Administrator Manual. guide to be simple to follow, with step by step instructions

CS/CIS 249 SP18 - Intro to Information Security

Introduction to the UNIX command line

Std: XI CHAPTER-3 LINUX

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

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

The Unix Shell & Shell Scripts

UNIX File Hierarchy: Structure and Commands

h/w m/c Kernel shell Application s/w user

Introduction: What is Unix?

Chapter-3. Introduction to Unix: Fundamental Commands

Set 1 MCQ Which command is used to sort the lines of data in a file in reverse order A) sort B) sh C) st D) sort -r

bash startup files Linux/Unix files stty Todd Kelley CST8207 Todd Kelley 1

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

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

CS Unix Tools. Lecture 2 Fall Hussam Abu-Libdeh based on slides by David Slater. September 10, 2010

5/8/2012. Encryption-based Protection. Protection based on Access Permission (Contd) File Security, Setting and Using Permissions Chapter 9

A Brief Introduction to the Linux Shell for Data Science

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

Computer Systems and Architecture

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

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

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

Mills HPC Tutorial Series. Linux Basics I

Unix File System. Class Meeting 2. * Notes adapted by Joy Mukherjee from previous work by other members of the CS faculty at Virginia Tech

INTRODUCTION TO LINUX

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.

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Perl and R Scripting for Biologists

Basic Linux Command Line Interface Guide

Lab Working with Linux Command Line

Getting your department account

Week 2 Lecture 3. Unix

Basic Linux Command Line Interface Guide

Practical Session 0 Introduction to Linux

Unix Filesystem. January 26 th, 2004 Class Meeting 2

Filesystem Hierarchy Operating systems I800 Edmund Laugasson

TEL2821/IS2150: INTRODUCTION TO SECURITY Lab: Operating Systems and Access Control

Getting Started. Running Utilities. Shells. Special Characters. Special Characters. Chapter 2 Unix Utilities for non-programmers

Files (review) and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Linux Command Line Primer. By: Scott Marshall

CSCI 2132 Software Development. Lecture 4: Files and Directories

Linux & Shell Programming 2014

Course 144 Supplementary Materials. UNIX Fundamentals

File System. yihshih

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Introduction to Linux Workshop 1

UNIX Quick Reference

Physics REU Unix Tutorial

Unix File System. Learning command-line navigation of the file system is essential for efficient system usage

Prof. Navrati Saxena TA: R. Sachan

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Operating Systems. Copyleft 2005, Binnur Kurt

Week Overview. Unix file system File types and file naming Basic file system commands: pwd,cd,ls,mkdir,rmdir,mv,cp,rm man pages

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

bash, part 3 Chris GauthierDickey

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

Introduction of Linux

INSE Lab 1 Introduction to UNIX Fall 2017

CHAPTER 1 UNIX FOR NONPROGRAMMERS

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

2) clear :- It clears the terminal screen. Syntax :- clear

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

Answers to Even- Numbered Exercises

LAB #7 Linux Tutorial

Introduction to UNIX Shell Exercises

CST8207: GNU/Linux Operating Systems I Lab Six Linux File System Permissions. Linux File System Permissions (modes) - Part 1

Introduction to Linux Part I: The Filesystem Luca Heltai

Introduction to Linux

A Brief Introduction to Unix

Introduction to Linux. Fundamentals of Computer Science

commandname flags arguments

d. Permissions 600 on directory dir and 300 on file dir/foo. c. Permissions 700 on directory dir and 200 on file dir/foo.

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

Unix Basics. UNIX Introduction. Lecture 14

UNIX System Administration

*nix Crash Course. Presented by: Virginia Tech Linux / Unix Users Group VTLUUG

Introduction to Unix: Fundamental Commands

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

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

Computer Systems and Architecture

Linux Systems Administration Getting Started with Linux

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.

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

Linux Command Line Interface. December 27, 2017

Overview of the UNIX File System

Hands-on Keyboard: Cyber Experiments for Strategists and Policy Makers

Transcription:

Chapter 1. Getting started with UNIX The core of the UNIX operating system is the kernel. The kernel keeps records of all programs (commonly called processes) running on the system and shares time among these processes according to a well-defined policy. Users of the UNIX system don't have a direct interaction with the kernel. Instead, the user always works with a program called the UNIX shell. The Super user This user is created during the installation process of HP-UX. Each user in the UNIX system has a unique number associated with the user name, called a User ID. The root user has ID 0; therefore, any user with that ID has super user privileges. represents a common user while # represents a super user. To log out, use the exit command at the shell prompt. Just after the login process, a user goes into a default directory set for that user. This default directory has a special significance and is called the user's home directory. Changing Your Password passwd Old password : New password : Re-enter new password : Password changed you must have a password between six and eight characters long, and it must be a combination of characters and numbers. The command history is saved in a file called.sh_history This file is kept in the user's home directory. history -3 (last 3 used command dilplayed using history command.) date whoami pwd Command Aliases alias dir=ls (here we can use dir command instead of ls command.) & Shell Startup Files The system startup file is common for all users of the system, while user startup files can be customized for every user. The system startup file is used for tasks that are common to all system users. The system startup file is called profile and is present in the /etc directory of HP-UX. The user startup file is called.profile ("dot profile") and is placed in a user's home directory. Page 1

The system startup file is executed first in the startup procedure, and then the user startup file is executed. Some Simple HP-UX Commands man command is used to get help on all HP-UX commands. pwd (command is used to print working directory) /home/boota whoami (this command is used to identify who is logged in.) boota who (display login names of all users logged into the system, along with their login time) operator pts/ta Aug 30 16:05 boota pts/tb Aug 30 15:59 w (tells how long the system has been up, current time, and what the logged-in users are doing.) 4:27pm up 1 day, 12:10, 2 users, load average: 0.07, 0.08, 0.09 User tty login@ idle JCPU PCPU what operator pts/ta 4:05pm 12 -sh boota pts/tb 3:59pm w W=uptime w (uptime w do same job that w command do. Both are similar.) uname -a (display system name & version of HP-UX running on the system.) HP-UX myhp B.11.00 E 9000/800 71678 8-user license The system name is myhp and it is running HP-UX version 11.00. Chapter 2. Working with Files and Directories Creating a File cat > newfile (this command is used to create a text file& CTRL+D to save it) This is first line. <ENTER> This is the second line. <ENTER> This is third and last line. <ENTER> <CTRL-d> ls newfile (ls without any argument list all file present in current dir.) newfile ll (long listing of all files in current directory & is similar to ls -l) total 350 -rw-r----- 1 boota users 104230 Aug 27 19:04 FORMAT -rw-rw-rw- 1 boota users 0 Aug 30 20:47 myf -rw-rw-rw- 1 boota users 72 Aug 30 20:47 newfile Deleting Files rm newfile (it remove file without any warning & it does not delete hidden files.) Displaying Contents of a Text File cat newfile (cat command without ">" symbole can be used to display text file contents.) Page 2

This is first line. This is the second line. This is third and last line. more newfile (command display next one page at a time when user hit spacebar) File Naming Rules Generally a file name in UNIX can be as long as 256 characters. There are no special names for executable files in UNIX; the file permissions show which file is executable and which is not. Any file that starts with a dot (.) is not displayed when using the ll or ls command. These are hidden or invisible files. It can be listed using ls a command. Creating Directories mkdir newdir (it make a new directory & first character in the file permissions is "d" instead of "-". ) Deleting Directories rmdir newdir (this will delete only empty directory) rm rf newdir (this will delete entire non empty directory without any warning.) Understanding Directory Structure The top-level directory is called the root directory and is represented by "/" symbol. A directory one level above is called a parent directory, while a directory one level below is called a child directory. cd /etc (command to change directory ) cp myfile anotherfile (copy content of one file to other file in same directory.) cp /etc/profile myprofile (copy profile from /etc directory to current directory with myprofile) "." is a reference to the current directory and ".." is a reference to the parent directory of the current directory. cp file1 file2 /tmp (multiple file can be copied using cp command but the destination must be a directory name.) Page 3

mv myfile newfile (command used to rename file here myfile renamed with newfile.) mv myfile /tmp/myfile (it can also be used to move from one directory to other.) mv file1 file2 /tmp (multiple file can be moved but destination must be a directory name.) mv I myfile /tmp/myfile (in this case it will ask to override in case of same file name.) Wildcards The * matches zero or more characters, whereas? matches only one character. ls myfile* myfile myfile00 myfile01 myfile010 ls myfile0? myfile00 myfile01 (Here myfile010 has not been listed.) cp * /tmp (this can be used to copy all file from current directory to other directory.) grep root /etc/* (used to find word root from all file of /etc directory.) ls /etc/[w,x]* ( [] this is used to give the range ) /etc/wall /etc/whodo /etc/wtmp /etc/xtab file /etc/profile (command is able to detect the type of file.) /etc/profile: ascii text Searching a Word grep Mark /etc/passwd mstyle:elby:2216:125:mark Style,,,:/home/mstyle:/usr/bin/sh mbuna:tqfwuno:2318:125:mark Buna,,,:/home/mbuna:/usr/bin/sh grep I (to make a search case insensitive,) grep c (without displaying the lines containing the string) grep v (all lines that don't match the string pattern are displayed.) head /etc/passwd (head command display first 10 line.) tail -n 3 /etc/passwd (it will display last 3 line of a file.) tail -f (This is a very useful tool to see text being added.) wc /etc/profile (to count lines, words, and characters.) 171 470 3280 /etc/profile ln myfile abc ( command to create a hard link to a file abc, to a file, myfile ln command is used.) ln -s myfile abc (To create a soft link, we use the -s option.) Chapter 3. Environment Variables you can set a variable at the command prompt just by entering a variable name followed by an "=" sign and the value you want to assign to the variable. a variable name can start with characters of the alphabet only, not with numbers. VAR3=TestVar The echo command is used to view the value of a particular shell variable. echo VAR3 TestVar If you want to list all variables known to your current shell, use the set command. set Page 4

EDITOR=vi EPC_DISABLED=TRUE NAME="Mike Ron" (To set variables containing multiple words we use single or double quotes.) Modifying a Variable NAME="NAME Junior" echo NAME Mike Ron Junior A shell variable can be removed by the unset command on HP-UX. NAME="Mike Ron" echo NAME Mike Ron unset NAME echo NAME sh: NAME: Parameter not set. echo "My login name is `whoami`" My login name is boota Chapter 4. Input/Output Redirection and Pipes File descriptor 0 is used with standard input, 1 with standard output, and 2 with standard error. To redirect the output of the cat command we use the following step. cat newfile > file1 (Now the cat command displayed nothing, as the output of the command is redirected to a file. If we check the contents of file file1, it will contain the same text as newfile) Appending to a File cat file1 >>file2 (This command means that file2 still contains the old contents of file2. In addition to this, the contents of file1 are added to the end of file2.) Redirecting Standard Input mail jane <myfile (The mail program sends an email message to user jane on the current system consisting of the contents of myfile) Redirecting Standard error ll xyz 2>abc (Now there is nothing displayed because the error message has been stored in a file with name abc.) Page 5

Chapter 5. Using the vi Editor Modes Used in vi There are three modes used in the vi editor. These are the command mode, the last line mode, and the insert or input mode. The insert mode is also called the text entry mode. When you start vi, it is in command mode. This means that whatever you type is considered a command by the editor. You can switch to text entry mode or insert mode by pressing i at any time in the command mode. ESC key is used to come out from text mode to command mode. Starting and Stopping vi The editor is started when you use a vi command and give a file name as its argument. vi myfile To save the file use the :w command. After you save the file, you can quit the editor with the :q command. You can also use :x or :wq to save and quit in one step. Note that you can use the vi command without any file name. In that case the editor will start with an unnamed file buffer in the memory. When you need to save a file, you can use :w filename instead of :w in the command mode. Multiple files can be opened with vi by supplying multiple file names at the command line. If you want to open three files (file1, file2, file3) simultaneously, the command will be as follows. vi file1 file2 file3 If you made some changes to a file and don't want to save them, you can quit the vi editor with the :q! command. You can add the exclamation symbol to any command when you want to force vi to do something. Cursor Movement Table 5-1. Cursor Movement Commands Command Effect l Move one character right h Move one character left j Move one line down k Move one line up <space> Move one character right G Go to last line of the file ng Go to line number n in the file Go to end of current line ^ Go to start of line w Go to beginning of next word b Go to beginning of previous word e Move to end of word H Go to first line of screen M Go to middle line of screen L Go to last line of screen ( Go to beginning of sentence ) Go to end of sentence { Go to beginning of paragraph } Go to end of paragraph Page 6

Inserting and Deleting Text HP UNIX NOTES BY Mr.SHIV KUMAR SUMAN Page 7

Undo and Redo You use the u command to undo the last change. You can also use the U command to undo all changes made to the current line. To redo something changed by undo, you can use the "." (dot) command. Page 8

Search and Replace Page 9

Cut, Copy, and Paste HP UNIX NOTES BY Mr.SHIV KUMAR SUMAN Importing and Exporting Text To insert a disk file into a location in the opened file, we use the :r filename command. You can export any number of lines so that they are saved as a new file on the disk. We use the w command for this purpose. As an example, if you have opened file1 in the editor and want to save lines 3 to 47 as file3, you can use the command :3,47w file3. If you want to save the opened file as a new file (to make a backup), the same command can be used without line numbers. To save the current file as file4, you use :w file4. Page 10

The vi editor has a configuration file with the name.exrc, which is stored in the home directory of each user. Chapter 6. Regular Expressions Page 11

Chapter 7. File Permissions There are three types of users in UNIX. They are: 1. the owner 2. the group 3. others Types of File Permissions For a proper operation, a directory should have read and execute permissions set. The following rules apply to directory permissions in addition to general file permissions: If read permission for a directory is not set, no file inside the directory can be listed or accessed. If execute permission of a directory is not set, files inside the directory can be listed with names only. This means ls will work but ll will not work. Also, no files inside the directory can be read or executed. Because of this, the execute permission for a directory is also called list permission. If only execute permission is set and read or write permissions are not set, a user can go into the directory with the cd command and execute a program inside the directory if the program name is known. Also, a file can be viewed with the cat command if the file name is already known. It means you can execute programs but can't see the files. If a directory contains a file that a user can't delete (no write permission), he or she can't delete the directory even though write permission is granted for the directory. Modifying File Permissions The superuser or owner of a file can modify its permissions. We use the chmod command for modifying permissions of a file. To grant permission to a user, we use the "+" symbol between the user and file modes. To revoke a permission, use "-" between the user and file modes. To exactly assign a permission, regardless of the previous permission, we use the "=" symbol. Page 12

Command to give same permission to all user = sign can be used. Page 13

Changing File Permissions Using Octal Numbers r= 4, w=2, x=1 A file that has all the permissions set can be considered as carrying weight 7 (4+2+1). If we want to grant all three (read, write, execute) permissions to all users, we can use 777 with chmod. Default File Permissions We can control default file permissions with the umask command. The umask command sets the mask for new files. The current mask value is displayed with the umask command when used without any argument. A new mask value can be set at any time. A better place for setting the mask value is the user startup file HOME/.profile so that the value is set as soon as a user logs in. Changing the Owner and Group of a File Any user other than the owner of the file can't change ownership of a file, except the superuser. chown - for changing the owner of a file. chgrp - for changing the group of a file. Page 14

Page 15

Special File Permissions HP UNIX NOTES BY Mr.SHIV KUMAR SUMAN There are three types of special file attributes: set user ID (SETUID), set group ID (SETGID), and sticky bit. We use the chmod command to set these special permissions to a file. If you are using a symbolic method, use u+s for setting SETUID and g+s for setting SETGID. Digit 4 represents SETUID and 2 represents SETGID. UID=4,GID=2 and Sticky Bit=1. As you can see, "x" is replaced by "s" in the file permission representation with either SUID or SGID. Sticky Bit The sticky bit is represented by "t" and can be set using the chmod command with the u+t symbolic method or 1 (one) in the fourth digit position of octal numbers. Finding Files Having a Particular Set of File Permissions If a system administrator wants to The following command lists all files for which SETUID is set, and anybody from group or others also has write permission to that file. find / -perm -u+s,g+w,o+w The id command is used to display the current user and group IDs. The switch user ID (su) command is used to change the user ID temporarily just as you used the newgrp command to change the group ID. Page 16

Listing ACL Changing ACL The newgrp Command Page 17

Chapter 8. UNIX File System Hierarchy Static and Dynamic Files There are two major types of file system hierarchies used in UNIX distributions. One of these is based on Berkley Distribution of UNIX (BSD) and the other is AT&T System V. The file system layout of HP-UX version 10.x and above is based on AT&T system V release 4 (SVR4). There are two major groups of these directories. One is the group in which system files are present and don't change frequently. This is the static part of the directory hierarchy. The other group contains those files and directories that are changed on a routine basis and are sometimes called dynamic files and directories. Static files and directories contain information that usually does not change after system installation. This part contains /sbin, /usr, /var, /tmp and the application directories under /opt. The Root Directory ( / ) All of the file system is viewed with reference to the root directory. The name root comes from the logical position of this directory. It is represented by a slash character (/). The Device Directory (/dev) The device directory contains all of the device files and are used to represent devices attached to the system. Files related to one type of device are kept in one subdirectory under /dev. Page 18

Figure 8-2. The device directory (/dev) hierarchy. The /etc Directory The /etc directory is where a system administrator spends much of his or her time. All of the system configuration files are placed in this directory. Files in the /etc/rc.config.d directory are configuration and control files for system startup and shutdown and other server and daemon processes and Files in the /etc/opt directory contain configuration files for applications installed on the system. A system administrator cannot place system configuration files in a directory other than /etc. Figure 8-3. The /etc directory hierarchy. The Home Directory (/home) This directory contains the home directories of all system users. A home directory for user linda will be /home/linda. The System Binary Directory (/sbin) This directory contains executable files needed at boot time. Under HP-UX, this directory also contains system scripts required at startup and shutdown time. Page 19

The most important script in the /sbin directory is the rc script that controls all of the system startup and shutdown processes. Figure 8-4. The /sbin directory subtree. The /stand Directory This is the directory where the HP-UX kernel is kept. The kernel configuration file is also present in this directory. The kernel file is /stand/vmunix and the configuration file is /stand/system. It also contains the /stand/build directory used to rebuild the new kernel. The /net Directory This is a reserved name for remote file system mount points. The Application Directory (/opt) The /opt directory is used for installing applications on an HP-UX system. The Temporary File Directory (/tmp) This directory has permissions for everybody to create or delete files. Most of the time, temporary files are placed in this directory and are deleted from time to time. Any applications that need to create temporary files should do so in the /var/tmp directory instead of /tmp. Page 20

The /usr Directory This is an important directory, as most of the HP-UX system files are placed here. The most important file types are user-related commands, libraries, documentation and manual pages, contributed software, and X-Window system files. Figure 8-6. Structure of the /usr directory /usr/bin This directory contains user commands, applications, and utilities. /usr/contrib Contains contributed software from other sources. /usr/include Header and include files used for programming. /usr/lib Libraries for programming and machine-dependent database files. A user may need to look into this directory to check the existence of certain libraries in case there are compilation or run-time errors. /usr/sbin Many system administration commands are placed here. /usr/share/man Manual pages for HP-UX commands. The Variable Files Directory (/var) This directory contains basically three types of files: log files, spool files, and temporary files created by applications. Page 21

Figure 8-7. Structure of the /var directory. Log Files Spool Files (/var/spool) Spool files related to print services are kept in the /var/spool/lp directory. Spool files for electronic mail go into /var/mail. Other spool files are kept in a directory under /var/spool. Temporary var files (/var/tmp) Temporary files generated by some applications go into the /var/tmp directory. The mail system also keeps its temporary files in the /var/mail directory. The lost+found Directory These files are usually created by the fsck command, which is used to check file system integrity from time to time and at boot time. The files that have no valid links are copied to this directory. Each file system contains one lost+found directory. Page 22

Some Useful Commands Related to File System Hierarchy The which Command which ls /usr/bin/ls This command is used to find out that in which directory command or executable files are located. The whereis Command whereis cat cat: /sbin/cat /usr/bin/cat /usr/share/man/man1.z/cat.1 This command is used to find or search and locate source, binary, and manual pages. Page 23

Chapter 9. Working with the POSIX Shell and Job Control POSIX Shell Capabilities The POSIX shell is the default shell for HP-UX users. There are two other shells used in HP-UX. One of these is called the restricted shell (/usr/bin/rsh), which is used to provide restricted access to some users. The key shell (/usr/bin/keysh) is a contextsensitive shell that may be handy to use with HP terminals. Controlling Resources (ulimit) The ulimit command is an intrinsic command of the POSIX shell and is used to limit user resources. This command is very useful if you want to specify a limit for resources. ulimit -c 1024 ulimit a Page 24

To reexecute a previous command, you use the r command with the command number. history 451 453 451 date r 451 Foreground and Background Jobs The user can't issue another command until the job is finished and the command prompt is back. If you want to start more than one job simultaneously, you need to start them in the background. To start a job in the background you can put the & symbol at the end of your command prompt. Usually, the jobs that don't need any interactive input are started in the background. ll /usr >mylist & [1] 11633 You can also list background-running jobs with the jobs command. jobs [1] + Stopped vi myfile [2] - Running vi file2 + sign means this is the current running command and means it will execute after completion of first. A -l switch with the jobs command shows the PID of all jobs. All suspended jobs can be resumed with the foreground (fg) command. The same command is used to bring background jobs to the foreground. fg %2 Job numbers are used with the percent (%) symbol with the fg command. Stopping a Running Job There is no direct way to stop a running job. We can bring a background job into the foreground and then suspend it. Waiting for Background Jobs to Finish At any point, if you want to wait for background jobs to be finished, just use the wait command. This command stops the command prompt until all background jobs are finished. wait To finish all background job. wait %2 To finish particular background job. Page 25

Chapter 10. Introduction to Shell Programming Anatomy of a Shell Program Let us go directly to our first program and analyze it. I have used the file name script-00 for this program. Contents of this file are shown below using the cat command. cat script-00 #!/usr/bin/sh # This is to show what a script looks like. echo "Our first script" echo "----------------" echo # This inserts an empty line in output. echo "We are currently in the following directory" pwd echo echo "This directory contains the following files ls Before looking into the program and explaining what each line does, let us see what happens if we execute it. We execute it from the current directory with the command line:./script-00 Our first script ---------------- We are currently in the following directory /home/boota This directory contains the following files PHCO_18132.depot myfile phco_18132.txt PHCO_18132.text phco_18131.txt script-00 The first program line shows which HP-UX shell will be used to execute commands found in the program. And is always starts with #! character combination and shows the full path of the executable program that will be used as shell. The subshell that will be used to execute the program is /usr/bin/sh, which is the POSIX shell. You can use other shells, such as C, by changing this to /usr/bin/csh. The echo command without any argument just prints a blank line. Steps for Creating a Shell Program In the first step, a file is created that contains commands and control structures. In the second step, you need to modify file permissions to make it executable. use the chmod u+x command to make it executable. Page 26

Debugging Shell Programs HP UNIX NOTES BY Mr.SHIV KUMAR SUMAN Debugging shell programs is a tricky business. You replace the first line of the program #!/usr/bin/sh with #!/usr/bin/sh -x. After that, when you execute the program, it displays each line on your terminal screen before executing it. The actual line present in the program is shown with a plus (+) sign in the start of the line. After that, its output is displayed../script-00 + echo Our first script Our first script + echo ---------------- ---------------- + echo + echo We are currently in the following directory We are currently in the following directory + pwd /home/operator + echo + echo This directory contains the following files This directory contains the following files + ls PHCO_18132.depot myfile phco_18132.txt PHCO_18132.text phco_18131.txt script-00 Using Variables cat script-01 #!/usr/bin/sh echo "Use of Variables" echo "----------------" echo TAB=table FUR=furniture echo "The TAB is an example of FUR" When this program is executed, the results are:./script-01 Use of Variables ---------------- The table is an example of furniture Page 27

cat script-02 #!/usr/bin/sh echo "Use of Variables" echo "----------------" echo TAB=table FUR=furniture echo "The TAB is an example of FUR" TAB=chair Echo "After change" echo "The TAB is an example of FUR" When this program is executed, the results are as follows. Note that the line used for printing the text is the same; only the variable value is changed../script-01 Use of Variables ---------------- The table is an example of furniture After change The chair is an example of furniture Using Command Line Arguments, or Positional Parameters Page 28

Let's see script-05, which shows how many command line arguments are provided, a list of all arguments, and the value of the first argument. This program is now shown using the cat command. cat script-05 #!/usr/bin/sh echo "Total number of command line arguments is: #" echo "These arguments are: *" echo "The first argument is: 1" When you execute the program with three arguments red, green, and blue, the result is as shown below../script-05 red green blue Total number of command line arguments is: 3 These arguments are: red green blue The first argument is: red The shift Command The shift command is used to move the command line arguments one position left. The first argument is lost when you use the shift command. Shifting command line arguments is useful when you perform a similar action to all arguments, one-by-one, without changing the variable name. The shift command throws away the left-most variable (argument number 1) and reassigns values to the remaining variables. The value in 2 moves to 1, the value in 3 moves to 2, and so on. Let's modify script-05 into script-06 as shown below using the cat command. cat script-06 #!/usr/bin/sh echo "Total number of command line arguments is: #" echo "These arguments are: *" echo "The first argument is: 1" shift echo "New first argument after shift: 1" shift echo "First argument after another shift: 1" Now let's execute script-06 with the same three arguments we used with script-05. You can see from the next result that after every shift, a new value is assigned to 1. This value is the variable that is just on the right side of 1 (i.e., 2)../script-06 red green blue Total number of command line arguments is: 3 These arguments are: red green blue The first argument is: red New first argument after shift: green Page 29

First argument after another shift: blue During the first shift operation, 1 value is lost forever and can't be recovered by the program. The shift command can also do multiple shift operations in one step. For this you need to supply an argument to the shift command. For example, shift 2 will shift two arguments in one step, such that the old values of 1 and 2 will be lost, the value of 3 will be assigned to 1, the value of 4 will be assigned to 2, and so on. Interactive Shell Programs Interactive shell programs can read user input at run time with the help of the read command. Most of the time you will use the echo command to display a message before the read command is executed. This message informs the user of what the program is expecting. These programs are used in situations where a program first checks some system parameter and then requires user input to perform an operation on it. As an example, if you want to talk to another user on the system, you may first want to see who is logged into the system. After getting a list of users, you may initiate conversation with a particular user using the talk command. The read Command The read command takes one line of input from the user and assigns it to a variable. The variable name is provided as an argument to the read command. After entering some text, the user presses the key. Below is script-07, which lists all users currently logged into the system and then waits for you to enter a user name. After getting the user name, it initiates conversation with that user using the talk command. cat script-07 #!/usr/bin/sh echo "Currently logged in users are:" who echo echo echo "Enter the name of the user to whom you want to talk" read NAME echo "initiating talk with NAME" talk NAME After you select a user, the program rings the other party and displays a message asking the other user to respond to your talk request. If that user accepts your request, a talk window appears. Before the talk window appears, the program executes as shown below. Here you initiate a talk session with a user linda../script-07 Currently logged in users are: boota pts/t0 Oct 18 17:53 linda pts/0 Oct 18 22:13 Enter the name of the user to whom you want to talk linda <The talk window appears which covers the full screen> The echo Command You have already used the echo command to display text on your screen. This command uses escape characters that can be used to format the displayed text to enhance its readability. The escape characters used with the echo command are listed in Table 10-2. Page 30

Exit code Page 31

Page 32

Page 33

Page 34

Page 35

Page 36

Page 37

Page 38

Page 39

Page 40

Page 41