I'm working on a bash script to iterate over a helm chart to get just the images out using simple commands like cat, grep, sed.
Let's call it chart A. Inside chart A, I will perform the command cat | grep | sed ... to get the docker images out to a file. Also, inside chart A, there may or may not be a charts folder. If there is a charts folder, create an array of all sub-folders inside that charts folder and go in each of these sub-folders and repeat from the beginning. This recursion goes on until there's no more charts folder to process, all sub-folders in all charts folder are processed and at each folder being processed, the command cat | grep | sed ... is run.
Below currently is my code writeup. When I ran the script, it didn't iterate through all the sub-folders in all charts folders as expected.
retrieve_images() {
basedir=$1
dirname=$2
# Run the command cat, grep, sed at each processing folder
cat | grep | sed ...
# Check if passed dirname is a charts folder, if so then create an
# array of all sub-folders inside this charts folder and
# recursively call retrieve_images on each of the sub-folders
if [ $dirname == 'charts ]
then
# Creating array of sub-folders in $basedir/$dirname
array=( "$basedir"/"$dirname"/* )
# Remove leading $basedir
array=( "${array[@]#$basedir/$dirname/}" )
# Recursively call retrieve_images on each sub-folder in the
# array
for i in "${array[@]}"
do
retrieve_images $basedir/$dirname ${array[i]}
done
# If passed dirname is not a charts folder, check if there's a
# charts folder inside of dirname. If there is, run retrieve_images
# with the charts folder
else
if [ -d $basedir/$dirname/charts ]
then
retrieve_images $basedir/$dirname charts
fi
fi
}
Then I just called the function right below, passing in $basedir as the current directory that I'm in, and $dirname is a helm chart inside my current directory as well.
retrieve_images . ./A
I'm still fairly new to bash scripting, let alone working with functions and recursion using bash. Thanks for any assistance in pointing out anything wrong or fixes to my script.
CodePudding user response:
Sorry to ask, but doesn't something like this solve your problem?
find /PATH_level_1 -name "*.yml" | xargs grep "image: *"
CodePudding user response:
Even simpler single grep command, can traverse directories recursively. Inspecting only files matching --include pattern:
grep -r --include="*.yaml" "image: .*" /PATH_level_1
