More UNIX Utilities for Shell programming
Embed Size (px)
description
Transcript of More UNIX Utilities for Shell programming


at-at, batch--execute commands at a later time
Example Explanationat 6:30am Dec 12 < program
At 6:30am in the morning December 12th, start the job
at noon tomorrow < program
At noon tomorrow start the job
at 1945 pm August 9 < program
At 7:45 in the evening on August 9th, start the job
at now + 3 hours < program In three hours start the job
at 8:30am Jan 4 < program At 8:30 in the morning of January 4th, start the job.
at –r 83883555320.a Removes previously scheduled job 83883555320.a

awk--pattern scanning and processing language
Example Explanationawk ‘{print $1, $2}’ file Prints the first two fields of
file where fields are separated by white space
awk ‘/John/ {print $3, $4}’ file
Prints fields 3 and 4 if the pattern John is found
awk –F: ‘{print $3}’ /etc/passwd
Using a colon as the field separator, prints the third field of the /etc/passwd file
date | awk ‘{print $6}’ Sends the output of the date command to awk and prints the sixth field

banner--make posters
Example Explanation
banner Happy Birthday Displays Happy Birthday

basename--with a directory name delivers portions of the pathname
Example Explanation
basename /usr/location Strips off the prefix /usr/local and displays bin
scriptname=“’basename $0’”
Assigns just the name of the scriptname, $0, to the variable scriptname

bc--processes precision arithmetic
Example Explanation
bc << EOFscale=34.5+5.63EOF Output : 6.366
This is a here document. From the first EOF to the last EOF input is given to the bc command. The scale specifies the number of digits to the right of the decimal point. The result of the calculation is displayed on the screen
bc ibase=25101 (Output)2010100 (Output)^D
The number base is two. The number converted to binary (ATT only)

bdiff—compares two big files
bdiff compares two files that are too large for diff.

cal—displays a calendar
Example Explanation
cal 1997 Prints the calendar year 1997
cal 5 Prints the month of May for 1978

cat—concatenates and displays files
Example Explanationcat /etc/passwd Displays the contents of
the /etc/passwd file
cat –n file1 file2 >> file3
Concatenates file1 and file2 and appends output to file3. The –n switch causes each line to be numbered

chmod—change the permissions mode of a file
Example Explanationchmod +x script.file Turns on execute permission
for user, group, and others on script.file
chmod u+x,g-x file Turns on execute permission for user, and removes it from group on file
chmod 755 * Turns on read, write, and execute for the user, read and execute for the group, and read and execute for others on all files in the current working directory. The value is octal (111 101 101).

chown—changes owner of a file
Example Explanationchown john filex Changes the user id of
filex to john
chown –R ellie ellie Recursively changes the ownership of ellie for all files in ellie directory

clear, clears the terminal screen

cmp—compares two files
Example Explanationcmp file.new file.old If the files differ, the
character number and the line number are displayed

compress—compress, uncompress, zcat compress, uncompress files, or display expanded files
Example Explanation
compress –v bookbook:Compression:35.07% -- replaced with book.Z
Compresses the book into a file book.Z and displays the percentage that the file was compressed and its new name
lsBook.Z

cp — copies files
Example Explanationcp file1 file2 Copies the contents of file1
to file2cp chapter1 book Copies the contents of
chapter1 to the book directory. In the book directory chapter1 has its original name
cp –r desktop /home/tom/docs
Recursively copies the entire desktop directory into /home/tom/docs

cpio —copy file archives in and out
Example Explanationfind . –depth –print | cpio –pmdv homejohn/tmp
Starting at the current directory, find descends the directory hierarchy, printing each of the entries of the directory even if the directory does not have write permission and sends the filenames to cpio to be copied into the john/tmp directory in the /home partition

cron—the clock daemon
cron executes commands at specified dates and times. Regularly scheduled jobs can be specified in the /etc/crontab file. (Must have superuser privileges)

crypt—encode or decode a file
crypt encrypts and decrypts the contents of a file. The password is a key that selects a type of transformation

