0% found this document useful (0 votes)
5 views

Unix Fundamentals Chapter 02

The document provides an overview of UNIX fundamentals, including command structure, basic command examples, and how to get help using man pages. It covers essential commands like date, echo, passwd, and script, along with explanations of the PATH variable and session logging. The chapter serves as a foundational guide for users to navigate and utilize UNIX commands effectively.

Uploaded by

gauravsom789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unix Fundamentals Chapter 02

The document provides an overview of UNIX fundamentals, including command structure, basic command examples, and how to get help using man pages. It covers essential commands like date, echo, passwd, and script, along with explanations of the PATH variable and session logging. The chapter serves as a foundational guide for users to navigate and utilize UNIX commands effectively.

Uploaded by

gauravsom789
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIX Fundamentals Workshop 39

______________________________________________________________________________

Chapter Two

Foundation

In This Chapter
Command Structure
Basic Command Examples
Some Simple Commands
Getting Help with Man Pages
Changing Your Password
The PATH Variable
Logging Your Session
Exercises
40 UNIX Fundamentals Workshop
______________________________________________________________________________
UNIX Fundamentals Workshop 41
______________________________________________________________________________

In This Chapter

Command Action

date display current system date and time

echo text print or ―echo‖ text to screen (via standard out)

man command display the online manual for a given command

man -k keyword search the online manuals‘ index for information related to
the given keyword

passwd change your UNIX password

script <filename> save the output of your shell session to a file – end with
Ctrl-d

sleep num_seconds delay a command by num_seconds


42 UNIX Fundamentals Workshop
______________________________________________________________________________
UNIX Fundamentals Workshop 43
______________________________________________________________________________

Command Structure

UNIX commands are entered at a shell prompt. Commands can invoke applications, scripts, filters,
and utilities. The syntax for UNIX commands is

command [options] [name(s)]


\_________________/
arguments

where options are optional command parameters, and name is the file, directory, or text string to per-
form the action upon. Together, the options along with any files or directories passed to the com-
mand become the command‘s arguments. The command plus its arguments are referred to as the
command line.

Syntax

Description Example

Commands are commonly entered in lower case. ls


Unix is case sensitive, so ―ls‖ is NOT the same as
―LS‖.

Commands and file or directory names are separated ls /dept/isa


by spaces or tabs (commonly referred to as whites-
pace)

Options are single letters preceded by a dash ls -l /dept/isa


( - ) and separated by spaces

Multiple options can be specified, if separated by ls -a -l -R /dept/isa


spaces. This is a common area for new users to make
mistakes because some DOS commands are forgiving
about the lack of spaces, but UNIX is not.

Multiple options can be combined. Note the spaces ls -alR /dept/isa


between commands, options, and file
and directory name

Error messages may appear, if incorrect file filename: not found


or directory names are entered filename: cannot open

Multiple commands can be entered on one line, if se- ls -alR /dept/isa ; cd ; ls


parated by a semicolon ( ; )

Commands are executed sequentially


44 UNIX Fundamentals Workshop
______________________________________________________________________________

Basic Command Examples

Command Action

pwd Display your current working directory

cat <filename> Display contents of file[s] to the screen

mailx userid@wnt Send email from command prompt (end with Ctrl-d)

script <filename> Save your session to a file – end with Ctrl-d

less <filename> Display contents of filename to screen, one page at a time

more <filename> Display contents of filename to screen, one page at a time

passwd Change your UNIX password.

stty -a List your terminal settings

man <command> View online manual for a specific command

man –k <string> Search online manual index for commands related to string

xclock & Display a clock application.

uname -a Display information about your system (OS version & level)

which <command> Display the location of command.

whereis <command> Follows the $PATH variable and displays all occurrences of
command found.

Note: To quit or exit any of the above applications, try one of these commands, keystrokes, or menu
selections: ctrl + c, ctrl + d, or exit.
UNIX Fundamentals Workshop 45
______________________________________________________________________________

Simple Commands

Displaying the Date & Time

The date command displays the current date and time. The command has several formatting op-tions
listed in the man pages. The command is useful when executed in conjunction with other commands,
to output the date and time of execution. It is frequently used in the generation of log files created
with shell scripts.

Display date...

prompt> date Enter


Mon May 7 15:12:51 EDT 2007

Display date in mm/dd/yy format.

prompt> date +%D Enter


05/07/07

Display full weekday name.

prompt> date +%A Enter


Monday

Display last two digits of year.

prompt> date +%y Enter


07

Display time zone name.

prompt> date +%z Enter


EST

Alert: Sometimes, the date or time displayed is incorrect!!! If it is wrong, contact your system ad-
ministrator and have them reset it (otherwise, things like caching can get confused).
46 UNIX Fundamentals Workshop
______________________________________________________________________________

Echoing Text

The echo command echoes the entered characters or associated shell variable contents. The com-
mand also has several escape char-acters, such \n to force a carriage return. The command is useful
when executed in conjunction with a series of other commands, within a shell script, to generate out-
put.

