mktempの改善

mktempの改善

mktemp暗号化されたコンテナまたはファイルシステムの使用を最も効果的に改善する方法を知りたいです。

私が扱っている問題は、可能であれば、シェルスクリプトが作業ディレクトリを含むファイルシステムに一時ファイルを保存したいということです。

通常の動作mktempは環境変数か/tmp。ただし、暗号化されたコンテナ内のファイルを扱う場合は、一時データが暗号化されていない場所に漏洩することがよくあります。

tmpアイデアは、まず現在のファイルシステムのマウントポイントにディレクトリが存在することを確認し、/tmpそれを最後の手段としてのみ使用することです。これを安定的かつ効率的に達成するにはどうすればよいですか?

編集する

特定のパスのマウントディレクトリを特定する方法は次のとおりです。

dir=`realpath [path]`; 
res=1; 
while [ $res -ne 0 ]; do 
  dir="${dir%/*}"; 
  mountpoint -q "$dir/"; 
  res=$?; 
done; 
echo "$dir";

しかし、これが最も効率的かどうかはわかりません。

ベストアンサー1

mktemp-pオプションを使用するか、他のTMPDIRを設定できます。

-p temp-dir, --tmpdir=temp-dir
          temp  directory  for  the  file.  This option is a member of the
          tmpdir class of options.

          If this option is not provided, mktemp will use the  environment
          variable  TMPDIR to find a suitable directory.  If these are not
          available, it will fall back to ~/tmp  or  /tmp.   A  <file-pat>
          command line argument containing a directory component will con-
          flict with this option.

たとえば、

#!/bin/bash
TMPDIR=`pwd`
mktemp

おすすめ記事