cut—removes selected fields or characters from each line of a file
Example Explanationcut –d: -f1,3 /etc/passwd Using the colon as field
delimiter, displays fields 1 and 3 of the /etc/passwd file
cut –d: -f15 /etc/passwd Using the colon as a field separator, displays fields 1 through 5 of the /etc/passwd file
cut –c1-3,8-12 /etc/passwd Cuts and displays characters 1 through 3 and 8 through 12 of each line from the /etc/passwd file
date | cut –c1-3 Sends the output of the date commands as input to cut. The first three characters are printed

date—displays the date and time or sets the date
Example Explanation
date +%T Displays the time as 20:25:51
date +20%y Displays 2096
date “+It is now %m/%d /%y”
Displays It is now 07/25/96

diff—compares two files for differences diff [-biw][-c] | -Cn
Example Explanationdiff file1 file21c1< hello there---> Hello there> 2a3 > I’m fine
Shows how each line of file1 and file2 differ. The first file is represented by the < symbol, and the second file by the > symbol. Each line is preceded by an ed command indicating the editing command that would be used to make the files the same

du—summarizes disk usage
Example Explanationdu –s /desktop Displays a summary of
the block usage for all the file in /desktop and its subdirectories
du –a Displays block usage for each file in this directory and subdirectories

echo—echoes arguments
System V echo options\b Backspace
\c Suppress new line
\f Form feed
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash
\0n N is a 1,2, or 3 octal value

egrep—searches a file for a pattern using full regular expressions
Example Explanationegrep ‘Tom|John’ datafile
Displays all lines in datafile containing the pattern either Tom or John
egrep ‘^ [A-Z]+’ Displays all lines starting with one or more uppercase letters

expr—evaluates arguments as an expression
Example Explanationexpr 5 + 4 Prints the sum of 5 +4
expr 5 \* 3 Prints of result 5*3. The asterisk is protected from shell expansion
num=0num=‘expr $num + 1’
After assigning 0 to variable num, the expr command adds 1 to num and results is assigned to num

fgrep—search a file for a character string
Example Explanation
fgrep ‘***’ Displays any line containing three asterisks from each file in the present directory. All characters are treated as themselves; i.e., metacharacters are not special
fgrep ‘ [ ] * ? $’ filex Displays lines in filex containing the string enclosed in quotes

file—determines the type of a file by looking at its contents
Example Explanation
file bin/ls/bin/ls:sparc pure dynamically linked executable
ls is binary file dynamically linked when executed
file gogo: executable shell script
go is a shell script
file junkjunk: English text
junk is a file containing ASCII text

find—finds files
Example Explanationfind . –name \*.c –print Starting at the present
working directory (do), finds all the ending files in dot c and prints the full pathname
find .. –type f –print Starting at the parent directory (dot dot), finds all files of type file; i.e. files that are not directories

finger—displays information about local and remote users
By default, the finger command displays information about each logged in user, including login name, full name, terminal line (prepended with a ‘*’ if write permission is denied), idle time, login time, and location if known

fmt—simple text formatters
Example Explanation
fmt –c –w45 letter Formats letter. The –c switch preserves the indentation of the first two lines within the paragraph and aligns the left margin of each subsequent line with that of the second line. The –w switch fills the output line of up to 45 columns

fold—folds long lines
Fold the contents of the specified filenames, or the standard input if no files are specified, breaking the lines to have maximum width. The default for width is 80. Width should be a multiple of 8 if tabs are present, or the tabs should be expanded

ftp—file transfer program
Example Explanationftp dbase.bridgeport.edu ftp to the machine
dbase.bridgeport.edu , a large repository run by UUNET service which handles e-mail and net news for UNIX systems
ftp –n 216.87.102.26 Opens a connection to the machine at 216.87.102.26 and does not attempt to autologin

getop(s)—parses command line options
The getopts command supersede getop. getops is used to break up options in command lines for easy parsing by shell procedures and to check for legal options.

grep—searches a file or a program
Example Explanationgrep Tom file1 file2 file3 grep displays all lines in file1,
files2, and file3 that contain the pattern
grep –in ‘^ton savage’ * grep displays all lines with line numbers from the files in the current working directory that contain tom savage if tom savage is at the beginning of the line, ignoring case

groups—prints group membership of user
• The command groups prints on standard output the groups to which you or the optionally specified user belong.

id—prints the username, user ID, group name and group ID
• id displays your user ID,user name, group ID, and group name. If your real ID and your effective ID’s do not match, both are printed.

