work on manual query but not work at bash sh file

2019-09-12 05:04发布

i just test this on query ( sysyoq ) it work but when i put it on sh file error

#!/bin/bash

lalu=$(date -d"1 month ago" "+%Y-%m-%d")
skrg=$(date '+%Y%m')
datenowone=$(date '+%Y-%m-01')
datenowtoday=$(date '+%Y-%m-%d')

set `date +%m" "%Y`
CURMTH=$1
CURYR=$2

if [ $CURMTH -eq 1 ]
then PRVMTH=12
     PRVYR=`expr $CURYR - 1`
else PRVMTH=`expr $CURMTH - 1`
     PRVYR=$CURYR
fi

if [ $PRVMTH -lt 10 ]
then PRVMTH="0"$PRVMTH
fi


mysql -h10.251.38.76 -umcd_metro -pBismillah2013 testarea -e "TRUNCATE TABLE testarea.user_trx_mtd;
INSERT INTO testarea.user_trx_mtd (`regional`, `branch`, `subBranch`, `cluster`, `user_type`, `user_id`, `name`)
SELECT a.regional, a.branch, a.subranch, a.cluster, a.`type_id`, a.user_id, a.name
FROM `${skrg}_central_jabotabek_ds_report_user` a
;


TRUNCATE TABLE `m1_sales`;
INSERT INTO `m1_sales`
SELECT a.user_id, a.event_id, COUNT(a.msisdn)
FROM `$PRVYR$PRVMTH_central_jabotabek_ds_trx_aktivasi` a
WHERE a.`trx_date` >= '$PRVYR-$PRVMTH-01' AND a.`trx_date` <= '${lalu}'
GROUP BY a.user_id, a.event_id
;

TRUNCATE TABLE `m1_rech`;
INSERT INTO `m1_rech`
SELECT a.user_id, a.event_id, SUM(a.nominal)*1000
FROM `$PRVYR$PRVMTH_central_jabotabek_ds_trx_isipulsa` a
WHERE a.`trx_date` >= '$PRVYR-$PRVMTH-01' AND a.`trx_date` <= '${lalu}' AND a.jenis = 'REGULER'
GROUP BY a.user_id, a.event_id
;

TRUNCATE TABLE `m1_rech_data`;
INSERT INTO `m1_rech_data`
SELECT a.user_id, a.event_id, SUM(a.nominal)*1000
FROM `$PRVYR$PRVMTH_central_jabotabek_ds_trx_isipulsa` a
WHERE a.`trx_date` >= '$PRVYR-$PRVMTH-01' AND a.`trx_date` <= '${lalu}' AND a.jenis = 'DATA'
GROUP BY a.user_id, a.event_id
;

TRUNCATE TABLE `m1_fitur`;
INSERT INTO `m1_fitur`
SELECT a.user_id, a.event_id, SUM(a.harga)
FROM `$PRVYR$PRVMTH_central_jabotabek_ds_trx_fitur` a
WHERE a.`trx_date` >= '$PRVYR-$PRVMTH-01' AND a.`trx_date` <= '${lalu}'
GROUP BY a.user_id, a.event_id
;

TRUNCATE TABLE `m_sales`;
INSERT INTO `m_sales`
SELECT a.user_id, a.event_id, COUNT(a.msisdn)
FROM `${skrg}_central_jabotabek_ds_trx_aktivasi` a
WHERE a.`trx_date` >= '$datenowone' AND a.`trx_date` <= '$datenowtoday'
GROUP BY a.user_id, a.event_id
;

TRUNCATE TABLE `m_rech`;
INSERT INTO `m_rech`
SELECT a.user_id, a.event_id, SUM(a.nominal)*1000
FROM `${skrg}_central_jabotabek_ds_trx_isipulsa` a
WHERE a.`trx_date` >= '$datenowone' AND a.`trx_date` <= '$datenowtoday' AND a.jenis = 'REGULER'
GROUP BY a.user_id, a.event_id
;

