Thursday, June 26, 2014

UNIX Information Commands

    man [-option] [command] (CR) : Manual or UNIX help command. The usual quit sequence `q (CR)' can be used to quit long UNIX `man' listings, `(CR)' is used for new `man' pages. During a IBM Telnet session the `Clear-key' is needed for new CMS pages that are not the same as the `man' pages. Otherwise `d', `q' or `Ctrl-c' should work for UNIX like access.

    finger [user] (CR) : Displays system biography on user `[user]'.

    whereis [name] (CR) : Locates source for program or command; e.g. `whereis kermit'.

    which [name] (CR) : Tell which version of a program or command will be used in your session in case of multiple copies; e.g. `which cc'  or which java

    whatis [command] (CR) : Describes the command [command].

    who am i (CR) : Displays current user id and access.

    who (CR) : Displays currently logged in users.

UNIX Process Commands


    jobs - l (CR) : Display a simple single line with currently active job status.

    ps (CR) : Display current process ids (``pid'') needed for killing.

    ps t[N] (CR) : Displays ``pid'' for terminal or tty [N].

    kill -9 [pid] (CR) : Means a ``sure kill'' of ``pid'' [pid]; this becomes necessary when you lose control of a process or have a session aborted. CAUTION: Aborted sessions are not uncommon so it is helpful to develop skills of a super process (program) killer.

Unix Env related command


    setenv TERM vt100 (CR) : Sets `TERM' variable to type `vt100', which should be the default and can be checked by using `printenv', else use `h19b' or your correct terminal emulation if recognizable by the remote host. The recognizable terminal type are in the alphabetized subdirectories of `/usr/lib/terminfo', e.g., `v' directory contains vt100 listings. Caution: `YTERM' is ideal for PC to CMS communication, but does not have a terminal type that is recognizable by UNIX systems ('vt100' may sometimes work as a substitute, but `unknown' type means a poor line edit session).

    setenv TERMCAP vt100 (CR) : Sets `TERMCAP' variable to type `vt100', else use `h19b' etc. You can put customized terminal configuration in the file `.termcap' and enable it with the command `setenv TERMCAP $HOME.termcap' either in your shell or in your '.login' file.

    tset -m :h19b (CR) : Sets terminal type to Heathkit or Zenith type `h19b'. WARNING: Several terminal commands are given here, because you might have to try several before you find one that works. Note that one of the biggest problems in working with advanced, remote computers is COMMUNICATION and that much of this local guide is intended to solve communication problems.

    stty erase \[key](CR) : Set or reset the terminal (`tty') erase key to `[key]'.

    stty all (CR) : Display usual Control characters; with arguments can be use to set terminal communication variables; also try `stty everything'.

printenv


    printenv (CR) : Print out environment meta parameters such as defaults, terminal types and key assignments, eg., SHELL, PATH, TERM, NCPUS, HOME, TMP, AFS, and AFSTMP.

Tuesday, June 10, 2014

Unix Step by Step Learning

http://www.tutorialspoint.com/unix/index.htm

UNIX - Touch

touch - is a standard Unix program used to change a file's access and modification timestamps. It is also used to create a new empty file.

-a, change the access time only
-c, if the file does not exist, do not create it and do not report this condition
-d date_time, use the date_time specified to update the access and modification times
-m, change the modification time only
-r file, use the access and modification times of file

-t time, use the time specified (in the format below) to update the access and modification times

Unix - Special Variables

Variable Description

$0 The filename of the current script.
$n These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).

$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.

$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.


$? The exit status of the last command executed.
$$ The process number of the current shell. For shell scripts, this is the process ID under which they are executing.


$! The process number of the last background command.

Unix - To lower/upper case

To lower/upper case

If you want to transform a string to upper or lower case, you can do so with the unix tr command. Here's a simple example.
#!/bin/sh

STR_ORIGINAL=aBcDeFgHiJkLmNoP
STR_UPPER=`echo $STR_ORIGINAL | tr a-z A-Z`
STR_LOWER=`echo $STR_ORIGINAL | tr A-Z a-z`

echo "Original: $STR_ORIGINAL"
echo "Upper   : $STR_UPPER"
echo "Lower   : $STR_LOWER"

Unix - Find in a string

Find in a string

Sometimes you need to find text in a string. Maybe you want to list files but print only the text appearing before the ".". So if the filename is asdf.txt, you would want to print only asdf. To do this, you will use expr index, and pass it the string followed by the text for which you are searching. Let's try an example:
#!/bin/sh

# Get the files:
FILES=`ls -1`

for FILE in $FILES
do
IDX=`expr index $FILE .`

if [ "$IDX" == 0 ]; then
IDX=`expr length $FILE`
else
IDX=`expr $IDX - 1`
fi

SUB=`expr substr $FILE 1 $IDX`
echo "Sub File: $SUB"
done

Advanced Unix shell scripting

Substrings

Often times a programmer needs to be able to get a substring from a variable at a given position. In unix you can use the expr command to do this with the substr parameter. 

Let's say that we have the text string "5283username$$2384/" and we want to get the text "username". To do this we need to read from position 5 for a length of 8. The parameters for substr are the input string, the starting position, and the length.

See the following example: 

INPUT="5283username$$2384/"

USER=`expr substr $INPUT 5 8`

echo "Sub: '$USER'"

Unix ---> More tricks

Every word of a file can be placed on a separate line by typing

    cat old_filename | tr -cs A-Za-z '\012' > new_filename
The following lists all words in filename in alphabetical order.

    cat filename | tr -cs A-Za-z '\012' | tr A-Z a-z | sort | uniq
You can find out when the file .rhosts was last modified by typing

    echo .rhosts last modified on `/bin/ls -l .rhosts | cut -c33-44`
Typing head -n displays the first n lines of a file. And typing last lists the last logins.

    grep '^...............$' /usr/share/dict/words  | grep -v '\(.\).*\1'
returns all the words in /usr/share/dict/words that are fifteen letters long and do not contain the same letter twice.

    grep 'a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u' /usr/share/dict/words
returns all the words in /usr/share/dict/words that contain all of the vowels in the correct order.

Unix ---> Removing files with strange names

There may come a time that you will discover that you have somehow created a file with a strange name that cannot be removed through conventional means. This section contains some unconventional approaches that may aid in removing such files.
Files that begin with a dash can be removed by typing

    rm ./-filename
A couple other ways that may work are

    rm -- -filename
and

    rm - -filename
Now let's suppose that we an even nastier filename. One that I ran across this summer was a file with no filename. The solution I used to remove it was to type

    rm -i *
This executes the rm command in interactive mode. I then answered "yes" to the query to remove the nameless file and "no" to all the other queries about the rest of the files.
Another method I could have used would be to obtain the inode number of the nameless file with

    ls -i
and then type

    find . -inum number -ok rm '{}' \;
where number is the inode number.
The -ok flag causes a confirmation prompt to be displayed. If you would rather live on the edge and not be bothered with the prompting, you can use -exec in place of -ok.
Suppose you didn't want to remove the file with the funny name, but wanted to rename it so that you could access it more readily. This can be accomplished by following the previous procedure with the following modification to the find command:

    find . -inum number -ok mv '{}' new_filename \;

Unix ---> Wildcards

A number of characters are interpreted by the Unix shell before any other action takes place. These characters are known as wildcard characters. Usually these characters are used in place of filenames or directory names.

    *     An asterisk matches any number of characters
          in a filename, including none.
    ?     The question mark matches any single
          character.
    [ ]   Brackets enclose a set of characters, any
          one of which may match a single character
          at that position.
    -     A hyphen used within [ ] denotes a range of
          characters.
    ~     A tilde at the beginning of a word expands
          to the name of your home directory.  If you
          append another user's login name to the
          character, it refers to that user's home
          directory.

Here are some examples:
  1. cat c* displays any file whose name begins with c including the file c, if it exists.
  2. ls *.c lists all files that have a .c extension.
  3. cp ../rmt?. copies every file in the parent directory that is four characters long and begins with rmt to the working directory. (The names will remain the same.)
  4. ls rmt[34567] lists every file that begins with rmt and has a 3456, or 7 at the end.
  5. ls rmt[3-7] does exactly the same thing as the previous example.
  6. ls ~ lists your home directory.
  7. ls ~hessen lists the home directory of the guy1 with the user id hessen.

Unix ---> Miscellaneous

This page is a terse description of a few more Unix commands that you may find occasion to use.
awk
- A pattern scanning and processing language.
bar
- Creates a tape archive. (Similar to tar)
bg
- Moves a job into the background.
cal
- Displays a calendar.
cc
- Compiles C code.
chfn
- Changes finger information.
clear
- Clears your terminal's screen.
cmp
- Performs a byte-by-byte comparison of two files.
cut
- Removes selected fields from each line of a file.
date
- Displays or sets the date.
ed
- The most basic line editor.
ex
- A simple line editor. Also know as e or edit.
fg
- Moves a job into the foreground.
file
- Determines the type of a file by examining its contents.
fmt
- Formats text.
hostname
- Sets or prints the name of the current host computer.
jobs
- Lists the current jobs in the shell.
make
- Maintains, updates, and regenerates related programs and files.
mesg
- Permits or denies messages on your terminal.
mt
- Provides magnetic tape control.
od
- Dumps octal, decimal, hexadecimal, or ascii representations of files.
pack/unpack
- Similar to compress/uncompress, but uses Huffman coding.
paste
- Joins corresponding lines of several files.
rev
- Reverses the order of characters in each line.
rcp
- Copies a file from a remote computer to the local computer and vice versa.
rsh
- Execute a remote shell command.
sed
- A stream editor-quite powerful.
sort
- Sorts and collates lines.
split
- Splits a file into pieces.
stty
- Sets or alters the options for a terminal.
tr
- Translates characters.
uname
- Displays the name of the current system.
units
- Converts a number into different units.
uuencode/uudecode
- Encodes/decodes a binary file into strictly ascii characters. (Useful for transmission via electronic mail)
write
- Write a message to another user.
xget/xsend
- Commands for sending/receiving secret mail.

Unix ---> grep

The grep (global regular expression print) program searches for an expression in a file or group of files. There are three versions: grepegrep (extended grep), and fgrep (fixed-string grep). The grep program expands wildcard characters in the given expression. The egrepprogram searches for the expression including alternations. The fgrep program searches for fixed-strings only and does not expand wildcard characters. The egrep program has more sophisticated internal algorithms, and is usually faster than grep or fgrep. The syntax for all three versions is:

    command [options] expression [file] ...

I have found these Unix commands to be very useful when programming. Suppose I had a C program with a number of subroutines and a global variable labeled chuck_wivell. Suppose further that Chuck found out about this and didn't like it. Of course I would change it immediately.

egrep chuck_wivell *.c

would give me a list of all source files where the offensive variable manifested itself. By placing a -n option in the command line I could also obtain the line numbers of the offenses.

The wildcard characters that grep handles are

\ [ ] . ^ * $

and a delimiter used to mark the beginning and end of an expression. Delimiters are necessary only if the expression contains blanks or wildcard characters. Here are a few examples to help solidify this potential mumbo-jumbo:

    grep 'Nostalgia is not what it used to be' fft.c

searches through the file fft.c for the expression Nostalgia is not what it used to be.
The wildcard character "." matches any character. Therefore,

    grep 'eur.' fft.c

would find eurekaamateurchauffeur, etc... in the file fft.c.
Characters placed inside square brackets are each compared when searching.

    grep '[cm]an' fft.c

would find any words with the sequence can or man, but would not locate sequences like ran or and.
Preceding a wildcard character by a "\" turns off the wildcard character feature and the character is treated normally, i.e., the expression eddie\. would yield all occurrences of eddie. but would not yield eddies or eddieboy.
Here are some useful options for all three of the greps:

    -f    Matches all the expressions in a given
          file as opposed to the one typed in the
          command line.
    -i    Removes case sensitivity so that uppercase
          and lowercase letters match.
    -n    Displays the line numbers containing a
          match.
    -l    Displays the names of the files that
          contain a match but not the lines that
          contained a match.
    -v    Displays the lines that don't match as
          opposed to those that do.


Unix ---> find

The find command recursively descends through the directory tree looking for files that match a logical expression. The find command has many options and is very powerful. Rather than go into detail here, I encourage you to take a look at the man pages for find. The findcommand does have a rather contorted syntax which is not easily mastered, and if truth be written, that's why I'm not spending more paper on it here.

However, here is a quick example that should get you started:

find ./ -name elm -print

will print all occurrences of a file named "elm" in the working directory and all of its subdirectories.

Unix ---> chmod

Your files and directories have a number of attributes that are set when they are created. Listing the files with the -la flag, i.e., ls -la, displays the attributes of each file and directory in the working directory. Here is an example listing:

    total 3
    drwxr-xr-x  2 taylor        512 Aug  2 08:41 .
    drwxrwxr-x 12 taylor       1024 Aug  2 08:41 ..
    -rw-r--r--  1 taylor          5 Aug  2 08:41 blue
    -rw-r--r--  1 taylor         12 Aug  2 08:41 green
    -rw-r--r--  1 taylor          7 Aug  2 08:41 yellow

To the far left of each file or directory name are ten characters which show the attributes. The first column indicates whether the entry is a directory (d) or not (-). The other nine characters are organized into three groups of the three. The first group pertains to the owner (that would be you for your files). The second group pertains to people in your group. The third group pertains to everyone else. Within each group of three are three characters. The first indicates read (r) permission. The second indicates write (w) permission. The third indicates execute (x) permission. If the permission is not present, a "-" will replace the rw, or x.

The chmod (change mode) command lets you change the attributes on a file or directory. There are a number of forms, but I have chosen to cover the following syntax because of its similarity with umask. The syntax is as follows:

    chmod mode filename

where mode is a three digit octal number. The first digit pertains to the owners privileges. The second pertains to the groups privileges, and the third pertains to everyone elses privileges. Each octal digit is composed of the addition of three components. The read component is worth 4, the write component worth 2, and the execute component worth 1. Suppose I wanted the owner to have read, write, and execute privileges, the group to have read and write privileges, and everyone else to have read privileges only. The octal number I would use withchmod would be 764.

Unix ---> alias

The alias command allows you to define shortcuts to save yourself time. In a sense, alias creates a link between a requested set of keystrokes and another set of keystrokes. For example, to use the rm command in interactive mode I would type

    rm -i

By typing

    alias rm 'rm -i'

the alias command would allow me to avoid typing the interactive flag, -i, every time a called the rm command.

Unix ---> Copying

The cp (copy) command lets you duplicate a file of choice. Here is an explanation by examples:
cp cocoon butterfly

makes a duplicate of the file cocoon and gives it the name butterfly. Note that the filenames can include pathnames as well.
cp /home/cernan/taylor/tex/contract ../contract.bak

makes a copy of the file contract found in the /home/cernan/taylor/tex directory and places it one directory level above the working directory in a file called contract.bak.

If /home/cernan/taylor/tex is a directory, then
cp report /home/cernan/taylor/tex

will place a copy of report in the /home/cernan/taylor/tex directory with the name report.
cp /home/cernan/taylor/tex/headlines .
will copy the file headlines in the /home/cernan/taylor/tex directory into the working directory. The name will remain unchanged.
cp /home/cernan/taylor/tex/* .
will copy all the files (but not the subdirectories) in /home/cernan/taylor/tex into the working directory. You can copy all the subdirectories in the directory and files contained in them by using the -r (recursive) flag as follows:
cp -r /home/cernan/taylor/tex/* .

Another useful flag is the -i (interactive) flag which prompts you if you are about to overwrite an existing file.

Vi ---> Repeat

Often times you may desire to repeat the last command performed. This can be done with the "." command. Place the cursor in the appropriate position and press "." to repeat the most recent command. Suppose I had a C program in which I wished to switch a variable name fromno_way to yes_way in two different places. One way I could accomplish this would be to place my cursor on the beginning of the first no_way and type cw (change word) and then type yes_way <esc>. This would accomplish my task for the first case. Now all I would need to do to change the second no_way would be to place my cursor at the beginning of it and type "." to repeat the last command.

Vi ---> Undo

The undo command, u, is another feature that has saved me many times. Pressing u undoes the last command you told vi to perform. Another form of the undo command is U which undoes all the changes made to the current line since you moved there.

Vi ---> Saving and Quitting

     :w    write to disk
     ZZ    write to disk and exit
     :q!   exit without writing to disk

Actually, the command for quitting vi is :q. You can save and quit by typing :wq but ZZ does the same thing1 and takes one less keystroke. If there are unsaved changes to the text and you try to quit using :qvi will warn you that you have unsaved changes and will prevent you from quitting. In order to quit without saving the changes you must use the override switch, !.

Vi ---> Deleting text

x     delete the character under the cursor
dd    delete a line

Vi ---> Moving around

     h         move the cursor one character to
               the left
     j         move the cursor one character down
     k         move the cursor one character up
     l         move the cursor one character to
               the right
     0         move to the beginning of a line
     $         move to the end of a line
     G         move to the end of a file
     1G        move to the first line of a file
     <ctrl-F>  move down one screen
     <ctrl-B>  move up one screen

UNIX Vi ---> Search and Replace

The search command is /. To search for polite type /politen repeats the search in the same direction, and N repeats the search in the opposite direction.
The search option accepts most of the standard Unix pattern matching language.

Here are a few examples (using this text) that you will probably never use but may find inspiring:

    /[a-z]as
will search for any lowercase letter followed by as. In this example, it would find was and last but not as or asked.

    /[^c]an
will search for any an preceded by any character other than a c. In our text it would find Milan but not scan or can.

    /^[A-Z].*\. *$
will search for any line that begins with a capital letter and ends with a period and any number of blanks. Our only match in the example text would be with the last line.
All of these search patterns can be used in the search and replace command that takes on the following structure:

    :s/search_string/replacement_string/g
This command replaces every search_string on the current line with replacement_string. Omitting the g (global) flag at the end of the command will cause only the first occurrence of search_string to be altered. Often you may wish to confirm each replacement. This can be done with the confirm flag c. The confirm flag should be placed after or in place of the g flag. Suppose I had the following line:
Give a skeptic and inch... and he'll take a mile.
and typed

    :s/take a mile/measure it/
I would be left with
Give a skeptic and inch... and he'll measure it.

Any command that begins with a ":" is called a line mode command and performs its duty on the line the cursor is currently on. However, you can override vi's default of operating only on the current line by preceding them with a range of line numbers. For example, if I wanted to replace guy with gal on lines 32 through 56 I would type

    :32,56s/guy/gal/g
Omitting the g would cause only the first occurrence of guy in each line to be replaced. The "." and "$" play a special role in this sort of designation. "." indicates the current line, and "$" indicates the last line of the file. Therefore, if I wanted to delete1 from the current line to the end of the file I would enter:2

    :.,$d
I could even do something like:

    :.,/Edison/d
which would delete from the current line to the next line that contained Edison.
One other shortcut that might be worth mentioning is that 1,$ and % both indicate all the lines in the file. Therefore,

    :1,$s/search_string/replacement_string/g
and

    :%s/search_string/replacement_string/g
do exactly the same thing.