Quote:
Originally Posted by boygenuis
For instance, this first bit:
Code:
echo Usage: $0 \<user1\> \[\<user2\> \[...\]\]
|
That was just me creating a simple "help" message to be displayed if the batch file is called without any argument. The
echo command simply writes everything to the screen, but since the text contains characters that have special meaning to the shell (like >, <, [ and ]), I had escape them with backslashes.
Quote:
Originally Posted by boygenuis
I'm also lost-in-the-sauce on this loop.
Code:
while [[ ! "$1" == "" ]]; do
if ! getent passwd $1 > /dev/null ; then
I don't get why the exclamation point comes before positional parameter $1, as we want to check that it doesn't equal null. And then, is the exclamation point in the if-statement an error and we should put brackets?
|
The ! is a general negation operator. Sometimes "is not equal" may be slightly easier to read than "is unequal". For instance, here are two versions of the same test:
Code:
if [ "foo" != "bar" ]; then
echo The strings are not equal.
fi
if [ ! "foo" == "bar" ]; then
echo The strings are not equal.
fi
I tend to use negation a lot rather than switching to the "is not equal" comparison operator, but it's really a matter of personal taste.
Anyway, the main loop in the script is this:
Code:
while [[ ! "$1" == "" ]]; do
# stuff
shift
done
The idea is pretty simple: We loop while $1 is not equal to "" (nothing). At the end of the loop, $1 gets shifted into $0 (the original $0 is lost) and the new value of $1 is whatever was held in $2. We do this until an empty value is shifted into $1, which means we've run out of command-line arguments.
The
shift command shuffles the contents of the command line variables one step "to the left", that is, $0 becomes $1, $1 becomes $2 and so on, and $9 becomes whatever was the 10th argument (if that many arguments were given).
As for the
if statement, the basic statement is this:
Code:
if command; then something
If you've never encountered that syntax, it may certainly look confusing. What it actually means, is "if
command returns an exit code of 0, then
something". I'm exploiting the fact that commands return an exit code of 0 if the operation completed successfully (whatever that means in the context of any given command).
getent passwd <user> returns 0 if it's able to find the account
<user> in the user database. Try this:
Code:
if getent passwd root ; then echo The root account exists ; fi
Replace the "root" argument to
getent passwd with a non-existent account and you'll see that the "then" part doesn't get executed.
In the script I used an exclamation mark to invert the result, so:
Code:
if ! getent passwd $1 > /dev/null ; then
...really means "if the command
getent passwd $1 does
not returns an exit code of zero, meaning the account doesn't exist, do
<whatever>". Since the
getent command will generate output if it
does find the account, I redirect all output to
/dev/null.
(The "!" actually works on the command line as well. If you run
ls (which is exceedingly likely to succeed) and then read the exit code with
echo $?, you'll get zero. Now try it with
! ls.)