Wednesday, March 30, 2011

Arithmetic Substitution

->VAR=$(( 1+3*2 ))
->echo $VAR
7

->VAR=$(( (1+3)*2 ))
->echo $VAR
8

Filename Substitution with Wildcards

Wildcards
*  0 or more characters
?  1 character
 
Command typed Meaning
ls *display all files
ls *.shdisplay all shell scripts
ls go*display all files starting with the letters 'go' and any possible combination of other characters afterwards
ls go??display all files starting with 'go' and two unknown characters
ls go[abcd]display all files starting with 'go' and one more character of either 'a', 'b', 'c', or 'd'
ls go[a-d]display all files starting with 'go' and one more character in the 'a' to 'd' range
ls go[a-dA-D]display all files starting with 'go' and one more character in the 'a' to 'd' range or the 'A' to 'D' range
ls go[!a-d]display all files starting with 'go' and one more character not in the 'a' to 'd' range
ls [a-d]*display all files starting with 'a', 'b', 'c', and 'd' and any other character -- or no other characters

All About Variables

Format
VARIABLE_NAME = value
echo $VARIABLE_NAME  
Types
  • scalar variable -- holds 1 value at a time (aka name value pairs)
  • array variable -- holds 0 or more values
Variable Names
  • a-z, 0-9 or underscore
  • conventionally uppercase
  • start with letter
Setting Values

PET=fish
PET='fish bowl'
PET="fish bowl"

Setting Scalar Values

->PET= cat #A space will cause an error
ksh: cat:  not found

->PET=cat
->echo $PET
cat

->pet='cat fish'
->echo $pet
cat fish

->PET=ferret
->echo $pet   # $pet is not the same as $PET
cat fish  
->echo $PET
ferret

Setting Array Values

->PET[1]=FERRET
->PET[2]=parrot
->echo ${PET[1]}
FERRET
->echo ${PET[2]}
parrot
->echo ${PET[0]}# set since name is same as scalar variable
ferret

Set Variables to Read-only

->readonly PET # now PET can't be set to a value
->PET=dog
ksh: PET: is read only

Unset Variables (if not readonly)

->TREE=oak
->echo $TREE
oak
->unset TREE
->echo $TREE

Variable Types
  local
available within the current instance. not available to programs started by the shell
  environment
variable available to child processes Use export to create an environmental variable from a local variable                         export VARIABLE=value
  shell
special variables set by the shell

Miscellaneous Unix Commands

type two commands at a timecmd1 ; cmd2
spell check a filecat filename | spell
Reset terminal: if mistakenly viewed a binary file with catstty sane
append results of a command onto an existing filecmd>>file.ext
write results of a command to a filecmd>file.ext
logout[Ctrl]+D or exit
ftp file to host1 from host2 ftp host1
search files for a string
-# = display '#' lines on both sides
-c = display a count
-v = display non-matching lines
-i = ignore case
grep string filename

View File Content

view file contentsmore file.ext
view last 10 lines of a filetail filename
view multiple fileshead filenam* | more
view top # lines of a filehead -# filename
view top 10 lines of a filehead filename

Sort

sort filesort filein > fileout
sort file and remove duplicatessort filein > fileout | uniq
sort file and view duplicatessort filein > fileout | uniq -d
sort file numericallysort -n filein > fileout
sort multiple files togethersort filein1 filein2 > fileout

System Information

list current shellecho $SHELL
show your groupgrep yourid /etc/passwd
show all groupsmore /etc/group
system informationuname -a
who's logged infinger

Help: Man Pages

help : output to a file in home directory for further annotationman cmd | col -b > ~/file.ext
help : viewing a specific page #man -s # cmd
help (on-line)man cmd
help with indexman -k cmd

Find Files

find files from current directory downfind . -name filename
find files from home directory using wildcardsfind ~ -name filenam*

Directory Listing

show subdirectorypwd
list files and directoriesls
list files and directories (one per line)ls -l
list files beginning with 'Adams'ls adams*
list files by screenfulls | more

Delete Files

delete files with promptingrm existingfile

Counts

count files in a directoryls | wc -l filename
count lines fileswc -l filename
count lines words and byteswc filename
count words fileswc -w filename
write a file count to a filels | wc -l > file.ext

Copy / Move

concatenate files together in new filecat oldfile1.ext oldfile2.ext > bothfiles.ext
copy a file and prompt before overwritingcp -i existingfile newfile
copy top # lines of a file to a new filehead -# filename > newfilename
move a file and prompt before overwritingmv -i existingfile newfile

Compare Files and Directories

