Awk|Bash – C++ file, enhances the version of the file in its name.
Home page Ubuntu C++ Hardware HTML i javascript Java Software SQL

Awk|Bash – C++ file, enhances the version of the file in its name.

Added: 2012-08-04

The script automatically increases the version of the file (the class name, for another file, you need to edit script). If you are working on Class_0.1.h it is class name, this script can automatically increase the version of the file name to: Class_0.2.h, Class_0.3.h etc.

The script has one parameter, which is the name of the file, whose you want to increase version. The file name should be structured:

  • [nameFile] Class_
  • [versionFile] 0.1
  • [extension] .h

Below an example of a script for a class called Class_0.1.h. You type by parameter only [nameFile].

./increase_version.bash Class_

#!/bin/bash
echo "The script increase version of class file.";
echo "";

if [ "$1" = "" ]; then
	echo "";
	echo "Argument does not exist!!!";
	exit 102;
fi;



ls | grep "$1*" | awk -vn=$1 'BEGIN{
	versionHFile = "";
	versionCFile = "";
	lw = length($0);
}{
	print $0;
	posExt = index($0, ".h");
	if (posExt != 0){
		ls = length(n);
		versionHFile = substr($0, ls+1, (lw - (lw - posExt +1)-ls));
	} else {
		posExt = index($0, ".cpp");
		if (posExt != 0){
		       	ls = length(n);
		        versionCFile = substr($0, ls+1, (lw - (lw - posExt +1)-ls));
		}
	}

}END{
	if (versionHFile != ""){
		print "Increase version...";	
		versionHFileNew = versionHFile + 0.1;
		ext = ".h";
		if (((versionHFileNew % 2) == 0) || ((versionHFileNew % 2) == 1))
			ext = ".0"ext;
		print "cp "n""versionHFile".h "n""versionHFileNew""ext;
		system("cp "n""versionHFile".h "n""versionHFileNew""ext);
	}

	if (versionCFile != ""){
		print "Increase version...";
                versionCFileNew = versionCFile + 0.1;
		ext = ".cpp";
		if (((versionCFileNew % 2) == 0) || ((versionCFileNew % 2) == 1))
			ext = ".0"ext;
		print "cp "n""versionCFile".cpp "n""versionCFileNew""ext;
		system("cp "n""versionCFile".cpp "n""versionCFileNew""ext);
	}
}'

exit 0;