Reputation: 727
What does !<
means in this bash script, and why it points to file that does not exist?
bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; npm start'
Thanks in advance for your time!
Upvotes: 3
Views: 351
Reputation: 249
!
is a negation operator.
Example:
while ! false; do
echo "inside while loop"
sleep 1
done
will be inside the loop forever since ! false
is always true.
<
is another operator. It tells bash to read the specified file. It will succeed (return zero exit code) if file exists and is readable. Without left operand, the read results will be discarded.
Example: check for /tmp/sample_file
existence:
if < /tmp/sample_file; then echo 'file exists'; fi
Thus, your code will run sleep 1
in a loop while /dev/tcp/db/5432
cannot be read.
Now take into account that /dev/tcp/
is a special path and accessing /dev/tcp/db/5432
means trying to connect to host db
via TCP port 5432.
So the logic behind your while loop is 'sleep until postgresql on host db is ready' (5432 is the default port for PostgreSQL).
Upvotes: 5
Reputation: 85580
You should read that together with the file thats after <
. The syntax < file
opens the contents of the file for input redirection for any application to read over standard input.
In your specific syntax </dev/tcp/db/5432
(tcp port on a remote host), the construct is defined just to verify is file is accessible for open for reading. The command will return successful exit code 0
, if file is accessible and !
negates the exit code returned.
So basically the while loop in your example, sees while 1
, if the file is accessible, which in the shell context means a non-zero exit code return, which doesn't let the sleep to happen at all. It is not entirely clear if your intention was to sleep until the file is available to open or the other way.
Upvotes: 5