Define Version with script in RPM spec file

I would do a wrapper script. That allows you to decide things like if it is a regular release or a development one, etc. Then you can pass the variables with the --define option - see this question for more options.

To respond to @Herrgott's comment "if you %define it in spec and try to redefine with --define it won't override that" (can't have newlines in comments) - you can set an "_override" variable of the same name with --define.

%if 0%{?val_override:1}
%define val %{val_override}
%else
%define val whatever
%endif

You must be having a shell script that invokes rpmbuild command. You can use that script to compute version (or for that matter, any command that you are trying to use in rpm spec file).

Change your original code,

%define version %(echo "$(sed -n 's|^[ ]*appVersion = "\(.*\)"|\1|p' /fullfilepath/values.txt | sed 's/^\(.*\)-.*$/\1/')")
Version: %{version}

to,

%define version _VERSION_
Version: %{version}

and sed VERSION to its computed value in the shell script that calls rpmbuild (before invoking rpmbuild). After the actual spec contents are dumped to some file, pass on that generated file to rpmbuild in the same shell script.

Here is a summary of the steps:

Assuming you have a builder.sh shell script that calls rpmbuild, follow below steps:

  1. Update your spec file to have VERSION placeholder string/macro as show above
  2. Move current rpm spec file to my_package_template.spec
  3. in builder.sh, run command(s) to get your version and save the version to a variable
  4. Use sed command on my_package_template.spec file to replace VERSION by this computed version, and save the sed output to my_package.spec
  5. Pass my_package.spec to rpmbuild command.

Repeat steps 1, 3 and 4 for replacing usage of any other shell commands inside your spec file.