0

I have some JobStreams which have some Jobs in it.

I intend to create a config file to be used generic for any Jobstream and another config file to be used by each Job.So I would have many job_config_files and only one generic_config_file

My question, is, can I create a config file as source by another? for example:

Job_config_file

export NAME_JOB='PP_AAAR'
export log_dir="/data/workdir/AAAR/aaar/logs/"
export cluster_logs_dir="/hadoop/cluster_logs/AAAR/"

test_general_config_file

source "/data/workdir/user/m292121/properties/test_job_config_file"

export TIMESTAMP=`date '+%Y%m%d_%H%M%S'`
export DT_INICIO=`date "+%Y-%m-%d %H:%M:%S"`
export HOST_NAME=`hostname -s`
export DIR=$(pwd)
export ULTIMO_ERRO='EXECU��O SEM ERROS'
export DATA_PROCESSAMENTO=`date '+%Y%m%d'`

export GF_NAME_JOB = $NAME_JOB

and I want to get the value of NAME_JOB from the Job_config_file and save it in the GF_NAME_JOB var in the general_config_file.

So, my sh script would be able to get the value of GF_NAME_JOB

sh_script

#!/bin/bash

source "/data/workdir/user/m292121/properties/test_general_config_file"   

echo $GF_NAME_JOB

and when I ran this, I got the error:

sh test_shell.sh
/data/workdir/user/m292121/properties/test_general_config_file: line 10: export: `=': not a valid identifier
gfernandes
  • 193
  • 1
  • 10
  • Config files in shell are really just shell scripts that you source. LIke any other shell script, the config file can itself contain a `source` command that executes the commands in *another* config file. – chepner Nov 08 '22 at 14:48
  • but, my config files aren't .sh files, are just files without extension...can I make reference still? – gfernandes Nov 08 '22 at 16:24
  • They are shell scripts; the extension is just conventional. – chepner Nov 08 '22 at 16:26
  • I'm going to edit my post to be more clear – gfernandes Nov 08 '22 at 16:50
  • (not just a convention, but a _bad_ convention; better to leave the `.sh` extensions off the names in the first place -- see essay @ [commandname extensions considered harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/)) – Charles Duffy Nov 08 '22 at 16:59
  • 2
    As for the problem here -- the spaces around the `=` in `export GF_NAME_JOB = $NAME_JOB` are illegal. Nothing to do with the `source` (though with `sh` instead of `bash`, you should use `. file` instead of `source file`). – Charles Duffy Nov 08 '22 at 17:00
  • 1
    `source "$` - what is the `$` doing here? Check your scripts with shellcheck – KamilCuk Nov 08 '22 at 17:02
  • 1
    thank you guys, it worked, the problem were the spaces as Charles pointed it out... – gfernandes Nov 08 '22 at 17:05
  • Charles, what do you mean by this "othing to do with the source (though with sh instead of bash, you should use . file instead of source file). "? – gfernandes Nov 08 '22 at 17:09
  • I meant exactly that. The command `source` is a bash extension. `sh` isn't guaranteed to support it. – Charles Duffy Nov 08 '22 at 17:29
  • See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_18 specifying the `.` command in the POSIX sh standard; that's the standardized version of what bash implements as `source`. – Charles Duffy Nov 08 '22 at 17:30
  • 1
    When you run `sh foo.sh`, you force `sh` to be used as the shell that runs that script, _completely ignoring_ any `#!/bin/bash` shebang the script may otherwise have. If you want your script to run with bash, you need to use `bash`, not `sh`. – Charles Duffy Nov 08 '22 at 17:32
  • ...if `source` works for you under `sh`, that implies that you're probably on a distro (like Red Hat) where `/bin/sh` is a symlink to bash; but in a lot of the world (Debian, Ubuntu, Alpine), `sh` is ash or dash instead, and hews much closer to only providing what the standard mandates. (Even where it's a symlink, bash engages POSIX compatibility mode when started under the `sh` name, but bash-in-POSIX-mode still has a lot of extensions that ash does not). – Charles Duffy Nov 08 '22 at 17:39
  • Anyhow, to concretely propose a duplicate: [Why does a space in a variable assignment give an error in bash?](https://stackoverflow.com/questions/41748466/why-does-a-space-in-a-variable-assignment-give-an-error-in-bash) – Charles Duffy Nov 08 '22 at 17:41

0 Answers0