Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't bash recognize the existence of a socket file

On a Linux box I want to check if a specific socket file exists. I know the socket files exists, but my checks in bash don't show that to me:

$ ls -l /var/run/supervisor.sock
srwxrw-rw- 1 root root 0 Jun  3 13:30 /var/run/supervisor.sock  # <== THE FILE EXISTS!!
$ if [ ! -f /var/run/supervisor.sock ]; then echo 'file does not exist!'; fi
file does not exist!

Why oh why can't bash see that the file exists?

like image 318
kramer65 Avatar asked Jun 03 '16 15:06

kramer65


People also ask

How do you check if a file doesn't exist in bash?

In order to check if a file does not exist using Bash, you have to use the “!” symbol followed by the “-f” option and the file that you want to check. Similarly, you can use shorter forms if you want to quickly check if a file does not exist directly in your terminal.

How do you check if directory does not exist in bash?

Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.

How do you check whether a file is present or not in shell script?

Many times when writing Shell scripts, you may find yourself in a situation where you need to perform an action based on whether a file exists or not. In Bash, you can use the test command to check whether a file exists and determine the type of the file.


1 Answers

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
like image 181
bodangly Avatar answered Oct 06 '22 07:10

bodangly