bash - 如果文件存在于给定路径中则显示文件

标签 bash shell unix

我是 Unix shell 脚本的新手。

我创建了一个文件,该文件将一个参数作为文件名以在文件中的给定路径中查找。如果找到文件,则它应该显示该文件或显示相应的消息。

文件:FilePath.sh

#!/bin/sh

Given_Path=/user/document/workplace/day1

File_Name=$Given_Path/$1

# Here i got stuck to write the if condition,
# to check for the file is present in the given 
# path or not.
if [---------] #Unable to write condition for this problem.
then
    echo $1
else
    echo "File not found"
fi

Run:运行文件

$ bash FilePath.sh File1.txt

最佳答案

#!/bin/sh

Given_Path=/user/document/workplace/day1

File_Name=$Given_Path/$1

#Check if file is present or not.

if [ -e "$File_Name" ] 
then
    echo $1
else
    echo "File not found"
fi

一些一般条件:

-b file = True if the file exists and is block special file. 
-c file = True if the file exists and is character special file. 
-d file = True if the file exists and is a directory. 
-e file = True if the file exists. 
-f file = True if the file exists and is a regular file 
-g file = True if the file exists and the set-group-id bit is set. 
-k file = True if the files' "sticky" bit is set. 
-L file = True if the file exists and is a symbolic link. 
-p file = True if the file exists and is a named pipe. 
-r file = True if the file exists and is readable. 
-s file = True if the file exists and its size is greater than zero. 
-s file = True if the file exists and is a socket. 
-t fd = True if the file descriptor is opened on a terminal. 
-u file = True if the file exists and its set-user-id bit is set. 
-w file = True if the file exists and is writable. 
-x file = True if the file exists and is executable. 
-O file = True if the file exists and is owned by the effective user id. 
-G file = True if the file exists and is owned by the effective group id. 
-n $var = Uninitialized variable is true.

关于bash - 如果文件存在于给定路径中则显示文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37547440/

相关文章:

在脚本中运行时 mysqldump 不会转储所有内容

linux - 如何使用 cut 和 awk 命令以表格格式提取文本输入?

ruby - 如何从 Ruby 调用 shell 命令

bash shell 嵌套 for 循环

python - 使用 python 压缩所有子目录

bash - 为什么 git diff-tree 的输出在我的 post-receive hook 中是空的?

Git 提交 Hook - 如何使用 commit-msg Hook 检查消息中的字符串?

bash - Bash vi 输入模式中的语法高亮显示

linux - 如何获取unix数据文件中每行的前n个字符

C std 库似乎没有链接到目标文件中