Skip to main content
added 309 characters in body
Source Link

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

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

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

deleted 98 characters in body; added 1 character in body
Source Link
techraf
  • 6.1k
  • 11
  • 36
  • 51

I use a lot of external files in my scripts. But

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

example main.shExample main.sh:

#!/bin/bash

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

example config.shExample config.sh:

#!/bin/bash

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

exit 0

Problem is there is the multiline var3var3, so I want to use source for var 1 & 2var1 and var2. The following is possible but not nice:

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

thisThis is only work for single line not for var3var3.

Is there a simple / easy solution ?

update because imI'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 itsit's also not working for the my file on github

........ sorry
and code formatting in answer suxx i cant a simple linebreak working im to stupid

I use a lot of external files in my scripts. But I have the problem, that if I source a file with exit in the contents of the file, the script exit 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 there is the multiline var3, so I want to use source for var 1 & 2. 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 im 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 its also not working for the my file on github

........ sorry
and code formatting in answer suxx i cant a simple linebreak working im to stupid

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

added 72 characters in body
Source Link

I use a lot of external files in my scripts. But I have the problem, that if I source a file with exit in the contents of the file, the script exit 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 there is the multiline var3, so I want to use source for var 1 & 2. 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 im 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.shraw.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 its also not working for the my file on github

........ sorry
and code formatting in answer suxx i cant a simple linebreak working im to stupid

I use a lot of external files in my scripts. But I have the problem, that if I source a file with exit in the contents of the file, the script exit 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 there is the multiline var3, so I want to use source for var 1 & 2. 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 im 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

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 its also not working for the my file on github

........ sorry
and code formatting in answer suxx i cant a simple linebreak working im to stupid

I use a lot of external files in my scripts. But I have the problem, that if I source a file with exit in the contents of the file, the script exit 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 there is the multiline var3, so I want to use source for var 1 & 2. 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 im 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 its also not working for the my file on github

........ sorry
and code formatting in answer suxx i cant a simple linebreak working im to stupid

added 553 characters in body
Source Link
Loading
added 26 characters in body
Source Link
Anthon
  • 81.4k
  • 42
  • 174
  • 228
Loading
Source Link
Loading