Why can't bash recognize the existence of a socket file
http://www.tldp.org/LDP/abs/html/fto.html
Use -S
to test if its a socket. -f
is for regular files.
See man 1 test
:
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
...
...
-S FILE
FILE exists and is a socket
How do I tell if a file does not exist in Bash?
The test
command (written as [
here) has a "not" logical operator, !
(exclamation mark):
if [ ! -f /tmp/foo.txt ]; then
echo "File not found!"
fi
Shell prompt does not shows on socket
man bash:
An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and
error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.
A non-interactive shell normally won't print the prompt at all.
Always gives false even though file exists and is not empty
It is enough to check for -s
, because it says:
FILE exists and has a size greater than zero
http://unixhelp.ed.ac.uk/CGI/man-cgi?test
also your output is switched, so it outputs does not exists
when a file exists, because -s
will give TRUE
if file exists AND has a size > 0
.
So correctly you should use:
echo " enter file name "
read file
if [ -s "$file" ]
then
echo " file exists and is not empty "
else
echo " file does not exist, or is empty "
fi
This will give you the expected output.
Also it should be
read file
instead of
read $file
If you want further informations, I recommand reading man test
and man read
How can I check a file exists and execute a command if not?
[ -f /tmp/filename.pid ] || python daemon.py restart
-f
checks if the given path exists and is a regular file (just -e
checks if the path exists)
the []
perform the test and returns 0
on success, 1
otherwise
the ||
is a C-like or
, so if the command on the left fails, execute the command on the right.
So the final statement says, if /tmp/filename.pid
does NOT exist then start the daemon.
Related Topics
How to Compile a Node C++ Addon So That I Can Use Distribute It on Amazon Aws
What's The Advantage of 3G/1G Vm Split ? 32Bit Linux Kernel
Vagrant - How to Mount The Virtualbox Shared Folder? ("Vboxsf" Not Available)
How to Connect to Sftp Using Linux Command from Perl
How to Have Postgresql Not Collapse Punctuation and Spaces When Collating Using a Language
Svn Setup of Existing Directory
Svn Keeps Prompting Me for Passwords and Refuses to Cache My Credentials
Specify CPU Frequency as a Kernel Cmd_Line Parameter of Linux on Boot
Socket Send Concurrency Guarantees
How to Remove File with Special Characters
Sed to Loop Through File and Replace Placeholder Variables