Sample Header Ad - 728x90

If-else Parameter substitution

0 votes
4 answers
455 views
I'm running a series of experiments with bash and want to store the log files in a directory whose name is based on the experiment configuration. Some items in the configuration are boolean (true/false). Take the following configuration as an example:
batch_size=16
fp16=false
bf16=true
checkpoint_activations=true
I'd like to store the log file from the experiment with above configuration as input in a directory with the following name:
output_dir="experiment_bs${batch_size}_dt${fp16 if fp16=true else bf16}_${cp if checkpoint_activations=true else empty}"
Of course, I could declare helper variables:
data_type=""
"${fp16}" && data_type=fp16
"${bf16}" && data_type=bf16
"${cp}" && cp="_cp" || cp=""
output_dir="experiment_bs${batch_size}_dt${data_type}${cp}"
But I find this somewhat clunky and hope that [parameter substitutions](https://tldp.org/LDP/abs/html/parameter-substitution.html#PARAMSUBREF) could be useful here. "${bf16:+bf16}" won't help in my case because this will always print "bf16" regardless of its boolean value as long as it's defined. Are there any parameter substitutions that could be applied to this use-case? Or is there an even better in-line solution to this problem? Note: there's an application-specific reason why I don't directly use data_type in my configuration.
Asked by Green 绿色 (331 rep)
Feb 6, 2024, 01:36 AM
Last activity: Jan 24, 2025, 08:25 PM