wethu
4/9/2015 - 2:54 AM

test.sh

#!/bin/bash

# Fetch all the daily lists of ID that are unfulfilled
# Usage: RAILS_ENV=production ID=28774 ./daily_list
# Exports to: APP_DIR/script/csv_tmp/daily_list-TIMESTAMP.csv
# Author: Ellis Gray 2015

RAILS_ENV=${RAILS_ENV:-development}
DATE=$(date +"%Y%m%d%H%M")
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

get_daily_list() {
echo "Dumping data from: $RAILS_ENV database."
  mysql -uroot <<EOF
    SELECT 'order', 'request', 'sku_id'
    UNION ALL
    SELECT
      li.item_collection_id as order_number,
      CONCAT(li.qty, ' x ', li.name) as request,
      li.sku_id
      INTO OUTFILE '$DIR/csv_tmp/daily_list-$DATE.csv'
      FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
      LINES TERMINATED BY '\n'
    FROM
      g2_$RAILS_ENV.production_requests as pr
    INNER JOIN g2_$RAILS_ENV.line_items as li on li.id = pr.line_item_id
    WHERE
        (pr.production_list_id = $ID)
      AND (pr.fulfilled_at IS NULL)
      AND ((pr.type = 'DailyListRequest'));
EOF
}
if [ -v ID ]; then
  get_daily_list;
  echo "Successfully exported to csv_tmp/daily_list-$DATE.csv";
else
  echo "You must set ID= to the unfulfilled id";
fi

# getting : ./daily_list.sh: line 33: [: -v: unary operator expected