1

I have a linux script job that is made up of 7 scripts in total to complete the job. There are about 60 Variables defined at the start of each script. These variables will be constant & identical in each of the 7 scrips.

The problem I have is that when I am working on the scripts, I have to copy paste all variables to all scripts each time I update or modify variable(s)

Is there a way that I can define all the variables in a file "variables.txt" & somehow reference all these variables from variables.txt at the start of each script?

I was thinking of using sed, but there is probably an easier & cleaner way..

linuxnoob
  • 675
  • 2
  • 6
  • 17
  • Yes you can. Put the line `. variables.txt` just below `#!/bin/bash` in each script and you're done. Make sure they are all in the same directory. – thom Dec 04 '13 at 04:11

2 Answers2

1

Create a file with the constants, and use source in each script:

#!/bin/bash
source `dirname $0`/variables.txt

(The dirname $0 part is to ensure that no matter how the script is called, it looks for variables.txt in the same directory as the script.)

Jack
  • 5,801
  • 1
  • 15
  • 20
  • I tried that but scripts are not working as they should. Some of the scripts are copied to a temp-working folder to be executed, could that be the problem – linuxnoob Dec 04 '13 at 02:56
  • As long as `variables.txt` is in your path, `source variables.txt` should find it. – chepner Dec 04 '13 at 03:08
  • Well, can you have the process that copies those scripts also copy varaibles.txt to the same place? If variables.txt is always going to be in a known place, you can just use the whole path: source /path/to/my/variables/file/variables.txt – Jack Dec 05 '13 at 13:40
0

Maybe if you set global variables, all the scripts will recognise it:

#!/bin/bash
FOO=bar
export FOO

Define Global Variables in Bash

Community
  • 1
  • 1
ismatim
  • 166
  • 3
  • 13