-3

I have a script that starts like this:

source /etc/os-release
if [ $NAME = 'Ubuntu' ]

And it runs fine as a normal user. However, if I invoke it like:

$ sudo ./install.sh

I get the following error:

./install.sh: 9: ./install.sh: source: not found

./install.sh: 10: [: =: unexpected operator

Why does that occur when in sudo mode, and what's required to fix that?

David542
  • 104,438
  • 178
  • 489
  • 842

2 Answers2

1

Read the documentation of sudo(8) and of execve(2) (used by sudo) and of bash(1). Read also the documentation of GNU bash (or of your interactive Unix shell, perhaps zsh or fish; use chsh(1) to change it)

Either your ./install.sh script (starting with a shebang) should be made executable with chmod(1), or you explicitly need to invoke /bin/bash -or /bin/sh- to run it (and have it being readable).

So try chmod a+x ./install.sh before sudo ./install.sh or else run sudo /bin/bash ./install.sh

I recommend taking several days to read Advanced Linux Programming then syscalls(2) and credentials(7)

You could also study for inspiration and self teaching the source code of simple shells (most of them are open source) such as sash, or of GNU bash or at least use strace(1) or ltrace(1) or gdb(1) to understand their runtime behavior.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0
Sudo chmod a+x install.sh
Sudo ./install.sh

Basically the install.sh needs the correct permissions.

Chmod : change mode permissions of the file a : for all users +x: add execute permissons

hamadkh
  • 359
  • 1
  • 16
  • 1
    If that was the issue then OP would see an *Operation not permitted* error, not *source: not found* and the like. – oguz ismail Jul 17 '20 at 05:08