jsh—the standard, job control shell
• The command jsh in an interface to the standard Bourne shell which provides all of the functionality of the Bourne shell and enables job control.

line—reads one line
line copies one line (up to a new line) from the standard input and writes it on the standard output. It returns an exit code of one on EOF and always prints at least a new line. It is often used within shell files to read from the user’s terminal.

logname—gets the name of the user running the process

lp(ATT)—sends output to a printer
Example Explanationlp –n5 filea fileb Send five copies of filea
and fileb to the printer
lp –dShakespeare filex Specify Shakepsepeare as the printer where filex will be printed

lpr(UCB)—sends output to a printer
Example Explanationlpr -#5 filea fileb Send five copies of filea
and filesb to the printer
lpr –PShakespeare filex Specify Shakespeare as the printer where filex will be printed

lpsat(ATT)—print information about the status of the LP print service

lpq(UCB)—print information about the status of the printer

ls —lists a contents of directory
Example Explanationls –alF The –a lists invisible files
(those files beginning with a dot), the –l is a long listing showing attributes of the file, the –F puts a slash at the end of directory filenames, a* at the end of executable script names, and an @ symbol at the end of symbolically linked list
ls-d a* If the argument to the –d switch is a directory, only the name of the directory displayed, not its contents

mail—mail, rmail—read mail or send mail to users
A recipient is usually a username recognized by login. When recipients are named, mail assumes a message is being sent. It reads from the standard input up to an end-of-file(Ctrl-D), or if reading from a terminal, until it reads a line consisting of just a period. When either of those indicators is received, mail adds the letter to the mailfile for each recipient.

mailx—interactive message processing system
The mail utilities listed above provide an interactive interface for sending, receiving, and manipulating mail messages. Basic networking utilities must be installed for some of the features to work. Incoming mail is stored in a file called mailbox, and after it is read, it sends to a file called mbox

make—maintains, updates, and regenerates groups of related program and files
make updates file according to commands listed in a description file, and if the target file is newer than the dependency file of the same name, make will update the target file.

mesg—permits or denies messages resulting from the write command
mesg without argument –n forbids messages via write by revoking no user write permission on the user’s terminal. mesg with argument –y reinstates permission. All by itself, mesg reports the current state without changing it.

mkdir—creates a directory

more—browse or page through a text file
more is a filter that displays the contents of a text file on the terminal, one screenful at a time. It normally pauses after each screenful, and prints “—More—” at the bottom of the screen.

mv—move or rename files
Example Explanation
mv file1 newname Renames files1 to newname. If newname exists its contents are overwritten
mv –i test1 tes2 train Moves files test1 and test2 to the train directory. The –i switch is for interactive mode, meaning is asks first before moving the files

nawk—pattern scanning and processing language
nawk scans each input filename for lines that match any of a set of patterns. The command string must be enclosed in single quotes(‘) to protect it from the shell. awk programs consists of a set of pattern/action statements used to filter specific information from a file, pipe, or stdin.

newgrp—log in to a new group
newgrp logs a user into a new group by changing a user’s real and effective group ID. The user remains logged in and the current directory in unchanged. The execution of newgrp always replaces the current shell with a new shell, even if the command terminates with an error (unknown group)

news—prints mew items
news is used to keep the user informed of current events. By convention, these events are described by files in the directory /var/news. When invoked without arguments, news prints the contents of all current files in /var/news, most recent first, with each preceded by an appropriate header

nice—runs a command at low priority
/usr/bin/nice executes a command with a lower CPU scheduling. The invoking process (generally the user’s shell) must be the time-sharing scheduling class. The command is executed in the time-sharing class. An increment of 10 is the default. The increment must be a range between 1 and 19, unless you are the superuser. Also a csh built-in.

nohup—makes commands immune to hang-ups and quits
Example Explanation
nohup lookup & The lookup program will run in the background and continue to run until it has completed, even if the user logs off. Any output generated goes to a file in the current directory called nohup.out.

od—octal dump
od displays filename in one or more formats, as selected by the first argument. If the first argument is missing, -o is default; e.g., the file can be displayed in bytes octal, ASCII, decimal, hex, etc.

