->VAR=$(( 1+3*2 ))
->echo $VAR
7
->VAR=$(( (1+3)*2 ))
->echo $VAR
8
Wednesday, March 30, 2011
Filename Substitution with Wildcards
Wildcards
* 0 or more characters
? 1 character
* 0 or more characters
? 1 character
Command typed | Meaning |
---|---|
ls * | display all files |
ls *.sh | display 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
Setting Scalar Values
Setting Array Values
Set Variables to Read-only
Unset Variables (if not readonly)
Variable Types
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
- a-z, 0-9 or underscore
- conventionally uppercase
- start with letter
PET=fish
PET='fish bowl'
PET="fish bowl"
->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 time | cmd1 ; cmd2 |
spell check a file | cat filename | spell |
Reset terminal: if mistakenly viewed a binary file with cat | stty sane |
append results of a command onto an existing file | cmd>>file.ext |
write results of a command to a file | cmd>file.ext |
logout | [Ctrl]+D |
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 contents | more file.ext |
view last 10 lines of a file | tail filename |
view multiple files | head filenam* | more |
view top # lines of a file | head -# filename |
view top 10 lines of a file | head filename |
Sort
sort file | sort filein > fileout |
sort file and remove duplicates | sort filein > fileout | uniq |
sort file and view duplicates | sort filein > fileout | uniq -d |
sort file numerically | sort -n filein > fileout |
sort multiple files together | sort filein1 filein2 > fileout |
System Information
list current shell | echo $SHELL |
show your group | grep yourid /etc/passwd |
show all groups | more /etc/group |
system information | uname -a |
who's logged in | finger |
Help: Man Pages
help : output to a file in home directory for further annotation | man cmd | col -b > ~/file.ext |
help : viewing a specific page # | man -s # cmd |
help (on-line) | man cmd |
help with index | man -k cmd |
Find Files
find files from current directory down | find . -name filename |
find files from home directory using wildcards | find ~ -name filenam* |
Directory Listing
show subdirectory | pwd |
list files and directories | ls |
list files and directories (one per line) | ls -l |
list files beginning with 'Adams' | ls adams* |
list files by screenful | ls | more |
Counts
count files in a directory | ls | wc -l filename |
count lines files | wc -l filename |
count lines words and bytes | wc filename |
count words files | wc -w filename |
write a file count to a file | ls | wc -l > file.ext |
Copy / Move
concatenate files together in new file | cat oldfile1.ext oldfile2.ext > bothfiles.ext |
copy a file and prompt before overwriting | cp -i existingfile newfile |
copy top # lines of a file to a new file | head -# filename > newfilename |
move a file and prompt before overwriting | mv -i existingfile newfile |
Compare Files and Directories
compare two directories | diff /directory1 /directory2 |
compare two files | cmp file1 file2 |
compare two files and show specific differences-i=ignore case-b=ignore blanks-w=ignore spaces and tabs | diff -iwb file1 file2 |
compare two files side-by-side| lines are the same< line is in first file only> line is in second file only | sdiff file1 file2 |
compare two files side-by-side showing only differences | sdiff -s file1 file2 |
Change / Make Directory
make a directory | mkdir subdir |
change to a subdirectory beneath where you are | cd subdir |
change to a subdirectory of the root | cd /subdir |
change to a subdirectory under your home directory | cd ~/subdir |
change to home directory | cd |
change up one level | cd .. |
Permissions
add write access to current permissions for a file | chmod g+w filename |
remove all access for this directory and subdirectories | chmod -r go-rwx * |
remove write access from everyone but you | chmod go-w filename |
replace current permissions for a file with those specifiedu=user, g=group, o=othersr=read, w=write, x=execute | chmod u=rwx,g=rwx,o=rwx filename |
change group associated with files | chgrp newgroup filename |
add all permissions for everyone | chmod 777 filename |
change ownership of files | chown 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
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 "# +-----------------------------------------------------------------------"
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
Grep
match any character | . | grep l..n filename | finds 'lynn' or 'loan' |
match only if at the beginning of a line | ^ | grep ^day filename | finds 'day into night'but not 'Some day' |
match only if at the end of the line | $ | grep day$ filename | finds 'One fine day'but not 'Monday is good' |
search for '.' or '^' or '$' in a file | \ | grep \* filename | finds '*****1.05' |
search for a set of names | [] | grep [dog,dil]bert filename | finds 'dogbert' and 'dilbert' |
find all lines that start with an alphabetic character in upper or lower case | ^[] | grep ^[A-Z,a-z] filename |
Subscribe to:
Posts (Atom)