【问题标题】:How to check if it is a directory/file in linux shell如何检查它是否是linux shell中的目录/文件
【发布时间】:2014-09-21 03:29:42
【问题描述】:

我正在 linux shell 中制作一个带有快速菜单的程序。

一个选项是编辑或更改目录。

我需要看看它是目录还是文件,因为我需要使用vim作为文件,使用cd作为目录。

感谢您的阅读/帮助

【问题讨论】:

  • 你检查man test

标签: file shell directory


【解决方案1】:
echo "Input a file: "
IFS= read -r file

if test -f "$file"
then
  echo "$file is a regular file"
elif test -d "$file"
  echo "$file is a directory"
else
  echo "It's something else, like a socket, fifo or device."
fi

另外,如果您 cd 但在脚本退出后仍发现自己位于同一目录中,请参阅 this post

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2015-11-11
    • 2014-02-18
    • 2013-12-12
    • 2015-07-15
    • 2014-10-03
    • 2010-09-10
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多