pack—pack, cat, unpack--compresses and expands files
pack compresses files. Wherever possible (and useful), each input file name is replaced by a packed file name.z with the same access modes, access and modified dates, and owner as those of name. Typically, text files are reduced to 60-70% of their original size. pcat does for packed file what cat does for ordinary files, except that pcat cannot be used as a filter. The specified files are unpacked and written to the standard output. Thus to view a packed file named name.z use: pcat name.z or just pcat name. unpack expands files created by pack.

passwd—changes the login password and password attributes
The passwd command changes the password or lists password attributes associated with the user’s login name. Additionally, privileged users may use passwd to install or change passwords and attributes associated with any login name.

paste—merges same line of several files or subsequent lines of one file
Example Explanation
ls | paste - - - Files are listed in three columns and glued together with a TAB
paste –s –s”\t\n” testfile1 testfile2
Combines a pair of lines into a single line using a TAB and new line as the delimiter. The –s switch causes subsequent lines from testfile1 to be pasted first and then subsequent testfile2

pcat—(see “pack”)

pg—displays files a page at a time
The pg command is a filter that allows you to page through one sreenful at a time on a terminal. If no filename is specified or it encounters the file name -, pg reads from standard input. Each screenful is followed by a prompt. If the user types a RETURN, another page is displayed. It allows you to back up and review something that has already passed.

pr—prints file
Example Explanation
pr –2th “TITLE” file1 file2
Prints two columns double sided with header “TITLE” for file1 and file2

ps—reports process status
Example Explanationps –aux | grep ‘^linda” ucb
Prints all processes running and pipes the output to the grep program printing only those processes owned by user linda, where linda is at the beginning of each line
ps –ef |grep ‘^ *linda’ att
Same as the first example, only the ATT version

pwd—displays the present working directory

rcp—remote file copy
Example Explanation
rcp dophin:filename /tmp/newfilename
rcp filename broncos:newfilename

rsh—starts a remote shell
Example Explanation
rsh bluebird ps –ef Connect to machine bluebird and display all processes running on that machine
rsh –l john owl ls; echo PATH. cat .profile
Go to the remote machine owl as user john and execute all three command

ruptime—shows the host status of local machines
ruptime gives a status line like uptime for each machine on the local network; these are formed from packets broadcast by each host on the network once a minute. Machines for which no status has been received for five minutes are shown as being down. Normally, the listing is sorted by host name, but this order can be changed by specifying one of ruptime’s options

rwho—who is logged in on local machine
The rwho command produces output similar to who, but for all machines on your network. However, it does not work through gateways and host must have the directory /var/spool/rwho as well as the rwho daemon running. If no report has been received from a machine for five minutes, rwho assumes the machine is down, and does not report users last known to be logged into that machines. If a user has no typed to the system for a minute or more, rwho reports this idle time. If a user has not typed to the system for an hour or more, the user is omitted from the output of rwho, unless the –a flag is given

script—creates a typescript of a terminal session
Example Explanationscript Starts up a script
session in a new shell. Everything displayed on the terminal is stored in a file called typescript. Must press ^d or exit to end the session
script myfile Starts up a script session in a new shell, storing everything displayed on the terminal in myfile. Must press ^d or exit to end the session

sed—stream editor
Example Explanationsed ‘s/Elizabeth/Lizzy/g’ file
Substitute all occurrences of Elizabeth with Lizzy in file and display on the terminal screen
sed ‘/Dork/d’ file Remove all lines containing Dork and print the remaining lines on the screen
sed –n ‘s5, 20p’ file Print only lines 15 through 20

size—prints section sizes in bytes of object files
The size command produces segment or section size information in bytes for each loaded section in ELF or COFF objects files. size prints out the size of the next, data, and bss (uninitialized data) segments (or section) and their total

sleep—suspends execution for some number of seconds
Example Explanation(sleep 105; command)& After 105 seconds,
command is executed. Prompt returns immediately
(In Script) while true do command sleep 60 done
Enters loop; executes command and sleeps for a minute before entering the loop again

sort—sort and/or merge file
Example Explanationsort filename Sorts the lines alphabetically
sort –u filename Sorts out duplicate entries
sort –r filename Sorts in reverse
sort +1 –2 filename Sorts starting on field 1 stopping at field 2 rather than sorting to the end of line
sort –2n filename Sorts the third field numerically
sort –t: +2n –3 filename Sorts numerically starting at field 3 and stopping at field 4, with the colon designated as the field separator (-t:)

