UNIX specific commands #Wildcards

advertisement
However, this command
UNIX specific commands
rsh tofu date ‘>’ remote.output
#Wildcards
#File Access Permission
#Reading variable length character strings in UNIX
#bat - making an executable file equivalent to a dos
executes date on the remote host and saves the output
on the remote machine in a file named remote.
output. Note that you can write this command as
follows:
name.bat file.
rsh tofu ‘date > remote.output’
Remote
rlogin
Commands
rlogin maxwell.phys.ufl.edu
The system prompts you for your user name
and password. Editors such as emacs and nedit either
work in terminal mode with no gui command lines or
do not work at all.
Rsh/ssh1 Eecuting Commands Remotely rsh is not encoded,
ssh is.
rsh [-l user] [ n] [host] [command]
In default mode ssh –l coldwell maxwell.phys.ufl.edu
From Linux can execute emacs and nedit on
the remote machine
Option Argument
Function
Set prompt=’%/>’
-1 user
-n
command
host
Sets the login name on the remote
system to user. The default is your
current login name on the local
system.
Redirects standard input from
/dev/null. If the program you are
executing remotely attempts to read
from standard input, it receives an
end-of-file indicator.
The shell command to run on the
remote computer. If you don’t
specify a command, rsh acts just like
rlogi n and starts a shell on the
remote host.
The name of the remote computer.
When you use rs h, you have to be concerned about
which machine — the local one or the remote one —
is interpreting the commands:
The local machine interprets shell metacharacters that
are not quoted.
The remote machine interprets quoted
rnetacharacters.
For example, the following command executes the
date command on the remote host (tofu) but saves the
output in a file named local .output on your
computer:
rsh tofu date > locaLoutput
1
Phil Hughes and Viktorie Navratilova, Linus for Dummies,
3rd edition, IDG Books Worldwide, Inc, Foster City, CA, …,
pp 172-173.
Sets the prompt to path> similar to the dos default.
find — Locating a file that meets
specified
characteristics
as a practical matter
find -name "*.for" -print > dir.lst
places ./dirname/filename in the directory dir.lst with a
separate line for each file.
The find command searches the specified paths for
files that match the specified selection criteria. This
command is known for having the worst syntax in
Linux, but this reputation exists because of the
number of files you can specify. The following
information is only a small subset of what find can
do. Type find --help to print a usage message (a
message that is, at best, confusing). See also the man
page (enter man find) for more information on find.
find [paths] [expressions]
Option or ,4rqument Function
paths
Any number of locations in the file
hierarchy tree where find should start
its search.
expressions
Finds criteria.
The following table shows the expressions you can
use with the fi nd command:
Expression Choices What They Mean
-name name
Finds files whose names match name.
You can use regular expressions if you
quote them.
-newer name
Finds files that have been modified
more recently than file name.
-perm mode
Finds files whose permissions exactly
match mode (octal).
-user uname
Finds files owned by uname.
-print
Prints the names of each matching file.
-exec command
Passes command to the shell for
execution. Use { } to specify the
currently matching filename and \; to
terminate the object of the exec.
You have in your directory hierarchy a bunch of files
with names ending in .bak and you want to delete
them. Assuming that you are in your home directory,
use the following command:
find . -name “*bak’ -exec rm “{}“ \;
Keep in mind these characteristics about this
command:
The double quotation marks are necessary to prevent
the shell from interpreting the strings inside. Without
the quotation marks around * ba k, the shell would
expand the string to match all files in the current
directory whose names end with bak. Thus, the find
command would miss the files from any
subdirectories.
The braces also have special meaning to the shell, but
if they make their way to the f i n d command, they
are replaced with the currently matching filename.
The backslash in front of the semicolon means that fi
nd sees this character and interprets it as the end of
the arguments to
find.
ls — Displaying filenames and
information
Each file has a name and some
characteristics associated with the file,
including its size in bytes, various dates of
modification, owner, and permissions. The
ls command displays this information in a
variety of formats that you select using the
command-line options.
ls [path- names]
options can be added as in
ls -latr
Option
-a
-d
-f
-1
-r
-R
-t
-u
Function
Includes all files and directories in the
listing, including those with names
starting with a dot.
List directories only
Appends a character to each filename to
indicate its type: * for executable regular
files, / for directories, @ for symbolic
links, | for FIFOs, and -for sockets.
Displays a long listing of file
information, including permissions, size,
owner, and modification time.
Reverses the order of the sort (the
default sort order is alphabetical).
Recursively accesses files in any
subdirectories.
Sorts the listing by last modification
time.
Sorts the listing by last access time.
grep
bat
Finds all occurrences of a pattern within a
file of a group of files
grep “this is a test” *.for
The standard search routine is
grep "end" ./pathwithname
The shell is the equivalent of a Dos bat
file.
chmod +x fname
makes fname an executable file. Comment
lines begin with #. Arguments are passed as
$1 $2 etc.
The file compile
# this is a sample shell file
make $1
compile nlfit will make nlfit.
makefile (See also make.htm)
#this is a sample make file note that the
indents before the f77 must be tabs
nlfit: nlfit.o robmin.o
f77 –o nlfit nlfit.o robmin.o
nlfit.o: nlfit.for
f77 –c –O nlfit.for
robmin.o: robmin.for
f77 –c –O robmin.for
Reading variable length
character strings in UNIX
The normal fortran reads have trouble with
character variables in unix files. The first five lines
in the file named NA are strings of an unknown
length. Dos simply requires
CHARACTER*64 STATUS,CIO
CHARACTER*80 II(4)
STATUS='OLD'
CALL MAOPEN(8,NA,STATUS)
DO I=1,4
READ(8,'(A)')II(I)
ENDDO
READ(8,'(A)')CIO
PRINT*,' CIO=',CIO
The part of the character variable beyond the end of
the line, which is marked by <cr><lf> is extended
as blanks. The unix g77 does not do this and in fact
returns a character variable with the first part of the
string missing, unless the length is equal to the
number of characters in the variable.
c *** unix free input only
DIMENSION LCOM(6),IRCOM(6)
CHARACTER*64 STATUS,CIO
CHARACTER*80 II(4)
STATUS='OLD'
CALL MAOPEN(8,NA,STATUS)
J=1
IRCOM(1)=0
LCOM(1)=0
DO I=1,400
it=FGETC(8,ct)
Getc(ct) and putc(ct) are unix fortran routiines for
moving a single character from the terminal.
Fgetc(n,ct) and fputc(n,ct) are unix frotran routines
for moving single characters from the file n. These
both treat <cr> = zD = 13 and <lf> = zA = 10 as
characters. There is a warning in a unix for fortran
guide2 that the gets and fortran reads should not be
mixed. For this reason the file needs to be closed
after the character lengths ha ve been established
and then re-opened. The same guide mentions that
the fortran reads for the rest of the variables are
enormously faster than are the gets.
chead(i:i)=ct
IF(ct.EQ.CLF)THEN
LCOM(J+1)=I
IRCOM(J+1)=I
IF(I.GT.1.)CT=CHEAD(I1:I-1)
IF(CT.EQ.CR)THEN
IRCOM(J+1)=LCOM(J+1)-1
ENDIF
IF(J.EQ.5)GOTO 25
J=J+1
ENDIF
ENDDO
25
CONTINUE
JT=J
C
do j=1,JT
C
ibeg=20*(j-1)
C
print'(80A1)',(chead(i:i),i=LCOM(J
)+1,IRCOM(J+1)-1)
C
enddo
C *** THE PURPOSE OF THE ABOVE
UNIX LINES WAS TO FIND LCOM AND
IRCOM
CLOSE(8)
CALL MAOPEN(8,NA,STATUS)
DO I=1,4
NRED=IRCOM(I+1)-LCOM(I)-1
Mike Loukides, UNIX for FORTRAN Programmers, O’
Reilly and Associates, Inc, 103 Morris Street, Sebastopol, CA
95472, (1990, 1991) see page 63
2
II(I)=' '
READ(8,'(80A1)')(II(I)(K:K),K=1,NR
ED)
ENDDO
NRED=IRCOM(6)-LCOM(5)-1
CIO=' '
READ(8,'(80A1)')(CIO(K:K),K=1,nred
)
Wildcards3
The UNIX wildcards are:
*
The asterisk matches zero or more
characters (including numbers and special
characters).
?
The question mark matches any single
character (including numbers and special
characters).
[]
The sequence [selection] matches any
character appearing in selection. A group of
consecutive letters or numbers can be expressed
with a - (dash); for example, [a-z] means all English
lower-case letters.
The name * means “absolutely everything” (except
files beginning with . (dot), which are a special
case).
Remember that UNIX doesn’t consider
extensions separate; you don’t need to say *.* The
name myfile.* means “all filenames beginning with
myfile. “, the name myfile.? means “all filenames
beginning with myfile. and followed by exactly one
character”, and the name myfile.[Fo] means
“myfile.F, myfile.o, or both”. You can use as many
wildcards as you need in a filename. For example,
the filename *.? is perfectly legal and means “all
filenames that end with a period followed by one
character.”
It is legal to use a wildcarded filename in a
command that requires a single argument, provided
that the filename only matches a single file. If it
matches several files, UNIX will print the error
message “filename: Ambiguous”.
File Access Permission4
The letters on the left side of an ls -l report
show the file’s protection status, more
3
ibid, page 13 The words were scanned from the book
ibid, pp 14-15 The words were scanned from the book – up
to Bob.
4
commonly known as the access mode or mode bits.
Here is a sample report:
The far-left character shows whether the
entry represents a file or a directoiy. A - (dash)
indicates a file; the d indicates a directory. In the
listing above, mail is a directory. The other entries
show regular files. You may occasionally see b, c,
or s in this field. These letters stand for block
devices, character devices, and sockets, all of which
are beyond the scope of this book.
The remaining nine letters are one-bit flags
that control file access. A - indicates that the flag is
not set, while any other letter indicates it is. To
make the display more readable, UNIX uses an r to
show that a bit controlling read access is set, a w to
show that a bit controlling write access is set, and
an x to show that a bit controlling execution access
(i.e., permission to execute a program) is set. You
may also see s or S in fields where you expect an x.
These encode some high- order bits (e.g., the
setuid,” “setgid,” and “sticky” bits), which are also
beyond the scope of this book.
These flags are divided into three sets. One
set controls access for the file’s owner (the three
bits to the immediate right of the directory bit).
Thus, the sample report above shows that the owner
has read and write access to all three files. The
owner only has permission to execute the file
named program. The other two files look like
simple text files, which we wouldn’t want to
execute any way. The access bits have slightly
different meanings for directories, which we’ll
discuss later.
The next three bits control access for
members of the file’s group. Each file belongs to a
group, as does each user. When a file is created, it
belongs to the same group as its creator, although
you can change a file’s group membership with the
chgrp command. All the files in the previous report
belong to the group staff. Thus, in our sample
report, complex.txt can be read (but not written) by
staff members, dazarep.txi can be read or written by
members of the group, and program can be read,
written, or executed.
The rightmost three bits control access for
all others. Only the owner and group members can
read complex.txt; others have no access to it. Others
have both read and write access to dazarep.txt and
are also allowed to execute program.
Earlier we mentioned that directories had
special interpretations for these bits. You can
understand the read and write bits by realizing that
the directory is also a file. Listing the contents of a
directory means reading the directory and requires
read” access. Writing a directory means adding or
deleting a file from the directory and requires
“write” access. You can modify a file in a directory
without write access because modifying a file
doesn’t require any changes to the directory itself.
The execute bits mean something completely
different. For directories, execute access means
permission to “search” a directory for a file. In other
words, execute access means you are allowed to
look up and access files within the directory. If you
do not have execute access to a directory, you can’t
do any thing to it. On the other hand, if you have
only execute access, you can do quite a lot. You
can’t list the files in the directory (so you can’t do
anything with a file unless you already know its
name); you can’t add or delete files, but if you
already know the name of a file, you can read,
modify, or execute it.
The change-mode command, chmod, lets
you change file-access permissions. It is used like
this:
where:
who Indicates whose access you want to change
and is optional but usually present The u
indicates the file’s owner (user), g indicates
the file’s group, o indicates others, and a
indicates all (i.e., user, group, and others). If
who is omitted, it defaults to a.
op
Is either + (plus) to allow access or - (minus)
to restrict access.
Perm Indicates which permission you want to
change. The r indicates read access, w
indicates write access, and x indicates
execute access.
Forexample the command:
allows group write access for the file
complex. Formerly group members were not
allowed to write the file.
Bob uses: The command to set html
privelages is
% chmod –R 755 ligo
The R makes it recursive to all
subdirectories. Ligo is the name of the directory.
Stopping Programs5
To stop a program, press CrRL-c. Under
most circumstances, this will force the program to
terminate. If it doesn’t, any of a number of things
may have happened. For example, the program may
have sent a strange sequence of special characters to
your terminal that managed to confuse it. In these
cases, do the following:
I. Log in at another terminal.
2. Enter the command ps -x. This displays a list of
the programs you are running, in the following
form:
3. Search through this list to find the command that
has backfired. Note the process identification (PID)
number for this command.
4. Enter the command kill PID, where PID is the
identification number from the previous step.
5. 1f the UNIX shell percent prompt (%) has
appeared at your terminal, things are back to
normal, If it hasn’t, find the shell associated with
your terminal (identified by a tty number) and kill
it. The command name for the C shell is csh. For
the Bourne shell, it is sh. In most cases, this will
destroy any other commands running from your
terminal. Be sure to kill the shell on your own
terminal, not the terminal you borrowed to enter
these commands.
6. Check ps to ensure your shell has died. If it is still
there, take more drastic action with the command
kill -KILL PID.
7. At this point, you should be able to log in again
from your own terminal.
The ps command, which lists all the programs you
are running, also gives you useful information about
the status of each program and the amount of Cpu
time it has consumed. Note that ps lists all the
progmms you are running, including many
5
Inid pp 18-19
programs you may not know about (e.g., programs
that other programs execute automatically). For
more information, see the entry for ps in Section 1
of the UNIX Progrwnmer’s Reference Manual.
Download