if and fi in UNIX

I am new to Unix. With the following code, I am trying to check if there is any existing file xxx.txt in the current directory and echo "error message" if not found in current directory . The whole script is erroring out, can someone help me

Here is my code
$ cat -n ifelseif_01.sh
     1  #!/bin/sh
     2  if [ -f find -name xxx.txt ] then
     3  echo "file found"
     4  echo "job successful"
     5  fi
     6  if [ $? != 0 ] then
     7  echo "error message"
     8  fi

Open in new window

d27m11yAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
   1  #!/bin/sh
     2  if [ -f find -name xxx.txt ]; then
     3  echo "file found"
     4  echo "job successful"
     5  fi
     6  if [ $? != 0 ];then
     7  echo "error message"
     8  fi
0
s___kCommented:
You can check if file exits with
if [ -f xxx.txt ] then
     echo "file found"
fi
0
farzanjCommented:
Sorry, I looked at if condition only.
1  #!/bin/sh
     2  if [ -f  xxx.txt ]; then
     3  echo "file found"
     4  echo "job successful"
     5  fi
     6  if [ $? != 0 ];then
     7  echo "error message"
     8  fi
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

farzanjCommented:
In if condition

Either use
if COND
then

fi

OR

if COND ; then
fi
0
omarfaridCommented:
if I understood your script correct then try this

#!/bin/sh
if [ -f find -name xxx.txt ]
then
   echo "file found"
   echo "job successful"
else
   echo "error message"
fi
0
d27m11yAuthor Commented:
can we have else condition included?
0
omarfaridCommented:
for else and condition you can use elif like

if [ -f find -name xxx.txt ]
then
   echo "file found"
   echo "job successful"
elif [ $? != 0 ]
then
   echo "error message"
fi

0
d27m11yAuthor Commented:
I have "sqlscript_01.sh" in my current directory and am trying to find it exists in current directory otherwise, I want to return message "file not found". I have the following script and that is returning me error message.
$ cat -n ifelseif_01.sh
     1  #!/bin/sh
     2
     3  if [ -f find -name sqlscript_01.sh ];
     4  then
     5  echo "file found"
     6  echo "job successful"
     7  else
     8  echo "file not found"
     9  fi

Open in new window

0
d27m11yAuthor Commented:

Please advise what is wrong in my script.

Here is error message I am getting -

$ ./ifelseif_01.sh
./ifelseif_01.sh: line 3: [: too many arguments
file not found
0
d27m11yAuthor Commented:

This is working
$ cat -n ifelseif_01.sh
     1  #!/bin/sh
     2
     3  if [ -f sqlscript_01.sh ];
     4  then
     5  echo "file found"
     6  echo "job successful"
     7  else
     8  echo "file not found"
     9  fi

Open in new window

0
d27m11yAuthor Commented:

How can I find if a file exists in root directory and its subdirectories using "if" condition with the same kind of code, please advise
0
omarfaridCommented:
remove ; from end of the line

 if [ -f find -name sqlscript_01.sh ]

also -name is not an option , remove it. I did not notice it when I posted my comment :)
0
omarfaridCommented:
don't use if, in this case you need to run find command like

find / -name sqlscript_01.sh > /dev/null 2>&1
if [ $? != 0 ]
then
   echo "error message"
else
 echo file found
fi
0
d27m11yAuthor Commented:

Does this mean

if [ $? != 0 ] --- if file not found in root directory and its subdirectories, please advise

Also, I could not understand what you meant by

find / -name sqlscript_01.sh > /dev/null 2>&1

Please help!
0
omarfaridCommented:
Yes, if the find command failed then the exit code $? will not be 0 (zero).

the line

find / -name sqlscript_01.sh > /dev/null 2>&1

is just a search from the dir / and sub dirs for a file with the name ( -name ) sqlscript_01.sh

the > and 2>&1 is redirecting the output and error messages of find command to the /dev/null file which is basically ignoring them
0
farzanjCommented:
if [ $? != 0 ] --- if file not found in root directory and its subdirectories, please advise
No, this is the exit status of last command.  This checks that it did not exit in error
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
d27m11yAuthor Commented:

I tried the following code and it is not exiting, it is kinda stuck... please advise
$ cat -n ifelseif_02.sh
     1  #!/bin/sh
     2  find / -name sqlscript_01.sh > /dev/null 2>&1
     3  if [ $? != 0 ]
     4  then
     5  echo "file not found"
     6  else
     7  echo "file found"
     8  fi

Open in new window

0
omarfaridCommented:
the find command will take some time specially if you have large number of files and subdirs
0
d27m11yAuthor Commented:

what if I find in current directory only, can I use this

$ cat -n ifelseif_02.sh
     1  #!/bin/sh
     2  find . -name sql.sh > /dev/null 2>&1
     3  if [ $? != 0 ]
     4  then
     5  echo "file not found"
     6  else
     7  echo "file found"
     8  fi

Open in new window

0
d27m11yAuthor Commented:

When I tried the above code, it says " file found" when there is no such sql.sh in my current directory, please advise what is wrong with my code ?

Appreciate your help!
0
omarfaridCommented:
the find command will always go to sub dirs

if you want to search in current dir only the don't use find and use

if [ -f filename ]

as before
0
Gerwin Jansen, EE MVETopic Advisor Commented:
This is working:
found=`find . -name sql.sh 2>/dev/null`
if [ $? != 0 ]
then
  echo "File not found"
else
  echo "File found here: ${found}"
fi

Open in new window


The result of the find is stored in the found variable and shown as a result.
0
d27m11yAuthor Commented:
Resolved the issue!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.