0

I am struggling to get a piece of my code working. I have a database with usernames in it and the script automatically chooses the next available username. For example input is JoeBlogs but JoeBlogs is already taken so JoeBlogs1 is used, then JoeBlogs2 etc. The database contains the list of used usernames so I am trying to get it to put the next number availible after the given name.

testdb=`cat test.db`
cd "$(dirname "$0")"
ftpfullname="JoeBlogs"
for output in $testdb
do
    if [ "$ftpfullname" == "$output" ]
    then
        ftpfullname="${ftpfullname}1"
    fi
done
echo "$ftpfullname has been selected"

test.db is very simple, just a bunch of names stored on their own line.

I understand why it's not working I just can't work out how to do it.

  • It seems your for loop is not incrementing. Put an printf instruction within it to see if it's working (As printf $output by example) – Redbob Feb 2 '18 at 2:30
  • It would help if you tell us your understanding of why it's not working. – wjandrea Feb 2 '18 at 2:33
  • Tell us how your test.db is constructed / updated, and be more specific than "its not working" – steeldriver Feb 2 '18 at 2:33
  • 1
    Please add your solution as an answer below (just click edit and copy & paste to preserve the formatting) and accept it by clicking the tick next to it. – dessert Feb 2 '18 at 7:36
  • 2
    I agree with @dessert: please move the solution from your question post to a proper answer. I'm taking the liberty to remove it from the question. You can still access it through the edit history. – David Foerster Feb 2 '18 at 12:32
0
accountsdb=`cat test.db | sort`
namex="0"
nameplus=0
ftpfullname="turner"
for output in $accountsdb
do
  if [ "$ftpfullname" == "$output" ]
  then
    let "nameplus += 1"
    for i in $nameplus
    do
      echo "$nameplus"
      echo "$i"
      if [ "$namex" == "1" ]
      then
        ftpfullname=${ftpfullname%?}
      fi
      namex="1"
      ftpfullname="${ftpfullname}${i}"
      break
    done
  fi
done
echo "$ftpfullname"
echo $namex
echo "$ftpfullname" >> test.db

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.