bashfor-loopfortify

Loop run an executable against all elements


I am pretty much trying to find a way to automate a purge in bulk of multiple apps in my test server. I mounted the server client fortifyclient inside a test pod and the pod can communicate with the server. So the flow:

  1. List all my apps and grep all their applications versions id's
  2. Purge those application versions id's
  3. done

So as a first step I set up my environment variables inside the pod.

# set env var locally
export token=value
export scandate=value
export sscurl=value
export dir=/test/bin/./fortifyclient

This is my purge.sh below

#!/bin/bash

# List app, fetch their appID's then purge based on date

for i in $("$dir" listApplicationVersions -url "$sscurl" -authtoken "$token" | grep -P "^[0-9]+" | awk '{print $1}')
do
   "$dir" purgeApplicationVersion -applicationVersionID "$i" -scanDate "$scandate" -url "$sscurl" -authtoken "$token"
done

output

5513
4659
4658
4688
4685
4686
4687
4683
do
   "/test/bin/./fortifyclient" purgeApplicationVersion -applicationVersionID "" -scanDate "value" -url "value" -authtoken "value"
done

As you can see that the first argument is working fine as I get my apps listed but the second argument to purge is not working. As you can see the $i is null and it should be the ALL the elements of the -applicationVersionID listed above is empty. I am not so strong in bash and feel free to share any alternatives. I would appreciate some feedback. I know I am close. Thank You


Solution

  • When I try to reproduce your script behaviour, my script flags an error about the "$dir" directory reference withing the $(). If you look at the POSIX references, it will indicate that it cannot expand anything with "/" among other characters.

    That may be your problem as well.

    This suggest that your listApplicationVersions should incorporate any usage of the "$dir" reference, and remove that from the $() expression. Also, for consistency within your context, you should probably do the same for the purgeApplicationVersion script and remove the "$dir" from your purge.sh .