Programming logic

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

Programming logic

#1 Post by 8-bit »

I was thinking on posting or not posting this.
But here are steps I took.

(1) Problem:
You have two types of frugal Puppy pupsave file backups with each having a given name format.
You also want to be able to do a straight copy if those names do not match the input file.
In the case of the pupsave backups, the destination file name has to have a date-time stamp in the filename removed.
In the case of another file name, it has to be passed unchanged.
(2) logic and solution
This script needs to be run from a terminal. But it is commented for logic.
From a terminal/command line, you type the name you gave this script along with a file name with path.
Backed up pupsave files using hotbkp, or dated backup utilities have a format like this:
hotbkp is /mnt/home/pupsave.2fs.BKP-11.10.04_12.30
dated backup is /mnt/home/pupsave-11.10.04_12.30.2fs
other files can be any path/file name.

Here is what I came up with some of which is used in my PupsaveRestore.sh
It also shows that I try to solve a problem step by step.
I called this short script fname.sh and after copying and saving it, you will have to make the file executable.
To run from terminal command line #./fname path/filename

Code: Select all

#! /bin/bash
PUPSAVE_FILE=$1 #Get source file from command line
	FS="$PUPSAVE_FILE" #Give variable shorter name
    LAST=${FS##*/} # Removes Path from filename
#FRST=${LAST%%save*};echo FRST=$FRST
#LST=${LAST##*save};echo LST=$LST
    chk=$(echo ${LAST/BKP})  #Check for "BKP" in source file name and delete in variable chk
    if [ ! "$chk" = "$LAST" ]; then # If chk does not equal LAST, "BKP" existed in name process name
#     chk=$(echo $LAST | grep "BKP")
#     if [ $chk = $LAST ]; then

    newname=`echo $LAST | sed "s/...................$//"` #That is 19 dots in sed Removes date-time
    else newname="$LAST" #If "BKP" did not exist in LAST newname is same as LAST
    fi
EXT1=${LAST##*.} # Get LAST extension
#Check LAST extension and process if one of 2fs,3fs,4fs otherwise pass newname unchanged
if [ $EXT1 = "2fs" ]
 then
   EXT2=.$EXT1
newname=`echo $LAST | sed 's/-.*'$EXT2'/-'$EXT2'/' | sed 's/-//'`
fi
if [ $EXT1 = "3fs" ]
   then
   EXT2=.$EXT1
newname=`echo $LAST | sed 's/-.*'$EXT2'/-'$EXT2'/' | sed 's/-//'`
fi
if [ $EXT1 = "4fs" ]
  then
  EXT2=.$EXT1
newname=`echo $LAST | sed 's/-.*'$EXT2'/-'$EXT2'/' | sed 's/-//'`
fi  
 echo PUPSAVE_FILE = $PUPSAVE_FILE #Show Source file name
echo newname = $newname #Show modified name for copying
echo EXT2 = $EXT2 #Show filename extension of source file in LAST

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post by technosaurus »

Code: Select all

BASNAM=${1##*/}
case ${BASNAM} in
*.?fs)EXT=${BASNAM##*.};newname=${BASNAM%.?fs}.$EXT;;
*BKP*)newname=${BASNAM%%.BKP*};;
esac
Last edited by technosaurus on Tue 04 Oct 2011, 23:12, edited 1 time in total.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#3 Post by 8-bit »

I know others have ways to improve this code.
I only posted it so others could get an idea of how I try to accomplish solving a problem. And that I am not as proficient at coding as others.

So, posting the code was not a request for improvement of it. It was only to show how I solved a problem.
If one tries the script as written, it works. Sure, a better programmer can improve on it.

I have had some comment that they thought my PupsaveRestore.sh script would allow them to do a HOT restore. If you want a real challenge, tackle that.
Or another asked if it was able to copy multiple files dropped in the source box to a destination directory.
That is another modification one could tackle.
I also know I could have used grub to find a substring in a string being the filename.

But thanks for your snip-it anyway. One never stops learning.

tech...
Adding "#! /bin/bash to your example and trying it gave me an error of : line 6: ${%.?fs}.$EXT: bad substitution

That is if it was supposed to replace the script I posted.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

there should have been a BASNAM before the % - post edited
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#5 Post by 8-bit »

Again, thank you technosaurus.
You have shown that different programmers write code as to their ability.
The only thing that was missing from your example to make it fully function like my example was the addition of one last test that would assign the variable newname the contents of BASNAM if it ended up empty after your code ran.
And it would end up empty if a filename that was not a pupsave backup name like "/mnt/home/somefile.txt"
Without that last test the variable newname would end up empty for a copy command of cp $BASNAM $DIR/$newname.
I simplified the example somewhat by not including a destination directory input.

With the addition of a test at the end though, it would function the same as my example with a lot less code.
Of course it relies on some givens.
Those givens are the structure of the pupsave file name and relying on one having "BKP" in the name and the other having a file name extension of "?fs.

Anyway, it shows me things that I again did not know
Also, the right parenthesis without a left I still do not get.
Is it the context it is used in that allows it without a left parenthesis?

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#6 Post by seaside »

8-bit wrote:
Anyway, it shows me things that I again did not know
Also, the right parenthesis without a left I still do not get.
Is it the context it is used in that allows it without a left parenthesis?
8-bit,

As far as I know, the "case statement" is one of the few places where you'll find a right without a left and it seems to suit the situation of providing a nice id ending to the variable.

Regards,
s
(Yes, many ways to do the same thing and we'll never know them all :) )

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#7 Post by 8-bit »

What I find interesting is that I am able to deciper/understand technosaurus's code snip-it as to how it works.

And thanks seaside for the explanation of the parenthesis use with the case command.

Post Reply