0

I'm trying to make a bash script that checks if a binary has the executable bit and if it doesn't, it automatically adds it.

When i execute the script i get the following error: script/repair.sh: 37: chmod: not found.

Here is the code:

PATH="/my/path/"
BIN="mybin"

if chmod +x $PATH$BIN != -1; then
    echo "Added the executable bit to '$BIN'"
else
    echo "Couldn't add the executable bit to '$BIN'"  
fi
  • The problem is caused by setting the special [PATH](https://www.gnu.org/software/bash/manual/bash.html#index-PATH) variable. There are many such special variables. All of them have `ALL_UPPERCASE` names. Best practice is to ensure that all of your own variables have at least one lowercase letter in their names to avoid clashes with the special variables. See [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/4154375). In this case changing `PATH` to `path` or `Path` (for example) would fix the problem. – pjh May 05 '22 at 10:09

1 Answers1

0

You are changing the value of "PATH" which is a shell environment variable used to search / execute files so it can no longer find the bin chmod.

#!/bin/bash

P="/home/user/"
BIN="a.out"

if chmod +x "$P$BIN"; then
    echo "Added the executable bit to '$BIN'"
else
    echo "Couldn't add the executable bit to '$BIN'"
fi
rabbit
  • 95
  • 7