#!/bin/csh -f #(ie run the cshell on this but don't read the .cshrc) echo "version = 2.06 of save 2011 Aug 27" # 2011 Aug 27, 2.06: give version (in difi) # 2011 Aug 11, 2.05: show ignoring message more clearly # 2011 Aug 10, 2.04: don't copy unless there is NO .Z # 2011 May 18, 2.03: files with path # 2009 Sep 15, 2.02: handle missing files # 2009 Sep 14, 2.01: actually use dave! # 2009 Aug 27, 2.00: total rewrite to be self contained # echo use save-original for now # exit set misc = "burble" # for testing set misc = "misc" set tras = "trash" set trash = "~/$misc/$tras" # switch the next two lines to see debugging information set debugging = 1 set debugging = 0 if ($#argv == 0) then echo 'usage: save [file]' echo "Save a file/directory into an 'old' directory for backup." echo "The file is unix compressed." echo "(Zip cannot be used since it can't handle files with '.'." echo echo "If there is a 'version = ' string, then use it" echo "to name the backup, otherwise use the date and time." echo "" echo "For example, if the version string is:" echo "version = 9.11; (* of makelogo.p 2000 Mar 17" echo "then 'save makelogo.p' will save in the file" echo "makelogo.p.9.11" echo "The place that the file is saved is one of three locations:" echo "1. A local directory named './old'" echo '2. a location given by an environmental variable $old' echo "3. If these do not exist, a pointer to $trash is used". echo "" echo "Dependency: dave program for the dated save" echo "" exit endif set source = "$1" # Does the source file exist? if !(-e "$source") then echo "$source is not a file or directory" exit endif # Is the source file not an executable? # forget this for now echo Determine target directory set target = "" if (-e "./old") then set target = "./old" else if ("$?old") then if (-d "$old") then echo "using environmental $old" set target = "$old" else echo "directory for environmental variable $old does not exist" exit endif else if (-d $trash) then set target = "$trash" else echo "creating $trash directory" if (-d "~$misc") then if (-d "$tras") then echo "save program error" exit else mkdir ~/$misc/$tras endif else mkdir ~/$misc mkdir ~/$misc/$tras endif endif endif endif echo "target: $target" # get the version number in the source set ver = "`cat "$source"| grep 'version = '|head -1`" if ("$ver" == '') then echo 'no version number, using dated save' echo dave "$source" dave "$source" exit else # parse $ver echo "$ver" set me = `whoami` set tmp = "/tmp/${me}.save" # a directory set tmp2 = "/tmp/${me}2.save" # a file set tmp3 = "/tmp/${me}3.save" # a file grep 'version = ' $1 | head -1 > $tmp2 if ($debugging) then echo '--- $tmp2' cat $tmp2 echo '---' endif cat $tmp2|sed 's/version = //'|tr '' '\n'|head -2|tail -1> $tmp3 if ($debugging) then echo '--- $tmp3' cat $tmp3 echo '---' endif # set version = `cat $tmp3|tr ' ' '\n'|head -1|tr -cd '[:alnum:]'` # set version = `cat $tmp3|tr ' ' '\n'|head -1|tr -c '[:punct:]'` set version = `cat $tmp3|tr ' ' '\n'|head -1|tr -d ';'` # set file = `cat $tmp3|tr ' ' '\n'|head -3|tail -1` #cat $tmp3|sed 's/ of //'|tr '' '\n'|head -2|tail -1|tr ' ' '\n'|head -1 set file = `cat $tmp3|sed 's/ of //'|tr '' '\n'|head -2|tail -1|tr ' ' '\n'|head -1` echo "version: $version" echo "file: $file" if ("$version" == '') then echo "version name is empty\!" exit endif if ("$file" == '') then echo "file name is empty\!" exit endif if !(-f $source) then echo "version name $file inside file $source" echo " does not match file name $file" exit endif if (-f ${target}/${file}.${version}.Z) then echo "${target}/${file}.${version}.Z already exists!" difi $source exit endif cp $source ${target}/${file}.${version} compress ${target}/${file}.${version} # zipper ${target}/${file}.${version} # can't do - zip won't take periods! if (-f ${target}/${file}.${version}.Z) then echo saved file in ${target}/${file}.${version}.Z else echo FAILED TO SAVE file in ${target}/${file}.${version}.Z if (-f ${target}/${file}.${version}) then echo the uncompressed version is in ${target}/${file}.${version} endif endif endif exit # **************************************************************************** # **************************************************************************** # ** original code is below ************************************************** # **************************************************************************** # **************************************************************************** # Plan to revamp save: 2009 Aug 24 # use version string inside file. # If none, use dated save directly (no more dave script). # save into $old directory from environment. # use zipper instead of unix compress. # remove dependencies: # worcha # versave # lksaveform # location of old directory echo "version = 1.53 of save 2009 Aug 24" # 2009 Aug 24, 1.53: make save work when there is no worcha # 2009 Aug 16, 1.52: BUG: save /tmp/something crashes, sed can't handle '/' # 2009 Mar 07, 1.51: build saveform # 2009 Jan 29, 1.50: don't use versave. # 2009 Jan 29, 1.49: if versave doesn't exist, it dies - don't use versave. # 2007 Jun 26, 1.48: turn off IF tracking by commenting them out. # 2005 Jul 8, 1.47: guide.html failed # 2005 Apr 27, 1.46: Maybe you mean one of ... ls *$1* # 2005 Apr 21, 1.45: IF 3 error when another computer was down. # -> "If expression syntax" # Probably could not get whoami. Add quotes around $tmp. # Make the $tmp be ended with '.save'. # 2004 Dec 17, 1.44: dotpermit not correct, needs ls -F to get / # 2004 Dec 17, 1.43: crash report: caught warning, see bottom of file. # 2004 Sep 15, 1.42: crash report: # | dotpermit is -rw- # | IF statement number 8 # | if: Malformed file inquiry # Repeat and it ran fine!!! # It looks like the disk is intermittantly # inaccessible. The dash messed up the if statement. # May be solved. Confirmed by setting dotpermit # to '-rw-': the malformed if message appears # and it crashes. # Put quotes around if statement to protect it. # This avoids the crash. # 2004 Aug 3, 1.41: document IF to user # 2004 Jul 5, 1.40: adjust IF bug tracking # 2004 Jul 5, 1.39: move IF to the right # 2004 Jul 2, 1.38: bug: crash when saving save again # mysteriously resolved itself again! # Add IF tags to track it next time # 2004 Jun 16, 1.37: bug: crash when saving pm and save itself - # mysteriously resolved itself! # 2004 Feb 16, 1.36: bug: crash when saving sip2004-2005allstudents.sxc # if: Expression syntax # 2004 Feb 6, 1.35: unsolved bug see below. Does not replicate! # 2004 Feb 6, 1.34: if statement bug # # Could not locate bug!!! symptom: # strawberry 34% save save # version = 1.33 of save 2004 Jan 7 # save: executable c-shell script # The file type is ok, it can be saved # ~/script/old ~/script # SAVING IN:/data/scratch/toms/trash # if: Malformed file inquiry # # 2004 Jan 7, 1.33: more info about dir for chmod warning # 2004 Jan 7, 1.32: chmod warning # 2003 Nov 17, 1.31: cleanup # 2003 Nov 17, 1.30: bug in saving: increase worcha word max # 2002 Nov 7, 1.29: chmod the trash directory to be writeable # 2002 Jan 29, 1.27: don't make directory for files. # "origin way before 1998 May 11" set gap = " " # set IF = "${gap}${gap}IF statement number" # echo "${gap}${gap}IF statements are" # echo "${gap}${gap}for tracking a rare bug:" # echo "$IF 1" if !($#argv == 1) then echo "save a file into a temporary old directory." echo "If there is a 'saveform' then use this format." echo "If there is a 'version = ' string, then use it" echo "to name the backup, otherwise use the date and time." echo "" echo "For example, if the version string is:" echo "version = 9.11; (* of makelogo.p 2000 Mar 17" echo "then 'save makelogo.p' will save in the file" echo "makelogo.p.9.11" echo "" echo "The place that the file is saved is a directory named 'old'." echo "If this does not exist, a pointer to ~/misc/trash is used". echo "(See mkold for details)" echo "" echo "The proceedure used is:" echo "use versave to determine how to change the saveform file to" echo "make a save command file for the program (file worchap)" echo "make the save command file using worcha (in file fout)" echo "make the file executable" echo "execute the file" echo "move the files out of my hair, but don't destroy them" echo "the program will create the old pointer (a directory)" echo "and the saveform on the fly. If they weren't in the directory before," echo "they are removed." exit endif # make sure this is an ascii text file first!! set asciitext = "ascii text" set filetype = "`file $1`" echo "$filetype" set type = `echo $filetype | sed -e "s|$1||" | sed -e "s| ||" | sed -e "s|:||" ` # echo "$type" # if (("$type" == "ascii text")||("$type" == "executable c-shell script")||("$type" == "English text")) then # that's too persnikity!! set s = `echo "$type" | grep "executable SPARC"` # echo s is "'"$s"'" # echo "$IF 2" if ("$s" == '') then echo "The file type is ok, it can be saved" else echo "The file type is not $asciitext, it CANNOT be saved." echo "Use dave instead if you insist on saving this." exit endif set me = `whoami` set tmp = "/tmp/${me}.save" # a directory set tmp2 = "/tmp/${me}2.save" # a file set tmp3 = "/tmp/${me}3.save" # a file # echo $tmp # echo "$IF 3" if !(-d "$tmp") then mkdir "$tmp" # echo created $tmp else # echo using $tmp endif # echo "$IF 4" if (-f $1) then # make sure we have a place to put the files rm -f $tmp/fin $tmp/fout # echo "$IF 5" if !(-d old) then mkold >& /dev/null set madeold = 1 # echo set madeold = 1 else set madeold = 0 # echo set madeold = 0 endif # echo "$IF 6" if !(-f saveform) then # see if we can link to the user's saveform in their script directory lksaveform >& /dev/null set madesaveform = 1 # echo set madesaveform = 1 else set madesaveform = 0 # echo set madesaveform = 0 endif #echo $madeold " " $madesaveform set ver = `ver<$1 | grep 'no "version =" string' | wc -l | sed -e "s/ //g"` # echo $ver # if (`ver<$1 | grep 'no "version =" string' | wc -l | sed -e "s/ //g"` == 1) # echo "$IF 7" if ($ver == 1) then echo no version string dave $1 else # permitting old to be writeable pushd old echo -n SAVING IN: pwd # 2004 Dec 17: this is not sufficient to get the permissions of ".": # set dotpermit = `ls -lat | head -2|tail -1 | cut -c1-4` # use this instead, the head is a safety: #set dotpermit = `ls -latF | grep ' \./' | grep -v ' \.\./' |head -1|cut -c1-4` set dotpermit = `ls -latF | grep ' \./' | head -1|cut -c1-4` # Note that the -F puts a '/' at the end - a safety check along with the # leading space. This ensures that the name for . is unique. echo dotpermit is "'"$dotpermit"'" # echo "$IF 8" # code for testing only: #echo reset dotpermit #set dotpermit = '-rw-' #echo dotpermit is NOW RESET TO $dotpermit # zzz # if ($dotpermit != 'drwx') then if ("$dotpermit" != 'drwx') then echo "*********************************************" echo "**** WARNING: 'chmod u+w .' WAS REQUIRED ****" echo "*********************************************" echo Before chmod: ls -lat | head -2 | tail -1 chmod u+w . echo After chmod: ls -lat | head -2 | tail -1 endif popd echo version string exists # determine if versave program exists # this only works on the sun: # if (`which versave|tr ' ' '\n'|head -1` == 'no') then # detect slash in result for system independence: if (`which versave|grep '/'|wc -l` == '0') then echo not using native versave grep 'version = ' $1 | head -1 > $tmp2 echo '--- $tmp2' cat $tmp2 echo '---' cat $tmp2|sed 's/version = //'|tr ' ' '\n'|head -2|tail -1> $tmp3 set number = `cat $tmp3|tr ' ' '\n'|head -1` set file = `cat $tmp3|tr ' ' '\n'|head -3|tail -1` echo "number $number" echo "file $file" echo "$file" > $tmp/worchap echo "$number" >> $tmp/worchap echo '--- $tmp/worchap' cat $tmp/worchap echo '---' else echo using native versave versave < $1 > $tmp/worchap set file = '' endif echo --- worchap file: cat $tmp/worchap echo --- # make sure that the file name created by versave exists set foundname = `head -2 $tmp/worchap | tail -1` echo the found name is "'"$foundname"'" # echo "$IF 9" if !(-f "$foundname") then echo $1 was the name of the file # echo "$IF 10" if ("$foundname" == 'echo "program halt."') then echo "No version string, using dave." dave "$1" else echo BUT THE NAME INSIDE THE FILE DOES NOT EXIST! echo It was "'"$foundname"'" echo PROGRAM HALT endif exit endif #echo $foundname #cat saveform # this is silly since saveform was already created! if (-f saveform) then echo using given saveform: ---- cat saveform echo -------------------------- cp saveform $tmp/fin else echo creating saveform echo 'cp file old/file.version' > $tmp/fin # echo 'compress old/file.version' >> $tmp/fin # echo 'zipper old/file.version' >> $tmp/fin # can't do this! # zipper would create old.zip instead of old/file.version.zip! echo 'pushd old' >> $tmp/fin echo 'zipper file.version' >> $tmp/fin echo 'popd' >> $tmp/fin echo 'echo saved file in old/file.version' >> $tmp/fin endif # echo '... moving to $tmp' pushd $tmp > /dev/null # clear if (`which worcha|grep '/'|wc -l` == '0') then echo not using native worcha # existance of worcha assumed to be tied to existance of versave if ("$file" == '') then echo versave exists, worcha does not - finish purifying. exit endif cat $tmp/fin |\ sed "s/number/$number" |\ sed "s/file/$file" |\ cat > $tmp/fout else echo using native worcha worcha > /dev/null endif chmod u+x fout popd > /dev/null # clear # echo '... and back again' echo ------ executing: cat $tmp/fout echo ----------------- $tmp/fout rm -f $tmp/fin $tmp/fout $tmp/worchap endif # echo "$IF 11" if ($madeold == 1) then # echo removing old rm -f old endif # echo "$IF 12" if ($madesaveform == 1) then # echo removing saveform rm -f saveform endif else echo "A file named $1 does not exist in this directory\!" if (-d "$1") then echo "$1 is a directory" exit else if (-e "$1") then echo "$1 is not a plain file:" echo `ls -l $1` exit else #echo BUBBA 0 # set others = "`ls *$1* >& /dev/null`" set others = "`ls *$1* |& cat`" # set others = "`ls *$1*`" #echo BUBBA 1 others:l"$others"l # ls *$1* if ("$others" == 'No match') then # There are no suggestions to be made exit endif if ("$others" != '') then # Suggest possible files echo 'Maybe you mean one of these?' echo "${others}" | tr ' ' '\n' endif endif endif endif exit ******************************************************************************** ******************************************************************************** ******************************************************************************** # 2004 Dec 17, 1.42: crash report: caught warning, see bottom of file. Unsent email on trapped bug: ----- To: admins Subject: caught disk error David: I put a set of IF statements into my save routine (~toms/script/save) to try to catch a rare crash of that script. Though I think I had fixed the bug, it was so rare that I needed the If statements. Today I caught something. The results were: strawberry 60% n sl /home/strawberry/toms/paper/scilogo strawberry 61% s version = 1.42 of save 2004 Aug 3 IF statements are for tracking a rare bug: IF statement number 1 paper.tex: ascii text IF statement number 2 The file type is ok, it can be saved IF statement number 3 IF statement number 4 IF statement number 5 IF statement number 6 IF statement number 7 ~/paper/scilogo/old ~/paper/scilogo SAVING IN:/export/data/scratch/toms/trash dotpermit is -rw- IF statement number 8 ********************************************* **** WARNING: 'chmod u+w .' WAS REQUIRED **** ********************************************* Before chmod: -rw------- 1 toms delila 1213503 Dec 17 09:20 .target1.2004Dec17.09:20:00 After chmod: -rw------- 1 toms delila 1213503 Dec 17 09:20 .target1.2004Dec17.09:20:00 ~/paper/scilogo version string exists I ran versave --- file scilogo.tex version 1.07 --- the found name is 'scilogo.tex' IF statement number 9 mkdir: Failed to make directory "old/scilogo.tex.1.07"; File exists scilogo.tex.Z already exists; do you wish to overwrite scilogo.tex.Z (yes or no)? y scilogo.tex.Z: already has .Z suffix -- no change saved scilogo.tex in old/scilogo.tex.1.07 IF statement number 11 IF statement number 12 strawberry 62% As you can see, there is a warning in the middle. it is that ... ----- So it looks like a dot file was found instead of '.' ... strawberry 52% ls -lat | grep '\./' | head drwxr-xr-x 2094 toms delila 515584 Dec 17 09:36 ./ drwxr-xr-x 9 toms delila 512 Aug 1 09:05 ../ Change permits: drwx------ 2094 toms delila 515584 Dec 17 09:36 ./ I changed the logic to grep the '\./'. It should be fixed now.