Z
Z
Zueuk2017-08-10 21:14:47
Oracle
Zueuk, 2017-08-10 21:14:47

How to escape the $ character and other characters in a bash script?

There is a bash script with an sql query to the Oracle database. When executed, an error is thrown, which is true, since there are service characters in the request. How to shield them correctly?

#!/bin/bash

HOST=db.local
MAILHEADER="Content-Type: text/html; charset=UTF-8; Importance: high X-Priority: 1 (Highest) X-MSMail-Priority: High"
MAILBODY="Хост: <b>$HOST</b><br>
Запрос: <b>пользователи</b><br>
Дата: <b>$(date)</b><br>
<br>"

SQL=`
sqlplus -s system/[email protected] << EOF
set linesize 3800
set pagesize 60
select distinct action from v$session where action like '%FRM%' order by action;
/
EOF
`
SQLOUTPUT="<pre>$SQL</pre>"
echo "$MAILBODY $SQLOUTPUT" | mail -s "$(hostname)" -a "$MAILHEADER" [email protected]

PS To the right question: why so, if it's easier to make a call to an sql query from a separate file? I will answer - in order not to produce a bunch of files and the call came from one place.
Clarification.
The highlight of this part of the script. v$session is not a bash script variable, but a database service table.
SQL=`
sqlplus -s system/[email protected] << EOF
set linesize 3800
set pagesize 60
select distinct action from v$session where action like '%FRM%' order by action;
/
EOF
`

Addendum 2.
It turned out in this version:
SQL=$(
sqlplus -s system/[email protected] << EOF
set linesize 3800
set pagesize 60
select distinct action from v\$session where action like '%FRM%' order by action;
/
EOF
)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2017-08-10
@Zueuk

\$

S
sim3x, 2017-08-10
@sim3x

https://toster.ru/answer?answer_id=758099#answers_...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question