depth first search in Bash script using simple data structures - bash

I'm trying to do as stated above. I have designed a breadth first search with relative ease.
The goal of the script is to create a directory structure of a certain depth and breadth input by the user. I'm trying to alter my breadth first implementation to support depth first search. This is where I got:
depthsearch(){
local open=("seed")
local tmpopen=()
local closed=()
local x="seed"
for((j=0;j<$depth;j++)); do
for x in "${open[#]}" ; do
for ((i=0;i<$breadth;i++)); do
tmpopen=("${tmpopen[#]}" "$x/$i")
mkdir echo "$x/$i"
done
open=("${tmpopen[#]}" "${open[#]:1}")
tmpopen=()
closed=("${closed[#]}" "$x")
done
tmpopen=()
done
}
Okay, so I trimmed down my question a bit. Apparently the problem was that I wasn't iterating by index, so I couldn't update my loop while it was iterating. However, I can't figure out how to iterate by index and update my array so I can construct my directories depth first. Any example would be appreciated.

If it is not strictly necessary to use data structures, you can do it with a simple recusion:
#!/bin/bash
depth=4
breadth=3
node_id=0 #for testing, increments +1 each time a folder is created.
# args: level( [0,depth) ), childNo ( [0,breadth) )
generateTreeDFS(){
declare -i level=$1
declare -i childNo=$2
declare -i i=0
if (( $level < $depth ));then
mkdir "n_$childNo-$node_id"
cd "n_$childNo-$node_id"
let node_id++
let level++
while [ $i -lt $breadth ]
do
generateTreeDFS $level $i
let i++
done
cd ..
fi
}
If we call
generateTreeDFS 0 0
The directory structure will be like this:
$ tree .
.
├── depthsearch.sh
└── n_0-0
├── n_0-1
│   ├── n_0-2
│   │   ├── n_0-3
│   │   ├── n_1-4
│   │   └── n_2-5
│   ├── n_1-6
│   │   ├── n_0-7
│   │   ├── n_1-8
│   │   └── n_2-9
│   └── n_2-10
│   ├── n_0-11
│   ├── n_1-12
│   └── n_2-13
├── n_1-14
│   ├── n_0-15
│   │   ├── n_0-16
│   │   ├── n_1-17
│   │   └── n_2-18
│   ├── n_1-19
│   │   ├── n_0-20
│   │   ├── n_1-21
│   │   └── n_2-22
│   └── n_2-23
│   ├── n_0-24
│   ├── n_1-25
│   └── n_2-26
└── n_2-27
├── n_0-28
│   ├── n_0-29
│   ├── n_1-30
│   └── n_2-31
├── n_1-32
│   ├── n_0-33
│   ├── n_1-34
│   └── n_2-35
└── n_2-36
├── n_0-37
├── n_1-38
└── n_2-39

Related

How do I split up the settings.kts file for TeamCity's Kotlin Configuration?