spell—finds spelling errors
spell collects word from the named filenames and looks them up in a spelling list. Words that neither occur among nor are derivable from words in the spelling list are printed on the standard output. If no filenames are named, words are collected from the standard input

split—splits a file into pieces
Example Explanation
split –500 filea Splits filea into 500 line files. Files are named xaa, xab, aac, etc.
split –100 fileb out Splits fileb into 1000 line files names outaa, outab, etc.

strings—finds any printable string in an object or binary file
Example Explanation
strings /bin/awk |head –2
Prints any ASCII text in the first two lines of the binary executable /bin/nawk.

stty—sets the options for a terminal
Example Explanation
stty erase <Press backspace key> or ^h
Sets the backspace key to erase
stty –echo; read secretword; stty echo
Turns off echoing; waits for user input, turns echoing back on
stty –a (ATT) or stty –everything (BSD)
Lists all possible options to stty

su—become super-user or another user
su allows one to become another user without logging off. The default username is root (superuser). To use su, the appropriate password must be supplied (unless the invoker is already root). If the password is correct, su creates a new shell process that has the real and effective user ID, group Ids, and supplementary group list set to those of the specified username. The new shell is specified, sh (Bourne shell) is used. To return to normal user ID privileges, type Ctrl-D to exit the new shell. The - option specifies a complete login.

sum—calculates a checksum for a file

sync—updates the super block and sends changes blocks to disk

tabs—sets tab stops on a terminal

tail—displays the tail end of a file
Example Explanation
tail +50 filex Displays contents of filex starting at line 20
tail –20 filex Displays the last 20 lines of filex
tail filex Displays the last 10 lines of filex

talk—allows you to talk to another user
Example Explanation
talk [email protected]
Opens a request to talk to user sbenayed on machine called dbase.bridgeport.edu

tar—stores and retrieves files from an archive file, normally a tape device
Example Explanationtar cvf /dev/diskette Sends all files under the
present working directory to tape at device /dev/diskette and prints the files that are being sent
tar tvf /dev/fd0 Displays the table of contents of what is on tape device /dev/fd0
tar xvf /dev/fd0 Extracts from tape all files and prints what files were extracted

tee—replicates file standard output
Example Explanation
date | tee nowfile The output of the date command is displayed on the screen and also stored in nowfile

telnet—communicates with a remote host
Example Explanation
telnet jordan.bridgeport.edu
Opens a session with the remote host jordan.bridgeport.edu

test—evaluates an expression
Example Explanationtest 5 gt 6 The test command
command performs an integer test to see if 5 is greater than 6
echo $? (Bourne and Korn shells)(output is 1, meaning the result of the test is not true.)
The $? Variable contains the exit status of the last command. If a nonzero status is reported, the test results are not true; if the return status is zero, the test result is true

time—displays a summary of time used by this shell and its children

touch—updates access time and/or modification time of a file
Example Explanation
touch a b c Three files, a, b, and c are created. If any of them already exist, the modification timestamp on the files is updated

tput—initializes a terminal or queries the term info database
Example Explanation
tput longname Displays a long name for the terminal from the terminfo database
bold=‘tput smso’Unbold=‘tput rmso’Echo “${bold}Enter your id: ${offbold}\c”
Sets the shell variable bold to turn on the highlighting of displayed text. Then sets the shell variable, unbold, to return to normal text display. The line Enter your id is highlighted in black with white letters. Further text is displayed normally

tr—translates characters
Example Explanationtr ‘A’ ‘B’ < filex Translates As to Bs in
filex
tr ‘[A-Z]’ [a-z] < filex Translates all uppercase letters to lower case letters
tr –d ‘ ‘ < filex Deletes all spaces from filex
tr –s ‘\11’ ‘\11’ , filex Replaces (squeezes) multiple tabs with single tabs in filex

true—provide successful exit status
true does nothing, successfully, meaning that it always returns a zero exit status, indicating success. Used in Bourne and Korn shell programs as a command to start an infinite loopwhile truedo
commanddone

