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
/tmp/xyz
(dropping any exit lines and then source the result?exit
statements? If not, could you refactor your scripts so they both source the definitions from a common file?