Echo text...

prompt>echo "Hello world Hello world" Enter


Hello world Hello world

Echo text containing single "n".

prompt>echo "Hello world n Hello world" Enter


Hello world n Hello world

Echo text containing "\n". Note carriage return.

prompt>echo "Hello world \n Hello world" Enter


Hello world
Hello world

Echo text containing "\t". Note tab.

prompt>echo "Hello world \t Hello world" Enter


Hello world Hello world

Echo contents of $DISPLAY variable.

prompt>echo $DISPLAY Enter


d8521.us.sas.com:0.0

Echo the output of the date command with helpful text (note the
quotes around the date command – they are called backticks).

prompt> echo "command last run: " `date` Enter


command last run: Thu Mar 10 13:31:52 EST 2005
UNIX Fundamentals Workshop 47
______________________________________________________________________________

Delaying a Command

The sleep command delays execution of one or more commands. The sleep command is useful for
testing output devices, such as delaying a printer job until a user can reach the printer room. The de-
fault format of the command‘s argument is in seconds.

In sixty minutes, echo text.

prompt>sleep 960; echo "Hello, World" Enter

960 seconds later...


Hello, World.
prompt>
48 UNIX Fundamentals Workshop
______________________________________________________________________________

Getting Help With man (manual) Pages


Online manual (or man) pages are available for most UNIX commands. Man pages are useful for list-
ing the options of a command or for troubleshooting a command.

The man command generates both a short synopsis of the command's arguments and a short descrip-
tion of the command's functions. In addition, many man pages include details examples of using the
command.

The command man -k keyword displays a list of all matching commands related to the given key-
word.

Look for commands related to keyword “permissions”:

prompt>man –k permissions Enter


chmod(), fchmod()(2) - change file mode access permissions
chmod(1) - change file mode access permissions
uucheck(1M) - check the uucp directories and per-
missions file
UNIX Fundamentals Workshop 49
______________________________________________________________________________

Anatomy of a Man Page

Most man pages contain the following sections:

Section Description

name name and short description of command

synopsis options available for command

description longer description of command

files related files

see also related commands

bugs known bugs

author author of man page


50 UNIX Fundamentals Workshop
______________________________________________________________________________

Example

DATEBOOK(1) DATEBOOK(1)

NAME
datebook - calendar and reminder program for X11

SYNOPSIS
datebook [-W] [-V] [-D date] [-F datefile]

DESCRIPTION
Datebook is a personal appointment calendar. It was written to solve
the never ending problem of forgetting when you need to attend a
meeting, go to the doctor, or phone home. Datebook provides calendar
features, that let you enter and review events for a particular day or
week, as well as reminder features that notify you before particular
events.
:
Each activity or appointment entered into Datebook is considered an
event. Events that are specified with a time will generate alarms.
In the X11 environment, an alarm will cause an alarm window to appear
on the user's screen. This window will present the user with the
option to acknowledge, delay (snooze) or delete the event in question.
Outside of X11 the user can have datebook call a program or script
when the alarm occurs (see AlarmNotify).

Events can be classified into various event classes. Each class can
have a different amount of time that the user will be warned in
advance of the event. For example, for local meetings you might want 5
minutes warning, but if the meeting is offsite 30 minutes warning
would be more appropriate. The class can also specify the time to
"snooze" before repeating an alarm. The class can also be used to
specify a bitmap to be displayed in the alarm window.

OPTIONS
-W List the events for the next week to stdout. This option will
not bring up X windows and will therefore work outside of X.

-D date This option is like the list week option, but it only lists
the events for a particular date. This date should be in a
simple date (month, day and year) like 12/14/89 or November
28. If used in combination with -W the week starting at date
will be listed.

-F datefile
This option allows the user to start with one or more (one -F
option per file) date files instead of the date files
specified by the ~/.datebookrc file.

-V Turns on the verbose mode. The program will print additional


information about the program state.

- 1 - Formatted: August 30, 1995


UNIX Fundamentals Workshop 51
______________________________________________________________________________

Man Page Sections

Some man pages have multiple sections, referenced as command(n) in the See Also section.

Example: See crontab(1) for more information.

Section Description

(1) User Commands

(1m) System Administration

(2) System Calls

(3) Subroutines

(4) File Formats

(5) Miscellaneous

(6) Games

(7) Devices

(8) System Administration

The command man n command displays section n of the man page for command.

Example: man 5 man

Example: man 1 crontab


52 UNIX Fundamentals Workshop
______________________________________________________________________________

Example Man Page Section

man(5) man(5)

NAME
man - macros for formatting entries in this manual

DESCRIPTION
These macros are used by nroff (and are usable by troff) to determine
the layout format of the on-line version of entries found in this and
other related reference manuals. These macros are used by the man
command (see man(1)).

