处理手动查询但不能在bash sh文件中工作

时间:2015-10-02 03:00:10

标签: mysql sql bash sh

我只是在查询(sysyoq)上对它进行测试 但是当我把它放在sh文件上时 错误

#!/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'
;"

当我运行sh脚本

[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

我已经在shell上测试了它 在#mysql下 它的工作原理(当然是$ $变量)

我尝试把它放在sh文件上,因为我想crontab它并使用$ date变量

1 个答案:

答案 0 :(得分:0)

看起来你的INSERT INTO子句中有反向标记(`)而不是常规的撇号(' - 单引号)。那些反向标记是说“执行此命令”的bash方式。

只需用单引号替换后面的刻度,你应该没问题。