I have a TeamCity settings.kts file where it consists of the Root Project and hence all subsequent sub project. Currently, it's one big massive file and I am trying to split up the KTS file based on projects.
What's the best practice to split up the settings file? Should I do a file per project and how do I reference them from the main settings file?
TeamCity generates a singe settings.kts file only for small projects.
You can try and play with some big project, download settings in Kotlin format for it.
E.g., here is how generated by TeamCity big project settings look like:
nadias-mbp:projectSettings 2 nburnasheva$ tree
.
├── README
├── ServiceMessages
│   ├── Project.kt
│   └── buildTypes
│   ├── ServiceMessagesChangeBuildStatus.kt
│   ├── ServiceMessages_BuildProgressServiceMessage.kt
│   ├── ServiceMessages_ErrorParsingServiceMessage.kt
│   ├── ServiceMessages_FailBuild.kt
│   └── ServiceMessages_ReportBuildParameterDoNotReport.kt
├── ServiceMessages_ReportBuildParametersChar
│   ├── Project.kt
│   └── buildTypes
│   ├── ServiceMessages_ReportBuildParametersChar_ReportBuildParameter.kt
│   ├── ServiceMessages_ReportBuildParametersChar_ReportBuildParameterWaitReasonWithTooLongValue.kt
│   └── ServiceMessages_ReportBuildParametersChar_ThreadSleep.kt
├── ServiceMessages_ReportBuildParametersChartCopy
│   ├── Project.kt
│   └── buildTypes
│   └── ServiceMessages_ReportBuildParametersChartCopy_ReportBuildPara.kt
├── _Self
│   ├── Project.kt
│   ├── buildTypes
│   │   ├── AnsiParseAnsiColorLoggerOutput.kt
│   │   ├── BuildStepsAutodetection.kt
│   │   ├── CheckPromptParameter.kt
│   │   ├── EchoBuildIdToFile.kt
│   │   ├── EchoParametersToConsole.kt
│   │   ├── EchoUmlaut.kt
│   │   ├── FailBuildOnTextInTheLogs.kt
│   │   ├── MpsQuottingTest.kt
│   │   ├── RunGitCommand.kt
│   │   ├── RunMavenFromCommandLine.kt
│   │   ├── SetPasswordParameterInServiceMessages.kt
│   │   ├── SimpleWindowsEcho.kt
│   │   ├── SparseFile.kt
│   │   └── StderrRunAsOnMacOS.kt
│   └── vcsRoots
│   ├── HttpsGithubComBanadigaPhotoBackupGitRefsHeadsMaster.kt
│   └── HttpsGithubComBurnashevaCommandLineRunnerGitRefsHeadsMaster.kt
├── pom.xml
└── settings.kts
9 directories, 32 files
And here is the content of settings.kts:
import jetbrains.buildServer.configs.kotlin.v2018_2.*
version = "2019.1"
project(_Self.Project)

How can I recursively go to every folders and execute shell script with the same name?

I have this directory.
.
├── animation
│   ├── animation-events
│   │   ├── app.js
│   │   ├── app.mustache.json
│   │   ├── create_view.sh
│   │   └── assets
│   │   └── dummy_character_sprite.png
│   └── change-frame
│   ├── app.js
│   ├── app.mustache.json
│      ├── create_view.sh
│   └── assets
│   └── dummy_character_sprite.png
├── app.css
├── app.mustache
├── bunch_of_functions.js
├── decorators.js
├── create_all_views.sh
└── displaying-a-static-image
├── app.js
├── app.mustache.json
├── create_view.sh
└── assets
└── piplup.png
I want for create_all_views.sh to execute all create_view.sh in the children directory. How can I achieve such thing?
As you are in Ubuntu, you have the GNU implementation of find,
which has the -execdir option,
and you can do like this:
find path/to/dir -name create_view.sh -execdir ./create_view.sh \;
That is,
for each create_view.sh file it finds in the directory tree,
it will execute ./create_view.sh in the directory of that file.

Gradle scripting: copy content of child folders and additional folder to new locations

