Bash script error: ./test: line 5: UID: readonly variable

bashscripting

I have a script that works okay on my dev server but when I try to run it in production doesn't work.

I'm getting the following error which I can't figure out:

./test.sh: line 5: UID: readonly variable

Any suggestions?

#!/bin/sh

    while read inputline
    do
     UID="$(echo $inputline | cut -d '"' -f4)"
     PASSWORD="$(echo $inputline | cut -d '"' -f8)"
     FIRST="$(echo $inputline | cut -d '"' -f6 | cut -d ' ' -f1)"
     LAST="$(echo $inputline | cut -d '"' -f6 | cut -d ' ' -f2)"    

    zmprov createAccount $UID $PASSWORD displayName "$FIRST $LAST" givenName $FIRST sn $LAST    

    done < company.csv

Best Answer

It's a good idea to use lowercase variables in your shell scripts, as uppercase variables are usually reserved for shell internals. (Environment variables are an exception to this, but should really be treated the same way, as things with special meanings that you shouldn't arbitrarily change without understanding what you're doing.) In this case, bash sets $UID to the Unix uid it's running under, and won't allow it to be changed or the variable to be used for anything else. Shells other than bash (sh may be dash or some other shell on some platforms) may not use $UID that way, and if bash was built to work in Bourne shell compatibility mode when invoked as sh (which is the default, but many Linux distributions disable that) it won't treat $UID as special either.