tsort—topological sort
The tsort command produces, on the standard output, a totally ordered list of items consistent with a partial ordering of items mentioned in the input filename. If no filename is specified, the standard input is understood. The input consists of pairs of items (nonempty strings) separated by blanks. Pairs of different items indicate ordering. Pairs of identical items indicate presence, but not ordering

tty—gets the name of the terminal
tty prints the path name of user’s terminal

umask—sets file-creation mode mask for permissions
Example Explanationumask Displays the current file
permission mask
umask 027 The directory permissions, 777, minus the umask is 750. The file permissions, 666, minus the umask 027 is 640. When created, directories and files will be assigned the permissions created by umask

uname—prints name of current machine
Example Explanation
uname –n Prints the name of the host machine
uname –a Prints the operating system name, and the operating system version—same as -m, -r, -s, and –v

uncompress—restores files to their original state after they have been compressed using the compress command
Example Explanation
uncompress file.Z Restores file.Z to its original state; i.e., what was before compressed

uniq—reports on duplicate lines in a file
Example Explanation
uniq file1 file2 Removes duplicate adjacent lines from file1 and puts output file2
uniq –d –2 file3 Displays the duplicate lines where the duplicate starts at third field

units—converts quantities expressed in standard scales to other scales
units converts quantities expressed in various standard scales to their equivalents in other scales. It works interactively in this fashion :You have:inch
You want:cm
*2.540000e+00
/3.937008e-01

unpack—expands files created by pack
unpack expands files created by pack. For each filename specified in the command, a search is made for a file called name.z (or just name, if name ends in .z). If this file appears to be packed file, it is replaced by its expanded version. The new file has the .z suffix stripped from its name, and has the same access modes, access and modification dates, and owner as those of the packed file

uucp—copy files to another system, UNIX-to-UNIX system copy
Example Explanationuuencode mybinfile decodename > uumybinfile.tosend
The first argument, mybinfile, is the existing file to be encoded. The second argument is the name to be used for the uuencoded file, after mailing the file, and uumybinfile.tosend is the file that is sent through the mail
uuencode uumybinfile.tosend
This decodes the uuencoded file and creates a filename which was given as the second argument to uuencode

uuencode—uuencode, uudecode—encode a binary file into ASCII text in order to send it through e-mail, or convert it back into its original form
Example Explanationuuencode mybinfile decodedname > uumybinfile.tosend
The first argument, mybinfile, is the existing file to be encoded. The second argument is the name to be used for the uuencoded file, after mailing the file, and uumybinfile.tosend is sent through the mail
uucode uumybinfile.tosend
This decodes the uuencoded file and creates a filename which was given as the second argument to uuencode

wc—counts lines, words, and characters
Example Explanationwc filex Prints the number of
lines, words, and characters in filex.
who | wc –l The output of the who command is piped to wc, displaying the number of lines counted
wc –l filex Prints the number of line in filex

what—extracts SCCS version information from a file by printing information found after the @(#) pattern
What searches each filename for the occurrence of the pattern, @(#), that the SCCS get command substitutes for the %Z% keyword, and prints what follows up to a “>, new line, \, or null character

which(UCB)—locates a command and displays its pathname or alias
which takes a list of names and looks for the files that would be executed had the names been given as commands. Each argument is expanded if it is aliased, and searched for along the user’s path. Both alias and path are taken from the user’s .cshrc file. Only .cshrc is used

whereis(UCB)—locates the binary, source, and manual page file for a command

who—displays who is logged on the system
Example Explanation
who Displays the users logged on the system
who ami Displays information about the current user

write—writes a message to another user
Writes copies to lines from your terminal to another’s terminal

xargs—constructs an argument list(s) and executes a command
Example Explanationls $1 | xargs –i –t mv ${} $2/{}
Moves all files from directory $1 to directory $2, and echoes each mv command just before executing
ls | xargs –p –l rm –rf Prompts (-p) the user files are to be removed at a time and removes each one

zcat—uncompress a compressed file to standard output. Same as uncopmress -c
Example Explanationzcat book.doc.Z | more
Uncompress books.doc.Z and pipes the output to more

References UNIX SHELLS BY EXAMPLE BY ELLIE
QUIGLEY UNIX FOR PROGRAMMERS AND
USERS BY G. GLASS AND K ABLES UNIX SHELL PROGRAMMING BY S.
KOCHAN AND P. WOOD