0

I have a script in FreeBsd 11

% cat my_script.sh 
#!/usr/local/bin/zsh

# [........]

It has x attribute:

-rwxr-xr--   1 root  wheel  669 Jun 14 02:45 my_script.sh

My shell is zsh. When I'm trying to execute it, I get an error. It has to do with the way I'm invoking it:

% sudo ./my_script.sh var1                  
sudo: ./my_script.sh: command not found


% sudo my_script.sh var1 
sudo: my_script.sh: command not found


% sudo source my_script.sh var1
sudo: source: command not found

What's the matter?

update:

I've changed my shell to bash, rebooted, changed the #! to bash and now I have:

$ sudo ./my_script.sh test9
sudo: ./my_script.sh: command not found
  • Pretty weird to have x permission for others, but not for the owner or group. – user41515 Jun 14 '17 at 2:56
  • @WumpusQ.Wumbley, typo – Jodimoro Jun 14 '17 at 3:08
  • Is the filesystem it's located on mounted noexec? – steeldriver Jun 14 '17 at 3:18
  • Windows-style newlines? – Satō Katsura Jun 14 '17 at 5:39
  • Does it need root privileges to execute? Otherwise why use sudo? The environment for the root user is probably different, have you tried sudo -E ./my_script.sh arg1? A good practice when writing scripts that use programs and utilities in non-standard locations is to define the PATH environment in the script or use fully qualified paths to executables called. – Deathgrip Jun 14 '17 at 6:50
2
% sudo ./my_script.sh var1                  
sudo: ./my_script.sh: command not found

This message indicates that either the file ./my_script.sh doesn't exist, or its loader doesn't exist. For a script, the loader is the executable mentioned in the shebang line.

Check that /usr/local/bin/zsh exists and that it isn't a broken symbolic link.

Check that the file really does contain #!/usr/local/bin/zsh as its first line and not, say #!/usr/local/bin/zsh with some whitespace or otherwise invisible characters after it or in the middle. Note in particular that a carriage return is one of the invisible characters that would break things. You would end up with a CR if you created (or perhaps if you edited) the script under Windows; if you edit scripts under Windows, make sure they're saved with line endings set to “LF” or “Unix” or “Linux” or whatever your editor calls it..

% sudo my_script.sh var1 
sudo: my_script.sh: command not found

This one doesn't work for a simple reason: myscript.sh is not in the command search path.

% sudo source my_script.sh var1
sudo: source: command not found

This one doesn't work for a simple reason: source is a shell builtin, it isn't an executable. (It would make no sense as an executable since its purpose is to execute some shell code in the current shell — only a shell can do that.)

Which shell you're currently running is irrelevant for all three commands.

0

The problem is that sudo is neither guaranteed to maintain your current directory or your PATH.

Thus, to be sure scripts will be found, specify the complete path:

sudo /path/to/my_script.sh

The same error also appears for sudo source because source is a shell built-in, not a file. Instead of using a shell, sudo executes its arguments using execev, which expects a file.

For more information, see man sudo (particularly the Process model section and the -E and -s options), and for configuration options see man sudoers and /etc/sudoers.

For many more options about specifying the path to your script with sudo, see https://stackoverflow.com/questions/12996397/command-not-found-when-using-sudo

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.