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?
 
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
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.