Testing

test

Lesson 9 | Lesson 11


The symbol "[" is a symbolic link to test and is a shell builtin.

type [
[ is a shell builtin



who; echo $?
2009-07-20 18:18 (:0)
mike     pts/0        2009-07-20 18:19 (:0.0)
mike     pts/1        2009-07-20 18:25 (:0.0)
0





!/bin/bash

#ping -c3 123.123.123.123
ping -c3 127.0.0.1
echo $?

sh test.sh
connect: Network is unreachable
2






sh test.sh
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.070 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.072 ms

--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.070/0.073/0.078/0.007 ms
0



Test a String
The test requires 3 parameters; the variable name($NAME), the test condition(=) and the value(fred).

Name="fred"
test $NAME = fred
echo $?


When you do tests enclose the variables in double quotes.

string test
-z    zero-length
-n    non-zero length


Numeric tests
string test
=
!=
<=
>=
>
<

Numeric tests
-eq
-neq
-le
-ge
-gt
-lt

x=4
test "$x" -lt 10
echo $?



File test

test -f file

test -d /etc

if [ -f /etc/vsftpd/vsftpd.conf  ]; then
echo 'File exists'; another_command;
fi

Check for empty string
test -z str
or
[ -s str ]

if [ -z “$FRUIT” ]; then
echo “Your fruit”
else
echo “second message”
fi

equal strings
test str1 = str2
or
[ str1 = str2 ]


File Test Operators
-b file        block file
-c file        character file
-d file        directory
-e file        file exists
-f file         regular file, not directory
-G file        file exists and owned by group id
-g file        set group id is set
-k file        sticky bit is set
-L file        symbolic link
-p file        named pipe
-O file        file exists and owned by effective uid
-r file        file is readable
-S file        file is a socket
-s file        file is nonzero in size
-f fd        true if file descriptor is opened on a terminal
-u file        Set user ID bit is set
-w file        file is writeable
-x file        file is executable

ssh.sh

#!/bin/bash

cd ~
cd .ssh
file=authorized_keys
if [ -x $file ]
then
echo "Permissions are incorrect they should be 644"
elif [[ -r $file && -w $file ]]
then
echo "You have read and write permissions to authorized_keys"
else
echo "YOU must pay attention to permissions"
fi

You want to move into the hidden .ssh directory in order to evaluate the authorized_keys file.  
cd ~
cd .ssh

This file is important as it helps set up a connection using SSH without using a password, so permissions are ctitical and often a place where users have a problem.  The variable is set up with this line and will be used in subsequent lines.
file=authorized_keys

Here a if...then clause is set up to evaluate the permissions on this file.  The first evaluation is to use testing to determine if the file is executable.  If the file is executable, SSH will not allow the use of the authorized_keys file so you need to reinforce the actual permissions.
if [ -x $file ]
then
echo "Permissions are incorrect they should be 644"

The second option will evaluate to see if the user had the read right (-r) and the write right (-w) to the file which would indicate the correct permissions for a specific user.
elif [[ -r $file && -w $file ]]
then
echo "You have read and write permissions to authorized_keys"

The final option will cover any thing else that occurs with the permissions, which will not work.
else
echo "YOU must pay attention to permissions"
fi

Now this script will work fine for a specific user, but if you run it as root you will find that it is not accurate because effectively, root and write to any file wheter it has write permissions or not.  Here is an example of the permissions and how they work with a user called sue.  You can see the script functions correctly.

The script executed as sue.

-r--r--r-- 1 sue sue    0 Aug  1 09:16 authorized_keys
YOU must pay attention to permissions


-rwxr-xr-x 1 sue sue    0 Aug  1 09:16 authorized_keys
Permissions are incorrect they should be 644


-r--r--r-- 1 sue sue    0 Aug  1 09:16 authorized_keys
YOU must pay attention to permissions

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874