TRUNCATE TABLE `m_rech_data`;
INSERT INTO `m_rech_data`
SELECT a.user_id, a.event_id, SUM(a.nominal)*1000
FROM `${skrg}_central_jabotabek_ds_trx_isipulsa` a
WHERE a.`trx_date` >= '$datenowone' AND a.`trx_date` <= '$datenowtoday' AND a.jenis = 'DATA'
GROUP BY a.user_id, a.event_id
;

TRUNCATE TABLE `m_fitur`;
INSERT INTO `m_fitur`
SELECT a.user_id, a.event_id, SUM(a.harga)
FROM `${skrg}_central_jabotabek_ds_trx_fitur` a
WHERE a.`trx_date` >= '$datenowone' AND a.`trx_date` <= '$datenowtoday'
GROUP BY a.user_id, a.event_id
;

UPDATE `user_trx_mtd` a
INNER JOIN `m1_sales` b
ON a.`user_id` = b.`user_id`
SET a.`sales_m1` = b.`sales`
;

UPDATE `user_trx_mtd` a
INNER JOIN `m1_rech` b
ON a.`user_id` = b.`user_id`
SET a.`rech_m1` = b.`rech`
;

UPDATE `user_trx_mtd` a
INNER JOIN `m1_rech_data` b
ON a.`user_id` = b.`user_id`
SET a.`rech_data_m1` = b.`rech`
;

UPDATE `user_trx_mtd` a
INNER JOIN `m1_fitur` b
ON a.`user_id` = b.`user_id`
SET a.`rev_fitur_m1` = b.`rev`
;

UPDATE `user_trx_mtd` a
INNER JOIN `m_sales` b
ON a.`user_id` = b.`user_id`
SET a.`sales_m` = b.`sales`
;

UPDATE `user_trx_mtd` a
INNER JOIN `m_rech` b
ON a.`user_id` = b.`user_id`
SET a.`rech_m` = b.`rech`
;

UPDATE `user_trx_mtd` a
INNER JOIN `m_rech_data` b
ON a.`user_id` = b.`user_id`
SET a.`rech_data_m` = b.`rech`
;

UPDATE `user_trx_mtd` a
INNER JOIN `m_fitur` b
ON a.`user_id` = b.`user_id`
SET a.`rev_fitur_m` = b.`rev`
;

UPDATE `user_trx_mtd` a
SET a.`update_date` = '$datenowtoday'
;"

when i run the sh script

[masterroot@locate2 mrzfolder]$ ./RIP-TEST.sh
./RIP-TEST.sh: line 24: regional: command not found
./RIP-TEST.sh: line 24: branch: command not found
./RIP-TEST.sh: line 24: subBranch: command not found
./RIP-TEST.sh: line 24: cluster: command not found
./RIP-TEST.sh: line 24: user_type: command not found
./RIP-TEST.sh: line 24: user_id: command not found
./RIP-TEST.sh: line 24: name: command not found
./RIP-TEST.sh: line 24: type_id: command not found
./RIP-TEST.sh: line 24: 201510_central_jabotabek_ds_report_user: command not found
./RIP-TEST.sh: line 24: m1_sales: command not found
./RIP-TEST.sh: line 24: m1_sales: command not found
./RIP-TEST.sh: line 24: 2015: command not found
./RIP-TEST.sh: line 24: trx_date: command not found

i already test it on shell under #mysql it works ( of course withouot $variable )

i try to put it on sh file because i want to crontab it and use $date variable

标签: mysql sql bash sh
1条回答
趁早两清
2楼-- · 2019-09-12 05:55

Looks like you have back-ticks (`) instead of regular apostrophes (' - single-quotes) in your INSERT INTO clause. Those back-ticks are the bash way of saying "execute this command".

Just replace the back-ticks with single-quotes and you should be fine.

查看更多
登录 后发表回答