I have a program in a “Working directory” but I also have multiple versions of this program in other folders. I want to easily change the version that resides in the working directory while backing up the current residing working one.
Currently, I have to do this manually with the following keystrokes.
cd /working/directory
cat version_number_file (to find out what version Im working with)
cd /working
mv -i foo/ /program_version_xxx (version identified from above)
mv -i bar/ /program_version_xxx (version identified from above)
cd /mnt/foo/bar/some/folder
mv -i file.info /program_version_xxx (version identified from above)
now the program is backed up, we can move the wanted version into the working directory.
I want to create a script that moves the files in the working directory into its respective “version folder” for later use and copies the version I want to use back into the working directory.
The said program has a file with it’s version number in it and that correlates to the same folder name where it resides when not in use. So the script should be able to cat this file in the working directory and see the version number, and know what folder those files belong in.
I don’t mind having multiple scrips, as in one for each version I want to change to. Better if the script had a menu system that would ask what version I wanted to switch to (but that’s probably asking to much)
I havent tested this. But i think it gives you an idea.
consider it meta code if anything.
but why arent you using git for version control?
#!/bin/bash
version=cat ./version.txt
echo “backing up old version”
cmd="mkdir ./versions/someprogram_$version "
eval $cmd
cmd=“mv someprogram version.txt ./versions/someProgram_$version”
eval $cmd
echo “Copying new version to location”
cmd=“cp /location/of/newVersion ./”
eval $cmd
#This part is only if the new version does not contain a new version file #if so. use cat
echo “Incrementing version” #mind this sets a length of 9 possible versions, but it gives you a general idea of substrings #Alternately use ${#variablename} for strlen
oldVersion=${version:4:1}
newVersion=${version:1:4}$(($oldVersion+1))
val=cat filename | grep “The version”
substr=${val:start:numberOfDescribingIndexes}
grep is allways your friend.
feks.
The version is build_number_1.1.1
would be
val=cat filename | grep “The version”
substr=${val:29:5}
a string e.g. “The version is build_number_1.1.1”
is a character array if a certain length.
saying you only need string:startindex:numberOfIndexes means
start at. The version is build_number_(28 indexes/characters, so +1)1.1.1
then read 5 more indexes
so 1[1] .[2] 1[3] .[4] 1[5]
If you want to parse hello out of “Hello World” in bash you would make a substring of .
val=echo “Hello world”
echo ${val:1:5}
Also, I’m not incrementing versions. I just have about 5 different versions of a program which each of them are in their own folder on /
I just want to move the folders of one or another version into the /working/directory while placing the old one back into it’s own folder (for sake of logs)
Thanks, this makes more sense. since there is a space between is and build_ would that count as 1? I do want to parse out build_number along with the _xxx So I guess the start would be 2? Then to use that extracted value, I’d put in $substr in place of it in the script?
Sorry, I’m new to scripting things with logic built in.
all characters are a index space is just a special character, like # and !, look up ascii characters.
But ill reiterate, and repeat eden, why not just use git? is there some special use case?
all this could be handled in a console using git, using only a couple of commands like git pull, git commit -m “some message” etc.
Normally when my co-workers write some sloppy code i simply git reset, and then write an angry email.
So, Piggybacking off of this, what would be the best way to start off the script with a
val=cat /working/directory/version_number_file | grep "The version" substr=${val:29:5}
if val=expected_version_number
then continue
else echo "error"
I’m sure I formatted that wrong but yeah. I hope that makes sense. Basically I want to run this, have it find out the version number from within a text file, expecting a specific version number (as sort of user error checking before continuing the script), and if it’s not the right version, then quit
val=some inline command
if [[ $val -eq “expected_version” ]] #[[]] means were working with strings
then
cmd=“Some_Arbitrary_Command”
eval $cmd
else
cmd=“some_other_arbitrary_command”
eval $cmd
fi
Thanks! I was actually able to get it working differently
echo "Checking current working version"
grep "Build name" /working/directory/version.info | while read line ; do
VERSION=$(echo $line | awk -F "Build name is " {'print $2'})
echo "$VERSION"
done
if [ $VERSION=desired_version_xxx ]; then
echo true
else
echo false
fi
The output was the build number followed by true so I guess I did it right! So I can run my move commands from there under the if/then/else statements. I spent way to much of my day getting just this far and I’m sure a bash guru would’ve done that in like 5 minutes or less. At least I’m learning