Bash Commands - File test operators

本文深入探讨了Linux中文件的权限、属性检查及其应用案例,通过具体脚本展示了如何验证文件是否存在、类型、链接及权限等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Returns true if...


-e             file exists

-a             file exists.     This is identical in effect to -e. It has been "deprecated," [34] and its use is discouraged.

-f              file is a regular file (not a directory or device file)
-s            file is not zero size
-d             file is a directory
-b             file is a block device
-c              file is a character device


device0="/dev/sda2"

# /
(root directory)
if [ -b "$device0" ]
then
echo "$device0 is a block device."
fi


# /dev/sda2 is a block device.

device1="/dev/ttyS1"
# PCMCIA modem card.
if [ -c "$device1" ]
then
echo "$device1 is a character device."
fi

# /dev/ttyS1 is a character device.


-p                      file is a pipe


function show_input_type()

{
[ -p /dev/fd/0 ] && echo PIPE || echo STDIN
}
show_input_type "Input"                             
# STDIN

echo "Input" | show_input_type                  # PIPE



-h                      file is a symbolic link

-L                         file is a symbolic link

-S                         file is a socket
-t                           file (descriptor) is associated with a terminal device
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a
given script is a terminal.
-r                           file has read permission (for the user running the test)
-w                       file has write permission (for the user running the test)
-x                         file has execute permission (for the user running the test)
-g                         set-group-id (sgid) flag set on file or directory

If a directory has the sgid flag set, then a file created within that directory belongs to the group that
owns the directory, not necessarily to the group of the user who created the file. This may be useful
for a directory shared by a workgroup.

-u                            set-user-id (suid) flag set on file

-k                            sticky bit set

-O                             you are owner of file
-G                             group-id of file same as yours
-N                             file modified since it was last read
f1 -nt f2                    file f1 is newer than f2
f1 -ot f2                    file f1 is older than f2
f1 -ef f2                    files f1 and f2 are hard links to the same file
!                                    "not" -- reverses the sense of the tests above (returns true if condition absent).





-- Found Protobuf: /usr/lib/aarch64-linux-gnu/libprotobuf.a;-lpthread (found version "3.0.0") Generated: /home/songzhiyi/onnx-rel-1.13.0/.setuptools-cmake-build/onnx/onnx-ml.proto Generated: /home/songzhiyi/onnx-rel-1.13.0/.setuptools-cmake-build/onnx/onnx-operators-ml.proto Generated: /home/songzhiyi/onnx-rel-1.13.0/.setuptools-cmake-build/onnx/onnx-data.proto -- Could NOT find pybind11 (missing: pybind11_DIR) CMake Error at CMakeLists.txt:463 (message): cannot find pybind -- Configuring incomplete, errors occurred! See also "/home/songzhiyi/onnx-rel-1.13.0/.setuptools-cmake-build/CMakeFiles/CMakeOutput.log". See also "/home/songzhiyi/onnx-rel-1.13.0/.setuptools-cmake-build/CMakeFiles/CMakeError.log". Traceback (most recent call last): File "setup.py", line 381, in <module> "backend-test-tools = onnx.backend.test.cmd_tools:main", File "/home/songzhiyi/.local/lib/python3.6/site-packages/setuptools/__init__.py", line 153, in setup return distutils.core.setup(**attrs) File "/usr/lib/python3.6/distutils/core.py", line 148, in setup dist.run_commands() File "/usr/lib/python3.6/distutils/dist.py", line 955, in run_commands self.run_command(cmd) File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command cmd_obj.run() File "/usr/lib/python3.6/distutils/command/build.py", line 135, in run self.run_command(cmd_name) File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command self.distribution.run_command(command) File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command cmd_obj.run() File "setup.py", line 233, in run self.run_command("cmake_build") File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command self.distribution.run_command(command) File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command cmd_obj.run() File "setup.py", line 219, in run subprocess.check_call(cmake_args) File "/usr/lib/python3.6/subprocess.py", line 311, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/usr/bin/cmake', '-DPYTHON_INCLUDE_DIR=/usr/include/python3.6m', '-DPYTHON_EXECUTABLE=/usr/bin/python3.6', '-DBUILD_ONNX_PYTHON=ON', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON', '-DONNX_NAMESPACE=onnx', '-DPY_EXT_SUFFIX=.cpython-36m-aarch64-linux-gnu.so', '-DCMAKE_BUILD_TYPE=Release', '-DONNX_ML=1', '/home/songzhiyi/onnx-rel-1.13.0']' returned non-zero exit status 1.
07-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值