UNIX Tip of the Day UNIX Tip of the Day ¥ Directory maneuvering commands % dirs pushd, popd, and, dirs /cis/staff/rvrpci /usr/tmp Shell Programming % cd /usr/tmp % pushd % pwd % pwd /usr/tmp /usr/tmp % pushd ~rvrpci % dirs % pwd /usr/tmp /cis/staff/rvrpci /cis/staff/rvrpci % pushd /usr/local/bin
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
UNIX Tip of the Day UNIX Tip of the Day UNIX Tip of the Day
% dirs % dirs % dirs /usr/local/bin /usr/tmp /usr/tmp /usr/local/bin /cis/staff/rvrpci /usr/tmp /cis/staff/rvrpci /cis/staff/rvrpci /usr/local/bin % pwd % pwd % popd /usr/local/bin /usr/tmp % dirs % pushd % pushd +2 /usr/tmp /usr/local/bin % dirs % pwd /usr/tmp /usr/local/bin /cis/staff/rvrpci /cis/staff/rvrpci
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 What is shell programming? .login file .cshrc file
¥ Shell programming set path=($HOME/bin /usr/local/bin \ if ($?prompt) then /usr/ucb /usr/sbin /bin /usr/bin \ set notify Ðautomate a set of UNIX commands. /usr/bin/X11 .) set history = 100 ÐJust like any programming language stty dec new set savehist = 100 –“wrappers” tset -I -Q alias pd pushd ¥ black box a customized collection of UNIX set mail=/usr/spool/mail/$USER alias pop popd commands. set editmode = emacs alias vt100 "set term = vt100" ÐExample of shell programs umask 077 endif .login biff n .cshrc date
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
When these files are Other useful .login/.cshrc executed? User defined shell program .cshrc entries Ðis automatically executed when you start a set filec ¥ Determine name of command new shell set cdpath=(~ ~rvrpci/pub ~/mythesis) ¥ Determine input, output, and option .login arguments Ðonly gets executed once when you first Other common entries login ¥ Determine UNIX commands to execute ¥ Establish error trapping set path=( $path /usr/local/bin) Can be re-executed by giving the source set path=(/usr/local/bin $path) ¥ Make shell program executable command % source .cshrc
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 A simple shell program We would rather see... Special Shell Variables Set
¥ dd command to swap bytes % swap_bytes input.dat output.dat % swap_bytes input.dat output.dat % dd if=input.dat of=output.dat $0 $1 $2 bs=2 conv=swab command $argv[1] $argv[2] ¥ Very difficult to remember ¥ Very little utility to non-UNIX geeks (normal people)
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
Another Special shell program Making swap_bytes Shell Variables swap_bytes shell script executable % swap_bytes input.dat output.dat #!/bin/csh -f % ls -l swap_bytes dd if=$1 of=$2 bs=2 conv=swab -rw------... swap_bytes $#argv % chmod u+x swap_bytes % ls -l swap_bytes Indicates how many arguments are present -rwx------... swap_bytes In this case, 2
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 To run swap_bytes Limitation of swap_bytes Improvement to swap_bytes
¥ swap_bytes becomes just another ¥ No error trapping #!/bin/csh -f unix command! ¥ Should give usage when typing command if ( $#argv != 2 ) then echo "usage: $0 input_file output_file" exit 1 % swap_bytes endif % swap_bytes input.dat output.dat usage: swap_bytes input_file output_file dd if=$1 of=$2 bs=2 conv=swab
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
Informational message from Commad exit status Interactive swap_bytes swap_bytes ¥ By convention ¥ UNIX style informational message ¥ If you want a “friendlier” shell program ÐHave it query the user for the inputs exit 0 % swap_bytes Indicates successful command completion usage: swap_bytes input_file output_file ¥ Another special shell variable can be used exit 1 (or non-zero) Indicates some error condition $<
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Interactive swap_bytes Interactive swap_bytes example #!/bin/csh -f ¥ User simply types the command if ( $#argv != 2 ) then echo -n "Please enter the input file> " UNIX Quotes % swap_bytes set input=$< Please enter the input file> input.dat echo -n "Please enter the output file> " Please enter the output file> output.dat set output=$< endif dd if=$input of=$output bs=2 conv=swab
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
A more complex shell program A note about quotes in UNIX A note about shell variables ¥ In pbmplus utilities, % set a=ls ¥ Shell variables can also double up as rawtopgm conversion exists % echo a arrays % echo $a pgmtoraw conversion does not ¥ Using the previous example, % set b=“$a” ¥ A version of pgmtoraw in a % echo $b % echo $b programming language like C % set b=‘$a’ % echo $b[1] ÐTime consuming % echo $b % set b=`$a` % echo $#b ÐProgram will likely be used infrequently % echo $b % echo $b[$#b] ¥ Solution: shell program
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Define input and output files Dimensions of input image pgmtoraw shell script design pgmtoraw ( pnmfile) ¥ Define input and output files #!/bin/csh -f % more test_data.pgm P2 ¥ Figure out dimensions of input image set command_name=$0 set number_args=$#argv 3 3 ¥ Determine number of bytes for input image if( $number_args != 1 ) then 255 ¥ Determine number of bytes for header echo “usage:$command_name input_file_name” 1 2 3 4 5 6 ¥ Need to strip out the header bytes exit 1 endif 7 8 9 ¥ Write out headerless image data . . % pnmfile test_data.pgm . test_data.pgm: PGM plain, 3 by 3 maxval 255
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
pgmtoraw (continued) pgmtoraw (continued) Resulting pgmtoraw utility
set input_file=$1 set file_info=`wc -c $input_file` ¥ Uses existing routines to obtain information set bytes_in_file = $file_info[1] set pnm_info = `pnmfile $input_file` pnmfile set image_type = $pnm_info[2] @ header = $bytes_in_file - $image_bytes wc set data_type = $pnm_info[3] dd if=$input_file bs=$pixel_bytes skip=$header set width = $pnm_info[4] dd set height = $pnm_info[6] ¥ Functional tool written in 20 command lines set maxval = $pnm_info[8] set pixel_bytes = 1 @ image_bytes = $width * $height
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Current Limitations of Shell Scripts Wrappers pnmtojpeg.pro Resulting pgmtoraw utility and IDL ¥ No check between “ASCII” vs. “RAW” pgm ¥ Another utility missing in pbmplus pro pnmtojpeg, input_file, output_file if( data_type == ‘plain,’) ... jpegtopnm or pnmtojpeg ¥ No provisions for multibyte per pixel case read_ppm, input_file, image ÐUse pnmfile results to check for above cases ¥ IDL has jpeg read/write capability write_jpeg, output_file, image Ðendian case needs to be addressed for –Create a “wrapper” that makes an idl multibyte case program pbmplus-like ¥ Above conditions can be addressed by suite end of UNIX commands
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
Usage of pnmtojpeg.pro in IDL Usage of pnmtojpeg.pro in IDL IDL_PATH environment variable IDL> pnmtojpeg,‘image.pnm’,’image.jpg’ IDL> pnmtojpeg,‘image.pnm’,’image.jpg’ setenv IDL_DIR /cis/common/rsi/idl_5 setenv IDL_PATH ¥ For IDL to automatically find pnmtojpeg.pro \+$IDL_DIR/lib:\+$IDL_DIR/examples: \+/dirs/common/rsi/idl_5:\+/dirs/common/lib/idl :\+~/lib/idl ÐIt must be in the current working directory
ÐDirectory containing pnmtojpeg.pro must be defined in the ENVIRONMENT VARIABLE ¥ IDL_PATH
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Usage of pnmtojpeg.csh pnmtojpeg.csh Limitation of pnmtojpeg.csh % pnmtojpeg.csh image.pnm image.jpg #!/bin/csh -f ¥ Does not conform to pbmplus piping, echo pnmtojpeg “,” “’”$1”’” “,” “’”$2”’” | idl i.e., % tifftopnm file.tif | pnmscale 2.0 > new_file.pnm
¥ No error trapping
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
Usage cases of pnmtojpeg Usage cases of pnmtojpeg Usage cases of pnmtojpeg (no command line arguments) (1 command line argument) (2 command line argument)
% tifftopnm file.tif | pnmscale 2.0 | pnmtojpeg > new_file.jpg % pnmtojpeg image.pnm > image.jpg % pnmtojpeg image.pnm image.jpg
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Yet another wrapper Code for no argument case Code for 1 argument case pnmtojpeg #!/bin/csh -f if($#argv == 0) then else if($#argv ==1) then set input_file = /usr/tmp/$0_input_$$ set input_file = $1 # If user interrupts process, jump to stop set output_file = /usr/tmp/$0_output_$$ set output_file = /usr/tmp/$0_output_$$ onintr stop cat > $input_file pnmtojpeg.csh $input_file $output_file # $0 is the command name pnmtojpeg.csh $input_file $output_file cat $output_file # $#argv is the number of arguments cat $output_file . # $$ is the process id . . . . .
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000
Code for 2 argument case pnmtojpeg summary Summary else ¥ Produced a “new” pbmplus utility ¥ The “dot” files set input_file = $1 set output_file = $2 ¥ Used UNIX shell scripting ¥ Basics of Shell Scripting pnmtojpeg.csh $input_file $output_file ÐArgument handling ¥ Special Shell Variables Endif ÐScratch space /usr/tmp ¥ Seamless integration of UNIX to other ÐProcess id handling #clean up when finished utilities (IDL) stop: ÐClean up rm -f /usr/tmp/$0_input_$$ ¥ Integrated IDL program and commands rm -f /usr/tmp/$0_output_$$ ¥ 21 lines of UNIX commands
Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Rolando V. Raqueño Saturday, January 8, 2000 Other Shell Constructs to keep in mind ¥ foreach ¥ while ¥ case
Rolando V. Raqueño Saturday, January 8, 2000