I'm relatively new to unix command line and I do not really understand what this command does:
[[ ! -e "$text_file" ]] || { echo "Error: I cannot overwrite '$text_file'." >&2;}
Does anyone have an idea?
I'm relatively new to unix command line and I do not really understand what this command does:
[[ ! -e "$text_file" ]] || { echo "Error: I cannot overwrite '$text_file'." >&2;}
Does anyone have an idea?
First of all, it checks 2 conditions with logical OR || operator.
-e FILE - True if the FILE exists and is a file, so the condition is negating the result of this check.
and then if this is false then it goes and prints the message and send that to stderr (>&2). If the first condition is true then second condition won't be evaluated (of course this default logical OR way of working)
In Unix-world, stdout is generally used when everything is working correctly and stderr is generally used to print messages when something goes wrong.
By default, stdout and stderr both print to your screen. The main difference is that the > and | operators catch stdout by default, but not stderr.
You can also check other things like below:
-r FILE - True if the FILE exists and is readable.
-w FILE - True if the FILE exists and is writable.
-x FILE - True if the FILE exists and is executable.
-d FILE - True if the FILE exists and is a directory.
-e FILE - True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
-f FILE - True if the FILE exists and is a regular file (not a directory or device)
This checks a file for existence and the ! negates the expression See a list of bash file testing