shell脚本 - 文件存在

时间:2016-10-30 08:25:01

标签: linux shell terminal

我想问一个关于shell脚本的问题 假设有两个路径可以安装文件 Path_1Path_2

首先应该在Path_1上查找该文件 如果文件不存在或安装在Path_1上 那么脚本应该在path_2上查找文件 如果它不存在那么显示错误

我有这个 代码: - # Make sure that sh.exe is found in C:\Program Files (x86)\Git\bin\sh.exe ls /c/Program\ Files\ \(x86\)/Git/bin/sh.exe > /dev/null 2>&1 || { echo echo -e "\t Git Bash was not installed in the default location." echo -e '\t Installation requires sh.exe to be found at C:\Program Files (x86)\Git\\bin\sh.exe.' echo echo -e "\e[0;31mAborting...\e[m" waitForEnterAndExit } echo -e "\t Git Bash installation found in C:\Program Files (x86)/Git/bin/sh.exe"

现在可以在

安装sh.exe

C:\ Program Files \ Git \ bin

 C:\Program Files (x86)\Git\bin\sh.exe

现在我想更改脚本,以便首先在

检查文件

C:\ Program Files \ Git \ bin

如果文件不在那里,那么

 C:\Program Files (x86)\Git\bin\sh.exe

1 个答案:

答案 0 :(得分:0)

以下是您可以部署的简单示例代码,并确保修改Path_1&的值。 PATH_2。

#!/bin/bash                                                                                                                                                                                                  

Path_1="path1"
Path_2="path2"

Filename=$1

[ -z ${Filename} ] && echo Filename cannot be empty && exit -1

[ -e ${Path_1}/${Filename} ] && echo Found ${Filename} under ${Path_1} && exit 0
[ -e ${Path_2}/${Filename} ] && echo Found ${Filename} under ${Path_2} && exit 0

echo ${Filename} cannot be found under ${Path_1} and ${Path_2} && exit -1