Ubuntu: (Bad Variable: Line 36) Please Help!



Question:

What have I done wrong here?, cause when I run my script and stop it after I set the variable, it says I have a bad variable

qmail.script: 36: read: : bad variable name.   

Help would be much appreciated.

unset PASSWORD  unset CHARCOUNT  echo  echo -n "Please Enter a Password For VqAdmin, Roundcube and QmailAdmin.  Password: "  stty echo  CHARCOUNT=0  while IFS= read "$PROMPT" -p -r -s -n   do      #Enter - accept password      if [[ $CHAR == $'\0' ]] ; then          break      fi      #Backspace      if [[ $CHAR == $'\177' ]] ; then          if [ $CHARCOUNT -gt 0 ] ; then              CHARCOUNT=$((CHARCOUNT-1))              PROMPT=$'\b \b'              PASSWORD="${PASSWORD%?}"          else              PROMPT=''          fi      else          CHARCOUNT=$((CHARCOUNT+1))          PROMPT='*'          PASSWORD=$CHAR      fi  done  stty echo  


Solution:1

I take it you're not showing all of the code, so line 36 is really the read statement from the snippet you posted.

The read statement is wrong. IMHO you mean

read -p "$PROMPT" -r -s -n1 CHAR  
  • $PROMPT is the parameter for -p, either printing a '*' or nothing at input.
  • -n needs a parameter too, telling how many characters to read at once.
  • The variable $CHAR is used later, but it's never assigned to by read

Also $PASSWORD will contain only the last character, since you forgot to concatenate: PASSWORD=$CHAR should rather be

PASSWORD+=$CHAR  

or

PASSWORD=${PASSWORD}${CHAR}  

With this corrections, the code does what I think it's supposed to: Present a 'starred' password prompt.


Upon request here the complete and corrected code snippet
unset PASSWORD  unset CHARCOUNT  echo  echo -n "Please Enter a Password For VqAdmin, Roundcube and QmailAdmin.  Password: "  stty echo  CHARCOUNT=0  while IFS= read -p "$PROMPT" -r -s -n1 CHAR   do      #Enter - accept password      if [[ $CHAR == $'\0' ]] ; then          break      fi      #Backspace      if [[ $CHAR == $'\177' ]] ; then          if [ $CHARCOUNT -gt 0 ] ; then              CHARCOUNT=$((CHARCOUNT-1))              PROMPT=$'\b \b'              PASSWORD="${PASSWORD%?}"          else              PROMPT=''          fi      else          CHARCOUNT=$((CHARCOUNT+1))          PROMPT='*'          PASSWORD+=$CHAR      fi  done  

Judging from your comment, you did not write this code yourself (entirely) (?) and have some trouble understanding it. I won't explain to you line-by-line what it does. Sorry. I suggest you read around the web and come back with a specific questions should they arise. A good explanation of read and it's options is for example given here.

General introductions to bash programming can be found for example at


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »