1
2

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.

bash > Ctrl+cした時に二重ループの処理から抜ける > trap exit SIGINT

Last updated at Posted at 2017-03-27
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

bash script内で二重ループしている時、Ctrl+cで内部ループを終了時に、次の外部ループの処理をしないで欲しい場合がある。

trapを使うようだ。

参考 http://d.hatena.ne.jp/pyopyopyo/20140820/p1

以下のように実装した。

test_bash_170327_exec
# !/usr/bin/env bash

SCRIPT_DIR=$(dirname $0)

echo "data_arrival,hour_arrival,hour_start,total_value,avg_lat,avg_lon,std_lat,std_long"

trap exit SIGINT  # to exit for Ctrl+c

for orgfile in xxxouput*.nc;do
	#echo $orgfile
	ln -fs $orgfile LN-test_nc
	for timidx in $(seq 0 23);do
		python $SCRIPT_DIR/calc_latlon_avg_std_170323.py -t $timidx
	done
done

これで内部ループでCtrl+cした時にすぐに終了するようになった。


(追記 2017/03/28) @akinomyoga さんに編集リクエストいただきました。 感謝です。

編集リクエストのコメントが勉強になるため、以下に貼り付けしておきます。

(1) trap は事前に (C-c よりも前に) 動作を予約するもの。ループの前に1回実行すれば良い (2) ls xxxouput*.nc は無意味。単に xxxouput*.nc で十分。ls ... とすると却って空白類を含むファイル名で危険

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?