Joel Joel - 4 months ago 11
Linux Question

bash: command works in terminal, not in script

I am doing

a="$(openssl x509 -in /path/to/pemfile.pem -text -noout)";
echo ${a} |grep -a1 -b2 Signature


which works quite well if i put this line into terminal

However, if i put the very same line in a file
executeme.sh
,
chmod +x executeme.sh
,
./executeme.sh


I seems does not create the linebreaks in the variable, resulting grep to receive just one line. output is as follows on the terminal:

20- Version: 3 (0x2)
38- Serial Number: 32 (0x27)
64: Signature Algorithm: md5WithRSAEncryption
107- Issuer: C=EN, ST=a, L=b, O=c, OU=d, CN=e
244- Validity
------
[...]


The script outputs the entire certificate , as if i would only do
a="$(openssl ...)"; echo ${a}

Answer

Do

echo "${a}" |grep -a1 -b2 Signature #mind the double quotes

Why doublequotes?

See [ this ] answer + [ this ] answer.