2

I use a lot of external files in my scripts.

But I have a problem, that if I source a file with exit in the contents of the file, the script exits from loading and the strings that I want to use are gone .

Example main.sh:

#!/bin/bash

clear;
source /opt/external-svn/config.sh;
echo "$var1 and $var2 and $var3";

Example config.sh:

#!/bin/bash

var1="foo bar is great"
var2="foooo baar is greater"
var3="foooooooo baaaaaar is \
to long"

exit 0

Problem is the multiline var3, so I want to use source for var1 and var2. The following is possible but not nice:

var1=$(cat "/opt/external-svn/config.sh" |awk -F'"' '/^var1/ {print $2}');

This is only work for single line not for var3.

Is there a simple / easy solution ?

update because I'm to stupid to make a correct answer

@Gilles no not working the file in raw is here for suggestions raw.githubusercontent.com/gorgone/temptest/master/config.sh

#!/bin/bash

FILE="/tmp/$(basename $0).$$.tmp"
configsh="/opt/external-svn/config.sh"
grep -vE '^exit' $configsh > $TFILE
source "$TFILE"

exit the mainscript and got echo to screen

@Anthon it's also not working for the my file on github

Update2 found solution

i found the magic ONE i use the idea but its not a generic solution

only for my special case

cat config.sh |sed 's/if [ $# = 0 ]/if [ $# = 1 ]/g' >config.source

so i can source the file

but the best way is to have a generic solution for all files

3
  • Any reason why can't just filter each of the files to /tmp/xyz (dropping any exit lines and then source the result?
    – Anthon
    Apr 18 '16 at 14:17
  • hmh not a bad idea i have also a fallback with wget to string there i can also sed all exits ... il try it ... I thought there is a cheat for source "skip ignore" or force read ... but if it works im happy Apr 18 '16 at 14:20
  • Is it possible to just remove the exit statements? If not, could you refactor your scripts so they both source the definitions from a common file? Apr 18 '16 at 16:34
2

If you have abc.inc:

x=1
exit 0

and klm.inc:

y=2
exit 0

then if you have combine script:

#!/bin/bash

for fn in abc klm; do
  grep -vE '^exit' $fn.inc > /tmp/xyz.tmp
  source /tmp/xyz.tmp
done

echo x: $x
echo y: $y

you can run combine and get as output:

x: 1
y: 2

This assumes that your exit statement are at the beginning of their lines, you might have to adjust the grep if they are not

4
  • If this solves your problem, please consider accepting this answer by clicking the ✔ (checkmark) next to the answer. That is the way other people know your problem has been solved. If a better answer comes along, you can always change the accepted answer.
    – Anthon
    Apr 18 '16 at 15:32
  • update first post Apr 18 '16 at 22:39
  • @GorgoneImpertinence I don't understand your comment
    – Anthon
    Apr 19 '16 at 8:08
  • update 2 in post 1 Apr 19 '16 at 8:54
4

You may override exit in your main.sh by declaring a custom exit function at the beginning of main.sh.

#!/bin/bash
# main.sh
exit() { 
   local exit_code="${1-?}"
   test "$exit_code" -ne 0 && builtin exit "$exit_code"
   :
}
clear;
source /opt/external-svn/config.sh;
echo "$var1 and $var2 and $var3";

#unset -f exit; exit 0
builtin exit 0
2
  • Perhaps you meant "$1" instead of "$?"...?
    – Wildcard
    Apr 18 '16 at 18:46
  • @Gilles update in first post code formatting in answer not working for my stupid brain Apr 18 '16 at 22:39

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.