The default page size is 8.5x11 inches, with a 6.5x10-inch text area.
The -rs1 option (which is ignored by nroff) reduces these dimensions
to 6x9 inches and 4.75x8.375 inches, respectively - and reduces the
default type size from 10-point to 9-point; the vertical line spacing
from 12 points to 10 points. The -rV2 option can be used to set
certain parameters to values appropriate for certain Versatec
printers: line length to 82 characters; page length to 84 lines;
underlining inhibited. This option should not be confused with the
-Tvp option of the man command, which is available on some UNIX
operating systems.

Any text argument below can consist of one to six ``words''. Double
quotes ( can be used to include blanks in a ``word''. If text is
empty, the special treatment is applied to the next line containing
text to be printed. For example, .I can be used to italicize a whole
line, or .SM followed by .B to make small bold text.

By default, hyphenation is turned off for nroff, but remains on for


troff. Paragraphs are left-justified, ragged-right for nroff, and
adjusted both left and right for troff.

Type font and size are reset to default values before each paragraph
and after processing font- and size-setting macros such as .I, .RB,
and .SM. Tab stops are neither used nor set by any macro except .DT
and .TH. .TH invokes .DT (see below).

Default units for indents in are ens. When in is omitted, the


previous indent is used. This remembered indent is set to its default
value (7.2 ens in troff, 5 ens in nroff - corresponding to 0.5 inch in
the default page size) by .TH, .P, .PP, and .RS, and restored by .RE.

.TH t s c n a Set the title and entry heading:


t Entry title.
s Section number. t is combined with s in
parentheses to form the top left- and right-
hand corners of the page heading.
c Extra commentary such as ``Optional Software
.
.
.
UNIX Fundamentals Workshop 53
______________________________________________________________________________

Changing Your Password

Use the passwd command to change your userid‘s password. If all goes well, the passwd
utility will update the user‘s password encryption in the /etc/shadow file (/etc/passwd
on some of the older systems). The /etc/passwd and /etc/shadow files are used to au-
thenticate you every time you log in.

Note: You can only change your own userid‘s password. A privileged user, such as root, can
change any user‘s password.

Execute the passwd command:

prompt>passwd Enter
passwd: Changing passwd for sssims
Enter login password: *********
New password: ********
Re-enter new password: ********
Passwd (SYSTEM): passwd successfully changed for sssims
prompt>
54 UNIX Fundamentals Workshop
______________________________________________________________________________

The PATH Variable

How does the shell know whether you have access to a command when you execute it? The shell
maintains an internal variable, called PATH, it its own environment. As such, you will commonly
hear such variables referenced as ―environment variables‖. The PATH variable is typically set to a
colon delimited list of directories. If you attempt to run a utility or command, and receive a message
such as ―command: not found‖, you can bet that the directory you need is not included in your PATH
variable.

Display current setting for PATH (note the need to preface the
variable with the dollar sign ($)).

prompt>echo $PATH Enter


/usr/bin:/sbin:/bin:/u/sssims/bin:.

In the above output, there are five directories listed. The last one
is signified by a „.‟ (referred to as “dot”). It signifies that your
current working directory is the last place the shell will look for a
command.

You can still execute a command that is not contained within a directory located in your PATH varia-
ble using one of these three methods:

 Use an absolute path to the command you wish to run, such as /usr/local/bin/sas

 Modify your PATH variable

 Change your current working directory to the location where the command resides. By ex-
ecuting it there, your PATH will pick it up because it will be in your current directory (the ―.‖
at the end of the PATH)

Note: For Windows users who are familiar with the PATH variable on that system – make
sure you note that UNIX uses a colon as the directory delimiter in the PATH variable, whe-
reas Windows uses a semicolon.
UNIX Fundamentals Workshop 55
______________________________________________________________________________

Logging Your Session – The script Command


The script command is probably one of the most useful, yet least used, utilities in UNIX. It
lets you ―record‖ your login session in a file. Not only does it record the commands that you
use, but also the output and errors of those commands as well.

Note: script will overwrite any information previously recorded to the file you specify.

Invoke script anytime after you log in:

prompt>script mysession.out Enter


Script started, file is mysession.out
prompt>

The shell prompt returns, and all input and output of your login
session will be recorded in the script file noted. When you are
ready to stop recording your session in the script file, enter the exit
command (or Control-D):

prompt>exit Enter
Script done, file is mysession.out

You can now view this file with the cat command

prompt> cat mysession.out Enter


.
.
.
56 UNIX Fundamentals Workshop
______________________________________________________________________________
UNIX Fundamentals Workshop 57
______________________________________________________________________________

Exercises
1. Which environment variable determines the search path for any program you run?

2. Start a script file – named myscript.out – located in your home directory. Your
prompt will return. With your script still running, do the following:

- display the current month

- display the current setting for your $PATH environment variable

- display the man page for script

3. Terminate your script file and view its contents using cat.
58 UNIX Fundamentals Workshop
______________________________________________________________________________

You might also like