compare two directoriesdiff /directory1 /directory2
compare two filescmp file1 file2
compare two files and show specific differences-i=ignore case-b=ignore blanks-w=ignore spaces and tabsdiff -iwb file1 file2
compare two files side-by-side| lines are the same< line is in first file only> line is in second file onlysdiff file1 file2
compare two files side-by-side showing only differencessdiff -s file1 file2

Change / Make Directory

make a directorymkdir subdir
change to a subdirectory beneath where you arecd subdir
change to a subdirectory of the rootcd /subdir
change to a subdirectory under your home directorycd ~/subdir
change to home directorycd
change up one levelcd ..

Permissions

add write access to current permissions for a filechmod g+w filename
remove all access for this directory and subdirectorieschmod -r go-rwx *
remove write access from everyone but youchmod go-w filename
replace current permissions for a file with those specifiedu=user, g=group, o=othersr=read, w=write, x=executechmod u=rwx,g=rwx,o=rwx filename
change group associated with fileschgrp newgroup filename
add all permissions for everyonechmod 777 filename
change ownership of fileschown newowner filename

Just Some Miscellaneous Notes

Man / More / Less

[Spacebar]  view next screen
[Enter]      view a line at a time
b            view previous screen
q                    quit viewing

Naming Files


  • The system will allow a file to be created with the same name as a directory.




  • The system will allow a space in a name, but some programs have trouble with it.




  • Use periods or underscores to separate characters.
  • Tuesday, March 22, 2011

    Documenting a Unix Script

    #! /usr/bin/ksh
    echo "# +-----------------------------------------------------------------------"
    echo "# | Program ID:    scriptname.sh"
    echo "# |"
    echo "# | Purpose:       description"
    echo "# |"
    echo "# | Developer:     Lynn Tobias"
    echo "# | Program Date:  mm/dd/yyyy"
    echo "# |"
    echo "# | Input File(s): /ux123/scripts/YES              -- 'Y' for prompts"
    echo "# |                /ux123/scripts/ods_uat.password -- sql+ password"
    echo "# |"
    echo "# | Table(s) Used: EXT_BLDLIST"
    echo "# |"
    echo "# | Called by:     ?"
    echo "# | Calls:         scriptname.pl"
    echo "# |"
    echo "# | Output:        /ux123/scripts/blddist.file10"
    echo "# |                "
    echo "# | Revisions:     Dvlpr Date     Version Comment/Change"
    echo "# |                ----- -------- ------- --------------------------------"
    echo "# |"
    echo "# +-----------------------------------------------------------------------"

    SQLPlus in a Here Document

    This is called a 'here document' - because it says "here is the text for your input". The redirection expressed as "<<EOF" says "take what follows as though I typed it at the keyboard, until you find a token to match EOF as the first token on a line". The token EOF has no special meaning - it's just a string of characters used as a marker to tell the 'here document' when the text input is completed.

    sqlplus <<EOF
          Select something
           from somewhere;
    EOF

    Important: the ending token must appear as the first characters on a line by itself.

    Thanks to Jim Benz of Pittsburgh for these notes.


    Monday, March 21, 2011

    Password Variables

    #! /usr/bin/ksh
    
    echo "#------------------------------------------------------------------------------"
    echo "# get passwords for the ftp and for sqlplus and assign to variables."
    echo "#------------------------------------------------------------------------------"
    
    export FTP_PASS='cat /ux123/dfact/xlt/scripts/ust_grp.password'
    
    export SQL_PASS='cat /ux123/dfact/xlt/scripts/xlt_ods_uat.password'
    
    echo "#------------------------------------------------------------------------------"
    echo "# start ftp from vax --i: turn off prompting    n: turn off login"
    echo "#------------------------------------------------------------------------------"
    
    ftp -in host1 <
    quote user ust_grp
    quote pass $FTP_PASS
    ...
    quit
    EOF
    
    echo "#------------------------------------------------------------------------------"
    echo "# In SQL+, ...
    echo "#------------------------------------------------------------------------------"
    
    sqlplus -s  $SQL_PASS << EOF
    .
    .
    exit;
    EOF

    Directory Listing

    Grep

    match any character.grep l..n filenamefinds 'lynn' or 'loan'
    match only if at the beginning of a line^grep ^day filenamefinds 'day into night'but not 'Some day'
    match only if at the end of the line$grep day$ filenamefinds 'One fine day'but not 'Monday is good'
    search for '.' or '^' or '$' in a file\grep \* filenamefinds '*****1.05'
    search for a set of names[]grep [dog,dil]bert filenamefinds 'dogbert' and 'dilbert'
    find all lines that start with an alphabetic character in upper or lower case^[]grep ^[A-Z,a-z] filename