# conditional loading

**URL:** <https://community.zeek.org/t/conditional-loading/3924>\
**Category:** Zeek\
**Created:** [December 1, 2015, 12:21am UTC](https://community.zeek.org/t/conditional-loading/3924 "2015-12-01T00:21:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dk\_Jack](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@Dk\_Jack](https://community.zeek.org/u/Dk_Jack)\
**Post date:** [December 1, 2015, 12:21am UTC](https://community.zeek.org/t/conditional-loading/3924/1 "2015-12-01T00:21:16Z")

</div>

Hi,  
Is it possible to perform conditional loading of bro script files ?  
In my local.bro file, I’d like to do something like this:

if file ‘filters.bro’ exists  
{  
@load filters.bro  
}

The file filters.bro may or may not exist because it’s getting generated from an external program.  
Thanks for your help in advance.

Dnj

---

<div class="post-metadata">

**Author:** ![anthony\_kasza1](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@anthony\_kasza1](https://community.zeek.org/u/anthony_kasza1)\
**Post date:** [December 1, 2015, 12:41am UTC](https://community.zeek.org/t/conditional-loading/3924/2 "2015-12-01T00:41:42Z")

</div>

See here:  
[https://www.bro.org/sphinx/script-reference/directives.html](https://www.bro.org/sphinx/script-reference/directives.html)

-AK

---

<div class="post-metadata">

**Author:** ![Dk\_Jack](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@Dk\_Jack](https://community.zeek.org/u/Dk_Jack)\
**Post date:** [December 1, 2015, 2:43am UTC](https://community.zeek.org/t/conditional-loading/3924/3 "2015-12-01T02:43:49Z")

</div>

the directives don’t seem to have file test operators. I tried to do the following…

local c = “test -f filters.bro”;  
local cmd = Exec::Command($cmd=c);

when (local res = Exec::run(cmd))  
{  
@if (res$exit\_code == 0)  
{  
@load filters.bro;  
}  
}

However, I am getting parsing errors…

---

<div class="post-metadata">

**Author:** ![anthony\_kasza1](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@anthony\_kasza1](https://community.zeek.org/u/anthony_kasza1)\
**Post date:** [December 1, 2015, 3:16am UTC](https://community.zeek.org/t/conditional-loading/3924/4 "2015-12-01T03:16:33Z")

</div>

I have no clue if it would work but… You could try to make a global variable in the script you’re testing for (something like global is\_loaded: bool) then check if it is defined with @ifdef. If so, the script was loaded.

-AK

---

<div class="post-metadata">

**Author:** ![Grant\_Stavely](https://avatars.discourse-cdn.com/v4/letter/g/d78d45/32.png) [@Grant\_Stavely](https://community.zeek.org/u/Grant_Stavely)\
**Post date:** [December 1, 2015, 3:42am UTC](https://community.zeek.org/t/conditional-loading/3924/5 "2015-12-01T03:42:17Z")

</div>

Innelegant but I just have the same process that creates populated files create empty ones for bro to @load in environments where they aren’t applicable and e.g. a puppet template would be overkill.

Grant

---

<div class="post-metadata">

**Author:** ![Dk\_Jack](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@Dk\_Jack](https://community.zeek.org/u/Dk_Jack)\
**Post date:** [December 1, 2015, 5:00am UTC](https://community.zeek.org/t/conditional-loading/3924/6 "2015-12-01T05:00:11Z")

</div>

Thanks Athiny, Grant,  
I’ll try your suggestions.

---

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [December 2, 2015, 5:27am UTC](https://community.zeek.org/t/conditional-loading/3924/7 "2015-12-02T05:27:12Z")

</div>

You already got the correct advice in this thread, but just to close it out I’ll go into a bit more detail on why it couldn’t work.

Since “@if” is a parser directive, that code is executed while the code is being parsed but the code within a “when” block is executed asynchronously. There is higher-order problem that I’ll get to next, but conceptually that @if wouldn’t work anymore since it wouldn’t be executed until later when the when statement’s body executes.

The high level problem is that it looks like you have a lot of code outside of event handlers. There isn’t much support for code outside of event handlers in Bro since that means the code would only be executed at parse time which is frequently not a useful time to accomplish things. If you want to do something at startup you would work in a bro\_init event handler (which still wouldn’t work for what you want to do unfortunately).

I would follow Anthony’s advice and do a condition in a script that just changes behavior based on the result of the command that gets executed. On the upside, this give you the flexibility to re-run the command later and have behavior change dynamically.

.Seth

---

<div class="post-metadata">

**Author:** ![Dk\_Jack](https://avatars.discourse-cdn.com/v4/letter/d/8c91f0/32.png) [@Dk\_Jack](https://community.zeek.org/u/Dk_Jack)\
**Post date:** [December 3, 2015, 5:16am UTC](https://community.zeek.org/t/conditional-loading/3924/8 "2015-12-03T05:16:21Z")

</div>

Thanks Seth.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:43pm UTC](https://community.zeek.org/t/conditional-loading/3924/9 "2022-05-06T15:43:15Z")

</div>


