I often find on files .sh
Shell Script in conditionals sintaxis like these:
if [ -n "condition" ]; then ...
if [ -z "condition "]; then ...
if [ -x "condition" ]; then ...
if [ -L "condition" ]; then ...
if [ -d "condition" ]; then ...
Those -n, -z, -x, -L, -d
are functions or how is it name and what is it purpose?
If you are using a different shell, just search for <my shell> operators and you will find everything you need. In your particular case, you are using: -n string is not null. - z string is null, that is, has zero length.
If you don't want to use echo repeatedly to create new lines in your shell script, then you can use the \n character. The \n is a newline character for Unix-based systems; it helps to push the commands that come after it onto a new line. An example is below.
“ls” is the name of the program or command built into the shell to run. “-l” is a special arguments called options, that helps the “ls” command to list the files in a long format, including the permissions, ownership, timestamps and file sizes. Most options start with a hyphen then a letter (the option itself).
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
IMHO best way is you could simply do man test
for all these details. It is very well explained there. As follows is the text from man page. For BASH conditional expressions look for link https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html too once.
-b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE FILE exists -f FILE FILE exists and is a regular file -g FILE FILE exists and is set-group-ID -G FILE FILE exists and is owned by the effective group ID -h FILE FILE exists and is a symbolic link (same as -L) -k FILE FILE exists and has its sticky bit set -L FILE FILE exists and is a symbolic link (same as -h) -O FILE FILE exists and is owned by the effective user ID -p FILE FILE exists and is a named pipe -r FILE FILE exists and read permission is granted -s FILE FILE exists and has a size greater than zero -S FILE FILE exists and is a socket -t FD file descriptor FD is opened on a terminal -u FILE FILE exists and its set-user-ID bit is set -w FILE FILE exists and write permission is granted -x FILE FILE exists and execute (or search) permission is granted
For expressions in man test
it is given:
( EXPRESSION ) EXPRESSION is true ! EXPRESSION EXPRESSION is false EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true -n STRING the length of STRING is nonzero STRING equivalent to -n STRING -z STRING the length of STRING is zero STRING1 = STRING2 the strings are equal STRING1 != STRING2 the strings are not equal INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2 INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
For conditional expressions info go for man bash
it gives info as follows too.
CONDITIONAL EXPRESSIONS Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. Expressions are formed from the following unary or binary primaries. If any file argument to one of the primaries is of the form /dev/fd/n, then file descriptor n is checked. If the file argument to one of the primaries is one of /dev/stdin, /dev/stdout, or /dev/stderr, file descriptor 0, 1, or 2, respectively, is checked.
Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather
than the link itself.
When used with [[, The < and > operators sort lexicographically using the current locale.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With