我只是在查询(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


我已经在外壳上测试了
在#mysql下
它有效(当然没有$ variable)

我尝试将其放在sh文件中,因为我想对其进行crontab并使用$ date变量

最佳答案

看起来在INSERT INTO子句中有反引号(`)而不是规则的撇号('-单引号)。这些反驳是说“执行此命令”的一种猛烈方式。

只需用单引号替换反引号,就可以了。

关于mysql - 处理手动查询,但不适用于bash sh文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32899922/

10-11 02:16