1

So I've got a shell script to do some lazy stuff for if the directory isn't changing for a user. It's below. Essentially, it should be an if statement that if the user enters "default" for the directory, it'll pull them to the default directory for the files. However, I'm getting a command not found on line 16, which is the reassignment statement.

The entire if statement below:

if [ $directory = "default" ];
then 
    echo Enter your ldap:
    read $ldap
    $directory = "/usr/local/home/google/${ldap}/Downloads"
fi

I've tried doing it without the dollar sign too...nothing. What's going on here? New to shell, couldn't find this question asked before either.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Travis
  • 55
  • 8

1 Answers1

1

This is how you should assign a value to a variable in shell:

directory="/usr/local/home/google/${ldap}/Downloads"
  • No dollar ($) sign.
  • No space around equal (=) sign.

Also, you should wrap your variables inside double quotes ("). This way, you avoid errors with undefined variables, arguments with spaces, etc.


That gives us:

if [ "$directory" = "default" ]
then
    echo "Enter your ldap:"
    read $ldap
    directory="/usr/local/home/google/${ldap}/Downloads"
fi
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • Didn't know spaces affected this - included spaces for readability. Thank you! – Travis Mar 26 '18 at 17:14
  • @RonanBoiteau, see the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer), third bullet point, regarding questions which "have been asked and answered many times before". – Charles Duffy Mar 26 '18 at 17:20