Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'v written a script that sources a bash shell fragment. The shell fragment is supposed to be a kind of configuration file, basically it's a bunch of bash variable.

The issue here is when some of those string variable contain characters that should be escaped. As they aren't escape, the script might faill later on or have undesired behavior.

At the moment, my script is validating the shell fragment (the configuration file) bash syntax (bash -n) but if any charachter is to be escaped, that won't be detected.

So, I would like either to auto-escape sensitive character or, at least, detect their are some so I can display an error and exit.

Any idea how could I achieve that?

Thanks in advance,

share|improve this question
1  
Can you give us an example of such a case. –  Khaled Jan 7 '13 at 15:15

1 Answer 1

up vote 2 down vote accepted

The $? variable is your friend. It contains the return status of the most recently executed command, including bash built-in functions.

$ /bin/false
$ echo $?
1
$ /bin/true
$ echo $?
0
$ echo 'FOO="bar' > atest
$ . atest
-bash: atest: line 1: unexpected EOF while looking for matching `"'
-bash: atest: line 2: syntax error: unexpected end of file
$ echo $?
1

Keep in mind that it only returns the status of the most recent command. If you need to run an additional command before performing your error test, you need to immediately assign it to a variable like so:

$ /bin/false
$ MYRETURN=$?
$ /bin/true
$ echo $?
0
$ echo $MYRETURN
1
share|improve this answer
    
I'v implemented that check, thanks for your tip –  user152368 Jan 7 '13 at 15:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.