0

I have three bash scripts. w.txt, t.txt and d.txt.

w.txt:

#!/bin/bash

wtimes=( 8:20 9:30 10:11 )
wtimef=( 10:10 11:20 13:30 )

echo $wtimes >> d.txt

t.txt

#!/bin/bash

. ./w.txt

echo "${wtimes[1]}"
echo "${wtimef[1]}"

I am trying to run t.txt so I can work with the array variables $wtimes and $wtimef in t.txt without running echo $wtimes >> d.txt. This is very simplyfied and there are actually many more commands in w.txt that I do not want executed from within t.txt. How can I be selective here? Can someone please help?

2
  • Is modifying the code (e.g. adding some logic) allowed? Commented Jul 28, 2020 at 5:10
  • I don't see why not. Commented Jul 28, 2020 at 6:12

1 Answer 1

2

A simple way would be to let the imported file know that it is imported, for example:

#!/bin/bash

wtimes=( 8:20 9:30 10:11 )
wtimef=( 10:10 11:20 13:30 )

if [ "${imported}" = "yes" ] ; then
        exit;
fi

echo $wtimes >> d.txt

and

#!/bin/bash

imported=yes

. ./w.txt

echo "${wtimes[1]}"
echo "${wtimef[1]}"

But you may want to rethink your strategy here. If a common set of variables is used by different scripts, it may be a good idea to put those variables in a separate file. For example:

vars.sh:

wtimes=( 8:20 9:30 10:11 )
wtimef=( 10:10 11:20 13:30 )

w.sh:

. ./vars.sh
echo $wtimes >> d.txt

t.sh:

. ./vars.sh

echo "${wtimes[1]}"
echo "${wtimef[1]}"
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.