MAK MAK - 6 months ago 6
Bash Question

Display file if its present in the given path

I am new to Unix shell scripting.

I have created file which take one parameter as a file name to find out in the given path in the file. If the file found then it should show the file or else show the appropriate message.

File:

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: Running file

$ bash FilePath.sh File1.txt

Answer
#!/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

Some general conditions:

-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. 
Comments