0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェル作成(個人メモ)

0
Posted at

作ったシェルスクリプトはこれ↓

sample.sh
# !/bin/sh

# 作業ディレクトリ
BASE_DIR=/home/batchuser
# カレントディレクトリを移動
cd $BASE_DIR

# (1) 処理対象ファイルを決める
# 処理対象ファイル名:FYYY_YYYYMM.zip
# ・FYYY:年度(例:2016, 2017...など)
# ・YYYYMM:年月(その年月までを含むデータ)
# ・処理対象:バッチ実行日の前月データ
#  例)2017/04/14に実行→FYYY_201703.zipを処理する
# (1-1) 前月を求める必要があるので当月1日を求める
this_month=`date +'%Y/%m/01'`
# (1-2) 検索ファイル名:*_YYYYMM.zip(YYYYMMは前月)
file_name_part=*`date -d "$this_month 1 month ago" +'%Y%m'`.zip #(※1)

# (2) 処理対象ファイルをbackupに移動する
# バックアップディレクトリ名:/home/batchuser/backup/YYYYMMDD
# ・YYYYMMDD:バッチ処理日
# (2-1) 本日を求める(YYYYMMDD)
today=`date +'%Y%m%d'`
# (2-2) バックアップディレクトリ名を決定
backup_today_dir=./backup/$today
# (2-3) 未作成ならディレクトリを作成する
if [ -e $backup_today_dir ]; then #(※2)
        : #(※3)
else
        mkdir $backup_today_dir
fi
# (2-4) 対象ファイルをbackupにすべて移動する
ls -1 ./$file_name_part | while read filename #(※4)
do
        mv $filename ${backup_today_dir}/
done

(※1): 一ヶ月前を求めるにはまず当月1日を求めなければならない。
"2017/03/30 1 month ago"とすると2017/03/01になってしまう(仕様)。
(※2): ファイル(ディレクトリ)の存在チェック
→ if [ -e ${path} ]; then : else : fi
(※3): bashで何も処理をしない場合は :
(※4): ファイルリストを複数行に出して、複数行を繰り返し処理する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?