1

I've copied a script from the unifi documentation

#!/bin/sh

## define required variables
username=admin
password=admin
baseurl=https://localhost:8443

## include the API library
. unifi_sh_api

unifi_login
# unifi_authorize_guest <mac> <minutes> [up=kbps] [down=kbps] [bytes=MB]
unifi_authorize_guest $1 $2
unifi_logout

This is the script and this is the file structure:

foo@site:/home/foo# ls
unifi.sh  unifi_sh_api

This is what I get when I try to execute the file. What can cause this? The file is obviously in the right folder.

foo@site:/home/foo# sh unifi.sh
unifi.sh: 9: .: unifi_sh_api: not found
user3218338
  • 652
  • 1
  • 8
  • 20
  • 2
    What if you use `source` instead of `.`? – fedorqui Mar 13 '14 at 09:08
  • 1
    Add current directory that is . in PATH and export it. Then the execution will work.. – Raghuram Mar 13 '14 at 09:18
  • 1
    @fedorqui I thought "source" was just an alias for dot ("."), so I don't understand why you would suggest exchanging them. Would you mind enlightening me if you have a minute please? – Mark Setchell Mar 13 '14 at 09:46
  • @MarkSetchell honestly I believe they are the same, but I saw this thread: http://stackoverflow.com/questions/670191/getting-a-source-not-found-error-when-using-source-in-a-bash-script and thought that in certain cases they could have different behaviour. I am pretty sure that it is not the answer, though. – fedorqui Mar 13 '14 at 11:07

1 Answers1

1

You are invoking bash as /bin/sh which puts bash into sh-compatibility mode. The POSIX standard says that:

If file does not contain a slash, the shell shall use the search path specified by PATH to find the directory containing file.

Which means that the current directory will not be searched unless it is part of $PATH:

$ /bin/sh -c '. test.sh'
/bin/sh: 1: .: t.sh: not found

$ /bin/sh -c 'PATH=".:$PATH"; . test.sh'
$

bash, however, seems to search the current directory:

$ /bin/bash -c '. test.sh'
$
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71