Check if there is a certain file in a directory
To check if a file file.txt
is present in the directory passed as the first argument to the script or function, use
[ -e "$1/file.txt" ]
Don't forget to double-quote variable substitutions.
This succeeds if the file exists, and fails if the file doesn't exist. Failure includes the case where the file exists but can't be reached, e.g. because you don't have the permission to traverse the directory.
Note that in shell scripts, and when it comes to process exit statuses, 0 means success and 1 (or more, up to 125) means failure. See What return/exit values can I use in bash functions/scripts? and Default exit code when process is terminated? for more details. So if you want to check whether a file exists, your script or function must return 0 if the file exists and 1 otherwise. Your snippet does the opposite: it checks if the file doesn't exist.
if [ -e "$1/file.txt" ]; then
exit 0
else
exit 1
fi
is just a more complicated way of writing
[ -e "$1/file.txt" ]
exit
(exit
with no status argument uses the status of the previously executed command). And if this is at the end of the script then exit
is redundant.
If you did want to check that the file doesn't exist then you would invert the command with the !
shell operator:
! [ -e "$1/file.txt" ]
or the !
test
/[
operator:
[ ! -e "$1/file.txt" ]
Related videos on Youtube
![huMpty duMpty](https://i.stack.imgur.com/B1ec7.jpg?s=256&g=1)
huMpty duMpty
Updated on September 18, 2022Comments
-
huMpty duMpty almost 2 years
I am using third party web service and trying to log the outgoing SOAP envelop when the web service method is called.
Came across this article about How to capture SOAP envelopes when consuming a web service in ASP.NET. and implemented similar way. B
But for some reason
ProcessMessage
is not get called.My class and namespace looks like below
namespace MyServiceClient { public class MySeriviceSoap : SoapExtension { ......
And my
app.config
<?xml version="1.0"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> <system.web> <webServices> <soapExtensionTypes> <add type="MyServiceClient.MySeriviceSoap, MyServiceClient, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" priority="1" /> </soapExtensionTypes> </webServices> </system.web> </configuration>
Any help on this please
-
George Vasiliou over 7 yearsstackoverflow.com/questions/638975/… correct switch is -f, not -e...
-
Gilles 'SO- stop being evil' over 7 years@GeorgeVasiliou No,
-e
is the correct switch.-f
tests whetherfile.txt
exists and is a regular file.-e
just tests if the file exists.
-
-
huMpty duMpty almost 9 yearsGreat! Will try this out
-
huMpty duMpty almost 9 yearsTried using fully qualified type name, but having the same result. I have updated the question with new code
-
CodeCaster almost 9 yearsOh, this is for Web Services (ASMX)... chances are you have a WCF client. Update your question with what client you're using.
-
huMpty duMpty almost 9 yearsIt something been already implemented using web service (wsdl)
-
CodeCaster almost 9 yearsYou don't seem to understand what I'm asking. Is the client generated through ASMX (wsdl.exe, Add Web Service Reference) or through WCF (svcutil.exe, Add Service Reference)?
-
huMpty duMpty almost 9 yearsOh sorry, its been added as "Add Web Service Reference"