Pages

Linux "-f" or linux "-z" or linux "-eq" or linux "-gt" ... ?

Often friends and people that I know that start to have an interest in Linux end up doing shell scripting and they get their ears caught. I usually get e-mails with questions like this:


What is linux "-f" or linux "-z" or linux "-eq" or linux "-gt" or linux "-rf" or linux "-le" ?
The short answer for the above is that those are number and file tests/comparisons of korn/bash shells. Linux shells are the primary ways the users interact with Linux operating systems. With shells you can interact with the file system and execute programs. You can chain shell commands in scripts in a programmatic way.

Number and file tests/comparisons occur in shell scripting constructs such as :


if [ true ]
then
  # do something here
fi
You can replace the "true" between the [] with expressions that evaluate to true or false, for example:

if [ 5 -eq 5 ]
then
  echo "5 equals 5"
fi
The above decisional block in plain english is " if 5 equals 5 print the string "5 equals 5" ".

File tests/comparisons are:

"-d" file Test if file is a directory
"-e" file Test if file exists
"-f" file Test if file is an ordinary file
"-r" file Test if file is readable
"-w" file Test if file is writable
"-x" file Test if file is executable

Numeric comparison operators are:

"-eq"    Checks if two operands are equal or not
"-ne"    Checks if two operands are equal or not, if values are not equal then condition becomes true.   
"-gt"    Checks if left operand is greater than    right operand
"-lt"    Checks if left operand is less than    right operand
"-ge"    Checks if left operand is greater than or equal to    right operand
"-le"    Checks if left operand is less than or equal to    right operand

I usually give people that want to learn shell scripting the finger/
This was another linux stuff article that nobody proably gives a damn about it.

No comments :

Post a Comment