5

I am trying to run a simple Bash script called deploy-site.sh (by http://klau.si/dev). I made the necessary edits to run it locally (change of WORKDIR, etc). I also followed the instructions here.

mkdir ~/bin
chmod 755 ~/bin

(To store the script in my home folder. Then I rebooted, so the system recognizes it.)

sudo chmod +x <path>
sudo chmod 755 <filename>

However, whenever I go to the terminal to execute the Bash script, I get:

$ sudo deploy-site.sh <sitename>
sudo: deploy-site.sh: command not found

How can I fix this problem?


Here is the original script:

#!/bin/bash

if [[ $# -lt 1 || $1 == "--help" || $1 == "-h" ]]
then
  echo "Usage:"
  echo "  sudo `basename $0` SITENAME"
  echo "Examples:"
  echo "  sudo `basename $0` drupal-8"
  exit
fi

WORKDIR="/home/klausi/workspace"
APACHEDIR="/etc/apache2/sites-available"
HOSTSFILE="/etc/hosts"

echo "<VirtualHost *:80>
    ServerAlias $1.localhost
    DocumentRoot $WORKDIR/$1
    <Directory \"$WORKDIR/$1\">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>" > $APACHEDIR/$1.conf
a2ensite $1
service apache2 restart
grep -q "127.0.0.1  $1.localhost" $HOSTSFILE
if [ $? -ne 0 ]; then
  echo "127.0.0.1  $1.localhost" >> $HOSTSFILE
fi

As requested, here is the results of echo $PATH

/home/getoprodigy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

  • Just because you add it to ~/bin and reboot doesn't mean that the system recognizes it - you need to add it your PATH. – Wilf Jul 12 '15 at 8:36
  • 2
    Add output of echo $PATH to your question. – Cyrus Jul 12 '15 at 9:10
  • 1
    Please run echo $PATH to see if the directory does not appear in $PATH for some reason. @Wilf it should be in $PATH after log out/in or running source ~/.profile. – Jacob Vlijm Jul 12 '15 at 9:20
  • @JacobVlijm not if run with sudo... :) – Wilf Jul 12 '15 at 9:24
  • 1
    @Wilf aargh, that's why one shouldn't visit AU on a small smartphone :) – Jacob Vlijm Jul 12 '15 at 9:30
12

deploy-site.sh is not in PATH. You have to run it as ./deploy-site.sh if you are in the same directory or /path/deploy-site.sh if not

  • 1
    Just to mention: ~/bin should be in $PATH if the directory exists and after log out or running source ~/.profile. – Jacob Vlijm Jul 12 '15 at 9:12
8

To run it as deploy-site.sh you need to add it to your $PATH - you can view your current path with echo $PATH:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ 

↑ standard path on Ubuntu 14.04 (probably)

You can a few things to 'fix' it:

  • Run it from the current directory:

    wget http://klau.si/sites/default/files/deploy-site.sh_.txt -O deploy-site.sh
    sudo ./deploy-site.sh
    
  • Add it to a existing directory PATH: As above you can view the existing path with echo $PATH, and add it to one of the directories to make it work - for custom commands that you want to run system-wide /usr/local/bin usually suffices.

    wget http://klau.si/sites/default/files/deploy-site.sh_.txt -O deploy-site
    sudo cp deploy-site /usr/local/bin/
    sudo chmod +x /usr/local/bin/
    sudo deploy-site
    
  • Add the directory to the PATH: Normal practice too add it permanently would be to add this to ~/.profile, ~/.bash_profile etc:

    #.bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ] ; then
    . ~/.bashrc
    fi
    
    #User specific environment and startup programs
    PATH=$PATH:$HOME/.local/bin:$HOME/bin
    
    export PATH
    

    BUT this only works for your current user (not when run with sudo). To make it work with sudo (i.e. the root user) ou should be able to make in a similar way by (CAREFULLY) modifying /etc/environment (or possibly /root/.profile etc), but as this script looks like it only need to be run once, I would recommend one of the above methods.

2

You are just changing the file permission. To run the script you can specify full pathname as tikend's answer.

If you want to use it as a command from any directory, create a link in /usr/bin and you can use the link as command.

$ cd /usr/bin
$ sudo ln /path/to/your/script linkname

now you can type linkname from any directory.

  • make the symlink, but can't run from any directory. still requires a path to execute – yupthatguy Jul 13 '15 at 9:28
  • did you try the the script's filename or the linkname. you have to use the linkname as command now. – Pravin Jul 13 '15 at 17:39
  • but what is the link name? – Lan... Jul 20 '16 at 15:37
  • linkname is whatever you want your command to be called by. you fill it in. – Brian Sizemore Jul 20 '16 at 15:49
1

You can run it by

sudo bash deploy-site.sh
  • 1
    This answer makes no sense, because there is already a shebang in the script. – A.B. Jul 12 '15 at 10:48
  • still should work - I don't think it needs the ./ or to executable permission then as well. – Wilf Jul 12 '15 at 11:35
  • 2
    I see down-votes and delete flag, please explain why before voting down or deleting this answer, I am sure it works and I use this way, you can see it as a hint in tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html – Mike Jul 12 '15 at 17:24
  • for all those concerned this answer works perfectly... but adding the bash command seems to be an extra step... anyway I can run script with the bash as designed? – yupthatguy Jul 13 '15 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.