Bash script에서 jquery 실행 오류
조회수 1176회
배쉬스크립트에 html 코드를 삽입하여 스크립트를 실행하면
html 파일이 만들어지는 간단한 코드입니다.
구글차트도 잘불러와지는데 jquery로 작성만 문구만 넣으면 아래와 같은 오류가 뜹니다.
[root@localhost 바탕화면]# bash total_ht.sh
total_ht.sh: line 162: document: command not found
total_ht.sh: line 162: .table tr td: command not found
total_ht.sh: line 162: v.parentElement: command not found
total_ht.sh: line 162: v.parentElement: command not found
삽입하려는 코드는 아래와 같습니다.
$(document).ready(function() {
$('.table tr td').each(function(i, v){
if(v.textContent === '양호') {
$(v.parentElement).addClass('table-success');
} else if(v.textContent === '취약') {
$(v.parentElement).addClass('table-danger');
}
})
})
아래코드는 jquey코드를 삽입하려는 bash script(total_ht.sh)입니다.
#!/bin/bash
present03=`ls -alL /etc/passwd`
if [ -f /etc/passwd ]
then
if [ `ls -alL /etc/passwd | awk '{print $1}' | grep ".rw-r--r--" | wc -l` -eq 1 ]
then
result03="양호"
gct=$((gct+1))
else
result03="취약"
bct=$((bct+1))
fi
fi
cat <<- _EOF_ > "test".html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['구분', '항목'],
['양호', $gct],
['취약', $bct]
]);
var options = {
legend: 'none',
pieSliceText: 'label',
title: 'test',
pieStartAngle: 100,
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="header">
</div>
<div class="container">
<div id="piechart" style="width: 900px; height: 500px;"></div>
<table data-row-style="rowStyle" class="table table-bordered table-hover thead-dark thead-inverse">
<thead>
<tr>
<th class="text-center">번호</th>
<th class="text-center">이름</th>
<th class="text-center">현황</th>
<th class="text-center">결과값</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-center">01</td>
<td class="text-center">$title01</td>
<td>$present01</td>
<td class="text-center">$result01</td>
</tr>
<tr>
<td class="text-center">02</td>
<td class="text-center">$title02</td>
<td>$present02</td>
<td class="text-center">$result02</td>
</tr>
</tboby>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</body>
</html>
_EOF_
왜 오류가 나타날까요?ㅠㅠ
-
(•́ ✖ •̀)
알 수 없는 사용자
2 답변
-
당연히 오류가 납니다.
원인은 $ 기호 때문입니다. 쉘스크립트에서 $ 기호는 매개변수 기호로 사용합니다.
쉽게 일반적인 문자가 아닌 예약된 기호(문법)라고 생각하면 됩니다.
우회하는 방법은 쉘스크립트에서 quoting 처리를 해서 $를 출력이 가능한 문자로 처리할 수 있습니다.
단순히 '' 로 묶어주면 됩니다.
자세한 내용은 아래 참고링크를 확인해보세요.
https://wiki.kldp.org/HOWTO/html/Adv-Bash-Scr-HOWTO/quoting.html
-
bash script가 $(
command
) 로 인식해서 그런거 같은데https://askubuntu.com/questions/97050/how-to-escape-and-symbols-in-bash
이 문답을 참고하시면 해결될거 같은데요?
댓글 입력