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