gala gala - 1 month ago 8
Linux Question

shell scripting - file existence

I want to ask a question on shell scripting
Suppose there are two paths on which a file may be installed

Path_1
or
Path_2


The script first should look for the file on
Path_1

if the file is not present or installed on
Path_1

then the script should look for the file on
path_2

if its not there too then show the error

Answer

Here's simple sample code you can deploy, and be sure to modify the values of 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