Solved [Solved] if then shell script help

Hello,

Could someone please help to tidy this script a little..
The script will be run inside the jail to create the user and install the full web server.
Code:
#!/bin/sh
#
#	FreeBSD 10 -RELEASE ZFS - jail post installation script

if [ "$(whoami)" != 'root' ]; then
        echo "You have no permission to run $0 as non-root user."
        exit 1;
fi

hostname=$(hostname -s)

if [$hostname == webjail]
then 
pw groupadd -q -n webadmin -g 1001
echo -n '$1$p75bbfK.$Kz3dwkoVlgZrfLZdAXQt91' |\
pw useradd -n webadmin -u 1001 -c "System Administrator" -g webadmin -G wheel -s /bin/csh -d /home/webadmin -m -H 0
pw usermod webadmin -p 01 01 01
chown -R webadmin:webadmin /home/webadmin

elif [$hostname == sqljail]
then 
pw groupadd -q -n dbadmin -g 1002
echo -n '$1$p75bbfK.$Kz3dwkoVlgZrfLZdAXQt91' |\
pw useradd -n dbadmin -u 1002 -c "Database Administrator" -g dbadmin -G wheel -s /bin/csh -d /home/dbadmin -m -H 0
pw usermod dbadmin -p 01 01 01
chown -R dbadmin:dbadmin /home/dbadmin

elif [$hostname == mailjail]
then 
pw groupadd -q -n mailadmin -g 1003
echo -n '$1$p75bbfK.$Kz3dwkoVlgZrfLZdAXQt91' |\
pw useradd -n mailadmin -u 1003 -c "Mail Administrator" -g mailadmin -G wheel -s /bin/csh -d /home/mailadmin -m -H 0
pw usermod mailadmin -p 01 01 01
chown -R mailadmin:mailadmin /home/mailadmin
else
echo "no match found"
exit 1;
fi

I am not sure that my syntax is correct and I haven't got the jail setup up yet to test it

Will case statement more suited for the job?

Thank you in advance

Fred
 
Re: if then shell script help

At quick glance the script looks fine. Except perhaps this bit:
hostname=$(hostname -s)

if [$hostname == webjail]
then [/code]

If $hostname happens to be empty this line will generate a syntax error. It's better to use this:
Code:
if [ "$hostname" == "webjail" ]; then
 
Re: if then shell script help

I have made the changes:
Code:
hostname=$(hostname -s)

if [$hostname == webjail];
then 
	pw groupadd -q -n webadmin -g 1001
	echo -n '$1$p75bbfK.$Kz3dwkoVlgZrfLZdAXQt91' |\
	pw useradd -n webadmin -u 1001 -c "System Administrator" -g webadmin -G wheel -s /bin/csh -d /home/webadmin -m -H 0
	pw usermod webadmin -p 01 01 01
	chown -R webadmin:webadmin /home/webadmin
elif [$hostname == sqljail];
then 
	pw groupadd -q -n dbadmin -g 1002
	echo -n '$1$p75bbfK.$Kz3dwkoVlgZrfLZdAXQt91' |\
	pw useradd -n dbadmin -u 1002 -c "Database Administrator" -g dbadmin -G wheel -s /bin/csh -d /home/dbadmin -m -H 0
	pw usermod dbadmin -p 01 01 01
	chown -R dbadmin:dbadmin /home/dbadmin
elif [$hostname == mailjail];
then 
	pw groupadd -q -n mailadmin -g 1003
	echo -n '$1$p75bbfK.$Kz3dwkoVlgZrfLZdAXQt91' |\
	pw useradd -n mailadmin -u 1003 -c "Mail Administrator" -g mailadmin -G wheel -s /bin/csh -d /home/mailadmin -m -H 0
	pw usermod mailadmin -p 01 01 01
	chown -R mailadmin:mailadmin /home/mailadmin
else
echo "no match found"
exit 1;
fi
and when I tested in a normal FreeBSD box, It gave me the following:
Code:
./fred.sh: [trinity: not found
./fred.sh: [trinity: not found
./fred.sh: [trinity: not found
no match found
How could I get rid of the "[trinity: not found" message?
trinity is the host name of my test box
 
Re: if then shell script help

The [ character is actually a command, test(1). There has to be at least one white-space character after it:


Code:
if [ $hostname == webjail ];
...
 
Re: if then shell script help

The square bracket [ is not punctuation, it is a command. See test(1). So it needs to be separated from text around it with spaces.

Please use indentation inside if/else/elsif, it is free and greatly improves readability.

(Edit: beaten by seconds by @kpa...)
 
Last edited by a moderator:
Re: if then shell script help

Thank you all for the replies.
I have got it to work by adding the space :)
 
There's one more thing you should be aware of and that is the shell variable expansion when the values of the variables may contain white space characters. It won't probably matter in your case here because the host names will be strings without any embedded white space characters. However, if you are dealing with strings with embedded white space characters things can go horribly wrong if your script is not written with that in mind. Consider these simple examples, this is with the standard /bin/sh shell:

Code:
$ foo="string with spaces"
$ if [ $foo = "string with spaces" ]; then
> echo "it has spaces";
> fi
[: string: unexpected operator

Code:
$ if [ "$foo" = "string with spaces" ]; then
> echo "it has spaces";
> fi
it has spaces
$

The only difference there is that the variable foo is in double quotes in the second example and the test works properly. What happens is that in the first case the shell expands the value of $foo into a list of words that are then fed to test(1) as command line arguments. Because there are no double quotes around variable that is being expanded each word is put into a separate argument. The arguments in the first case are exactly (each argument on its own line):

Code:
string
with
spaces
=
string with spaces
]

That's a syntax error because test(1) expects the second argument to be an operator and not a plain word since the first argument was not any of the known operators that can start the expression.

In the second example the arguments are different because of the use of the double quotes:

Code:
string with spaces
=
string with spaces
]

Always use double quotes around the variables as in the second example, you'll never know if you have to pass values in the variables that include white space characters.
 
@kpa, Thank you very much for the crash course:) I really value that time you spend writing this explanation for me.
 
Last edited by a moderator:
Back
Top