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.

bash > fileIO > ファイル存在のチェック (existのe) | regular fileの定義 | testの-hと-Lはsymbolic linkの元ファイルをチェックする

Last updated at Posted at 2018-03-20

FeeBSD2.2.6?からの練達である(話半分)7of9であるが、「bashのファイル存在チェックはどう書くのだっけ?」という日々を過ごす。

man bashでCONDITIONAL EXPRESSIONSにて様々な選択肢がある。

       -a file
              True if file exists.
       -b file
              True if file exists and is a block special file.
       -c file
              True if file exists and is a character special file.
       -d file
              True if file exists and is a directory.
       -e file
              True if file exists.
       -f file
              True if file exists and is a regular file.
       -g file
              True if file exists and is set-group-id.

(以下略)

記憶の宮殿にコマンドオプションを格納する案として、「existの-e」と覚えればいいだろう (こじつけ)。

一方で、-fオプションを使う例もある。

True if file exists and is a regular file.

regular fileとは何か?

answered Apr 18 '12 at 7:10
jman

A regular file is something that isn't a directory / symlink / socket / device, etc.

つまり、symbolic linkしたファイルはregular fileでないため、-f testは失敗する、と推測した。

man bashにはregular fileの定義はない。

と思ったが、以下を見つけた。

Strange, on Ubuntu 14.04, using -f, it evaluates to true even for symlinks... – Garrett May 15 '15 at 18:28

CentOS 6.9

動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
GeForce GT 730 1GB GDDR5
CentOS 6.9 (64bit)
NCAR Command Language Version 6.3.0
for WRF3.7.1, WPS3.7.1
  openmpi-1.8.x86_64 とその-devel
  mpich.x86_64 3.1-5.el6とその-devel
  gcc version 4.4.7 (とgfortran)
for WRF3.9, WPS3.9
  Open MPI v2.1.1
  gcc version 4.9.2 (とgfortran; devtoolset-3使用)
 NetCDF v4.4.1.1, NetCDF (Fortran API) v4.4.4
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv
GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)
date (GNU coreutils) 8.4 
tmux 1.6-3.el6
check_file_exist_180320_exec
# !/usr/bin/env bash

set -eu  # just in case 

if [ -e LN-file ]
then
	echo "file exists [-e]"
fi

if [ -f LN-file ]
then
	echo "file exists [-f]"
fi
$ touch afile
$ ln -fs afile LN-file
$ bash check_file_exist_180320_exec 
file exists [-e]
file exists [-f]

CentOSでは-fのregular fileの結果でsymlinkは含まれるようだ。

Stackoverflow

質問しておいた。
https://stackoverflow.com/questions/49380068/what-is-the-regular-file-on-centos-and-ubuntu

nos氏による回答によると、-h-Lオプションではsymbolic linkはなく、その元ファイルのチェックになるようだ。

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?