0

I'm just starting with some scripts with the bash. Now I got the following errors:

  • 0=1: command not found on line 9.
  • Home * Error: command not found on line 17

I want to check with my script if the home-directory and later some other directories exist and have the right permission.

I have already searched with Google and on Stack Overflow but I didn't find an answer. First I had too many spaces between the variables and their assigned value. This error I think I fixed but still there are other errors for which I didn't find an answer.

Thanks for you help and here you can see my code from the script:

#!/bin/bash
users=(Hans Peter Alfred Georg Stefan Albert Christine)
for user in ${users[@]};
do

   nutzerVerzeichnis="0"
   if [ -d "/home/${user}" ]
   then
           ${nutzerVerzeichnis}="1"
           echo $home_dir
   fi

   if [ "${nutzerVerzeichnis}" -eq "1" ]
   then
           "Home ${user} ok">>/root/skript/permission.log
   else
           "Home ${user} Error">>/root/skript/permission.log
   fi
done
exit 0
jww
  • 97,681
  • 90
  • 411
  • 885
  • 4
    `${nutzerVerzeichnis}="1"` -> `nutzerVerzeichnis="1"` – Mad Physicist Apr 29 '18 at 22:45
  • 2
    Also missing `echo` on the other lines – Mat Apr 29 '18 at 22:45
  • You're dereferencing the left hand side. – Mad Physicist Apr 29 '18 at 22:45
  • 3
    You don't need to add ${} when you assign a value: `nutzerVerzeichnis="1"` – Michael Apr 29 '18 at 22:45
  • 1
    Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Apr 29 '18 at 22:56

1 Answers1

0

In this line:

${nutzerVerzeichnis}="1"

You are telling it to evaluate to the value of nutzerVerzeichnis which is 0. Therefore it is the same as writing.

0=1

if your trying to assign it a value instead then leave off the ${} to this

nutzerVerzeichnis="1"

There are also a few other mistakes in your code. Here is the corrected code.

#!/bin/bash
users=(Hans Peter Alfred Georg Stefan Albert Christine)
for user in "${users[@]}";
do
    nutzerVerzeichnis="0"
    if [ -d "/home/${user}" ]
    then
            nutzerVerzeichnis="1"
            echo "$home_dir"
    fi

    if [ "${nutzerVerzeichnis}" -eq "1" ]
    then
            echo "Home ${user} ok" >> /root/skript/permission.log
    else
            echo "Home ${user} Error" >> /root/skript/permission.log
    fi
done
exit 0
chepner
  • 497,756
  • 71
  • 530
  • 681