Error while checking existence of a file in shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Error while checking existence of a file in shell script



I wrote a script to check if a given file exists or not ( i am considering only regular files )
It displays opposite, that is, if file exists then "not found" and "file found" if file doesn't exists.
Have i messed up the if else ?
Please tell me how to rectify it.


#! /bin/bash
is_file_exists()
local file="$1"
if [[ -f "$file" ]]
then
return 1
else
return 0
fi


if [[ "$#" -eq 0 ]]
then
echo "enter a file name"
exit
fi

if ( is_file_exists "$1" )
then
echo "file found"
else
echo "not found"
fi





You have the return 0 and return 1 around the wrong way in your is_file_exists function, and you should use: if is_file_exists "$1", i.e. no parentheses.
– cdarke
Aug 6 at 14:05



return 0


return 1


is_file_exists


if is_file_exists "$1"





@aditya feel free to accept one of the answer below if you think that it answers your question
– Lino
Aug 6 at 19:48




3 Answers
3



Well, firstly, you're not checking for existence (-e); you're checking that it exists and that it's a regular file.


-e



Secondly, you're returning 0 (success) if it doesn't exist.



But since the return value of a function is that of the last command, there's no need for the if/else at all:


if


else


is_file_exists()
test -f "$1"



Re-written program (as portable shell, since it doesn't need anything Bash-specific):


#!/bin/sh

regular_file_exists()
test -f "$1"


# Is it used correctly?
if [ $# -ne 1 ]
then
echo "enter a file name" >&2
exit 1
fi

if regular_file_exists "$1"
then
echo "file found"
else
echo "not found"
fi





Is it the fact that the function is returning 0 when [[ -f file_name ]] evaluates to true ( that is the file exists ) and 1 otherwise?
– user8378799
Aug 6 at 14:13





Your function was the wrong way around - returning 1 (false) when the file exists, when you should return 0, and vice versa.
– Toby Speight
Aug 6 at 14:23



As my comment said, you have the return 0 and return 1 around the wrong way in your is_file_exists function, and you should use: if is_file_exists "$1", i.e. no parentheses.


return 0


return 1


is_file_exists


if is_file_exists "$1"



Shell if statements test success or failure of the given command. Success is defined as a returned value of zero. So maybe you had the returns around that way because you might have come from C, Java, or a similar language where zero is false. Think in terms of success or failure rather than true or false.


if



I have suggested a few other tweaks, including consistent indentation:


#! /bin/bash
is_file_exists()

local file="$1"

# [ (test) could have been used here
# Is the name given a regular file?
if [[ -f $file ]] # quotes not required with [[ (but are with [ )
then
return 0
else
return 1
fi


# Use (( )) for arithmetic comparisons
if (( $# == 0 ))
then
echo "enter a file name" >&2 # error messages should go to stderr
exit
fi

# Delimit filenames on display, shows unintended whitespace
if is_file_exists "$1"
then
echo "'$1' file found"
else
echo "'$1' not found"
fi



If you still want to keep the "inverse" logic of your is_file_exists() function then you can do the following:


is_file_exists()


#! /bin/bash
is_file_exists()
local file="$1"
if [[ -f "$file" ]]
then
return 1
else
return 0
fi


if [[ "$#" -eq 0 ]]
then
echo "enter a file name"
exit
fi

is_file_exists "$1"
retVal=$?

if [ $retVal -ne 0 ]; then
echo "file found"
else
echo "not found"
fi



This stores the return value of the function into the retVal variable and checks its value to provide the desired output.


retVal



Also, as suggested by @Toby, another solution would be (skipping the fuction):


if [[ "$#" -eq 0 ]]
then
echo "enter a file name"
exit
fi

if ! is_file_exists "$1"; then
echo "file found"
else
echo "not found"
fi






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS