The script was effective even with the syntax error, so it seems that this question really boils down to how to run a script.
Running as an Executable File
To run a script that is in the current directory without explicitly invoking the shell, you must preface its name with ./
. It must also be executable.
chmod u+x install-depot-multisystem.sh
./install-depot-multisystem.sh
That will only work if the script starts with a hashbang line specifying what shell is supposed to run it.
If you ran chmod 777
on the script, and that helped, then the reason it helped was almost certainly that it gave you, as the owner of the script, execute permissions on the script. You also gave yourself and all other users read and write permissions, as well as execute permission to all other users. This is unnecessary, potentially a serious security problem. chmod u+x
is just as good, and much safer.
Invoking the Shell
Alternatively you can run the script by explicitly invoking the shell, like you had tried initially:
sh install-depot-multisystem.sh
The script has a .sh
extension, so it should be runnable with sh
. But in case the script author named it badly, and it's really a bash
script, you can try running with bash
(as geirha suggested):
bash install-depot-multisystem.sh
Running in the Current Shell
This is included for completeness only--unless you wrote the script and know this will work properly, or the instructions that accompany the script say to run it this way, you should use one of the previous two methods instead. Many scripts will only work right if they have their own shell in which to run (which is accomplished by either of the above two methods).
With that said, you can attempt to run the script in the current shell, with the current shell's environment, with either of the following commands:
. install-depot-multisystem.sh
source install-depot-multisystem.sh
Running as root
If the script is installing a systemwide program or service, it might need to be run as root. Do not run a script (or anything) as root unless you know you need to do so.
To run it as root, put sudo
in front of the command that runs it. Any of these three commands will work (though the first still requires execute permissions):
sudo ./install-depot-multisystem.sh
sudo sh install-depot-multisystem.sh
sudo bash install-depot-multisystem.sh
Best Answer
There are three obvious errors:
else s=$1-50 m=$2-50 h=$1+1 fi
, the wordfi
is not treated as a keyword, because it is not the first word in the command. To the shell, this looks like three assignments that apply to the commandfi
. If you ever got to execute this line, you'd see an errorbash: fi: command not found
. Putfi
on a line of its own (or put a;
before it).[ "$3" < 50 ]
is the same as[ "$3" ] < 50
— it's the command[ … ]
(which can also be writtentest
) with the sole argument"$3"
, and with an input redirection from the file50
. Either use the numeric comparison operator-lt
, or use an arithmetic instruction(( … ))
. The single bracket construct is an ordinary built-in command, so special characters such as<
retain their normal meaning. The double parenthesis construct is special syntax, and you can use<
as a numeric comparison operator inside.["$2" < 50 ]
is missing a space after the opening bracket.Also the usual convention in shell scripts is to put a newline after
then
andelse
. Furthermore, instead of anelse
block that consists entirely of anif
statement, you should useelif
. And please indent consistently.P.S. I haven't reviewed your logic. You seem to be looking for
date +%T -d 'now + 10 seconds'
.