I have the following folder structure (there is an arbitrary number of child folder and the names are not known). I only have the path to the parent folder available.
Parent
| Child_folder_0
| Child_folder_N
as well as a separate folder called contentFolder
I am trying to copy the each child folder (and it's content) into a different location as well as copy the content of contentFolder into each child folder's new location.
Thanks!
The following code:
def parentFolder = 'Parent'
def contentFolder = 'contentFolder'
def destDir = 'destDir'
task copyChildFilesFromParent(type: Copy) {
from(parentFolder) {
include '**/*'
}
into destDir
}
task copyContentFilesIntoChildren() {
(parentFolder as File).eachDir { childDir ->
copy {
from(contentFolder) {
include '**/*'
}
into "$destDir/${childDir.name}"
}
}
}
task copyFiles(dependsOn: ['copyChildFilesFromParent', 'copyContentFilesIntoChildren'])
Will transform the following directory structure:
├── build.gradle
├── contentFolder
│   ├── content.txt
│   └── data.dat
└── Parent
├── Child_folder_0
│   ├── 0.dat
│   └── data.0
├── Child_folder_1
│   ├── 1.dat
│   └── data.1
├── Child_folder_2
│   ├── 2.dat
│   └── data.2
└── Child_folder_N
├── data.N
└── N.dat
into
├── destDir
│   ├── Child_folder_0
│   │   ├── 0.dat
│   │   ├── content.txt
│   │   ├── data.0
│   │   └── data.dat
│   ├── Child_folder_1
│   │   ├── 1.dat
│   │   ├── content.txt
│   │   ├── data.1
│   │   └── data.dat
│   ├── Child_folder_2
│   │   ├── 2.dat
│   │   ├── content.txt
│   │   ├── data.2
│   │   └── data.dat
│   └── Child_folder_N
│   ├── content.txt
│   ├── data.dat
│   ├── data.N
│   └── N.dat

concatenate all imports into one scss file

I'm currently working on a SASS library which is a long list of partials imported into 1 file.
The file structure is as following:
css-directory
├── functions
│   ├── _px2em.scss
│   └── _unitless.scss
├── helpers
│   ├── _align.scss
│   ├── _clearfix.scss
│   ├── _float.scss
│   ├── _hidden.scss
│   ├── _invisible.scss
│   ├── _ir.scss
│   └── _visuallyhidden.scss
├── layout
│   ├── _blockquote.scss
│   ├── _button.scss
│   ├── _fixed-footer.scss
│   ├── _form.scss
│   ├── _list.scss
│   ├── _loading.scss
│   ├── _triangle.scss
│   └── _truncate.scss
├── modules
│   ├── _grid.scss
│   └── _normalize.scss
├── prefixes
│   ├── _animation-delay.scss
│   ├── _animation.scss
│   ├── _background-clip.scss
│   ├── _borderbox.scss
│   ├── _border-radius.scss
│   ├── _box-shadow.scss
│   ├── _box-sizing.scss
│   ├── _flex.scss
│   ├── _font-face.scss
│   ├── _gradient.scss
│   ├── _hyphens.scss
│   ├── _keyframes.scss
│   ├── _transform-origin.scss
│   ├── _transform.scss
│   ├── _transition.scss
│   └── _user-select.scss
└── _verepo.scss
and the _verepo.scss file imports all the other partials.
I'd like to be able to concatenate _verepo.scss and it's partials into 1 .scss file so that I can then distribute it easily.
Found a solution, by using grunt and grunt-contrib-concat.
Fixed it with the following code block inside of grunt's initConfig:
concat: {
options: {
banner: '/*! veRepo.scss v%VERSION% | MIT License | https://github.com/varemenos/verepo */\n\n'
},
dist: {
src: [
'src/functions/_**.scss',
'src/prefixes/_**.scss',
'src/helpers/_**.scss',
'src/layout/_**.scss',
'src/modules/_**.scss',
],
dest: '_verepo.scss'
}
}

Using Brunch.io together with Apache Maven in order to build a java webapp

I am trying to use Brunch.io in order to simplify my javascript life.
My app is a java web archive (.war) and I use Maven as a build tool.
I am planning to have a ./brunch directory at the same level as the ./src directory (see tree output below).
Before I switch to brunch, I have a few questions:
Can I specify public: '../src/main/webapp' as a path in config.coffee and safely use brunch without fear that it will delete content from src/main/webapp?
Can I leave my Thymeleaf templates in WEB-INF/web-templates?
Is there any other points to take into account?
Thanks in advance for your input.
Here is the output from the tree command:
./src/main/webapp/
├── fonts
│   ├── glyphicons-halflings-regular.eot
│   ├── glyphicons-halflings-regular.svg
│   ├── glyphicons-halflings-regular.ttf
│   └── glyphicons-halflings-regular.woff
├── js
│   ├── custom
│   │   ├── addressAutocomplete.js
│   │   ├── languageChooser.js
│   │   ├── messages.js
│   │   ├── postcodeChooser.js
│   │   ├── resendActivationEmail.js
│   │   ├── signup.js
│   │   ├── trainings.js
│   │   └── workExperiences.js
│   ├── libs
│   │   ├── angular.js
│   │   ├── bootstrap.js
│   │   ├── bootstrap.min.js
│   │   ├── jquery-1.10.2.js
│   │   └── jquery-ui-1.9.0.custom.js
│   └── plugins
│   ├── chosen.jquery.js
│   ├── chosen.jquery.min.js
│   ├── component.json
│   ├── jquery.maskedinput-1.3.js
│   ├── jquery.maskedinput-1.3.min.js
│   ├── select2.jquery.json
│   ├── select2.js
│   └── select2_locale_fr.js
├── media
│   ├── checked.png
│   ├── favicon.png
│   └── nav-active-arrow.png
├── styles
│   ├── bootstrap.css
│   ├── bootstrap.min.css
│   ├── bootstrap-theme.css
│   ├── bootstrap-theme.min.css
│   ├── chosen.css
│   ├── chosen-sprite.png
│   ├── select2.css
│   ├── select2-custom.css
│   ├── select2.png
│   ├── select2-spinner.gif
│   ├── select2x2.png
│   ├── signin.css
│   ├── sticky-footer-navbar.css
│   └── style.css
└── WEB-INF
├── spring
│   └── webmvc-config.xml
├── tiles-defs.xml
├── web-templates
│   ├── advertisement
│   │   ├── childminder
│   │   │   ├── edit.html
│   │   │   ├── edit.html.old
│   │   │   └── new.html
│   │   ├── family
│   │   │   ├── edit.html
│   │   │   └── new.html
│   │   └── views.xml
│   ├── common
│   │   ├── footer.html
│   │   ├── header.html
│   │   └── layout.html
│   ├── conditions
│   │   ├── cgv.html
│   │   └── views.xml
│   ├── curriculum
│   │   ├── edit.html
│   │   ├── main.html
│   │   ├── new.html
│   │   ├── trainings.html
│   │   ├── views.xml
│   │   └── work-experiences.html
│   ├── errors
│   │   ├── 403.html
│   │   ├── 404.html
│   │   ├── error.html
│   │   └── views.xml
│   ├── messages
│   │   ├── body.html
│   │   ├── messages.html
│   │   └── views.xml
│   ├── passwordReset
│   │   ├── passwordReset.html
│   │   ├── resetPassword.html
│   │   ├── sendPasswordResetInfo.html
│   │   └── views.xml
│   ├── preference
│   │   ├── address.html
│   │   ├── email.html
│   │   ├── password.html
│   │   ├── preferenceMenu.html
│   │   └── views.xml
│   ├── search
│   │   ├── advertisement.html
│   │   ├── body.html
│   │   ├── childminderAdvertisementSearchForm.html
│   │   ├── childminderAdvertisementSearchResults.html
│   │   ├── familyAdvertisementSearchForm.html
│   │   ├── familyAdvertisementSearchResults.html
│   │   ├── view.html
│   │   └── views.xml
│   ├── signin
│   │   ├── signin.html
│   │   ├── standardSignin.html
│   │   └── views.xml
│   └── signup
│   ├── signup.html
│   ├── standardSignup.html
│   └── views.xml
└── web.xml
23 directories, 92 files
Can I specify public: '../src/main/webapp' as a path in config.coffee and safely use brunch without fear that it will delete content from src/main/webapp?
yes
Can I leave my Thymeleaf templates in WEB-INF/web-templates?
yes
Is there any other points to take into account?
I would suggest making "webapp" 100% auto-generated so that you could delete it freely. Also, to not keep generated stuff in repository.
This can be accomplished by moving all your stuff to app/assets directory, for example. Things from assets directories are copied as-is to public directory. But it's up to you.

Resources