0

I have a configuration file where I am exporting variables that can enable and run certain functions in my bash shell script. I was wondering the best way to accomplish this. My thought was just to use a for loop and do a simple if else - not sure if there is a better way. Any suggestions?

Example exported variables in config (I don't have to do it like the following but this was what I was thinking):

data_error_check_run=TRUE
check_ctl_dat_exists_run=FALSE
check_ctl_dat_format_run=TRUE
ctl_dat_count_check_run=TRUE
mask_field_run=FALSE
custom_target_file_run=TRUE

Functions inside script to run:

data_error_check
check_ctl_dat_exists
check_ctl_dat_format
ctl_dat_count_check
mask_field $MASK_FILE
custom_target_file $CUSTOM_CONFIG
0

So you want to run the functions only when the corresponding variable is set to TRUE? I would do something like

[[ "$data_error_check_run" == TRUE ]]     && data_error_check
[[ "$check_ctl_dat_exists_run" == TRUE ]] && check_ctl_dat_exists
[[ "$mask_field_run" == TRUE ]]           && mask_field "$MASK_FILE"

[[ doesn't barf on unset variables so not setting a specific one is the same as setting it to FALSE.

  • @Kusalananda Just out of curiosity: for which values of the variables are the quotes really necessary here? – nohillside Mar 26 '18 at 18:33
  • 2
    At least $MASK_FILE. The others may not need quotes, but I added them out of habit. Please feel free to roll back the edit if you wish. – Kusalananda Mar 26 '18 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.