[SOLVED]Need help creating a bash script

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.

cd /program_version_xxx (desired version)

mv -i foo/ /working

mv -i bar/ /working

mv -i file.info /mnt/foo/bar/some/folder/

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)

Or pass an argument to the script with the version number.

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))

echo $newVersion > ./version.txt

Not sure if I understood correctly but this might work. Make the scripts executable with:

chmod +x backup.sh work.sh

Backup Script:

#!/bin/bash
back_num=$(cat /working/directory/version_number_file)

mkdir -p /working/directory/$back_num
mv -i /working/directory/foo/ /working/directory/bar/ /working/directory/$back_num
mv -i /mnt/foo/bar/some/folder/file.info /working/directory/$back_num

Work Script:

#!/bin/bash
ls -d /working/directory/*/

read -p "Version number? " work_num

mv -i /working/directory/$work_num/foo/ /working/directory/$work_num/bar/ /working/directory
mv -i /working/directory/$work_num/file.info /mnt/some/folder
1 Like

There is a small problem that I need help figuring out.

the file.info is a text file that basically is a description of the version and 4 or 5 lines down the version is at the end of that line.

something like

The version is build_number_xxx

So I dont know how to make bash skip the “the version is” and just grab the end and set it as the variable

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}

2 Likes

I’m not familiar with this, what is it doing?

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.

Maybe I’m thinking wrong here, but isn’t this what git and others is for?

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.

Yes it’s for a special use case on an air-gapped net :sleepy:

program is a tad proprietary in house thing so that’s why I was a little vague on the naming schema.,

I appreciate the help @Lauritzen and @drunkenhazard . I’ll probably mix and match some of both examples for my problem.

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

1 Like

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 :disappointed_relieved: and I’m sure a bash guru would’ve done that in like 5 minutes or less. At least I’m learning

2 Likes

You have more than one version per line?

Just another way you could do it (may not be portable?)

while IFS="," read -r -a line; do
  echo "Build name is" ${line[1]}
done < <(grep "Build name" /working/directory/version.info)

the .info file has many lines of text describing the version, but only one of the lines has the version number in it, which is what I wanted to grab.

1 Like

You could just grab that directly with awk or grep.

Is the line unique?

awk i don’t know well enough atm. grep you could do this

grep "Build name is" | cut -d, -f2

Assuming it was comma delimited for example (TAB by default) and you can match the line uniquely.

If you’re just looking for one line you should be able to match it without a loop.

1 Like

yes, its the only line that starts with “Build name is” followed by the number

1 Like

You should be able to get rid of the whole while loop then :slight_smile:

1 Like