You have the syntax of your while...do...done loop wrong. You are running this:
while read x ; OneTimePass=$(openssl rand -base64 14 | head -c 6) ; do ...
The while keyword's expected format is explained in help while:
$ help while
while: while COMMANDS; do COMMANDS; done
Execute commands as long as a test succeeds.
Expand and execute COMMANDS as long as the final command in the
`while' COMMANDS has an exit status of zero.
Exit Status:
Returns the status of the last command executed.
Here, you are giving it two commands: read x ; and OneTimePass=$(openssl rand -base64 14 | head -c 6), and the second command always works, it doesn't have any ending since you can always re-run the openssl command. This is why the while loop never exits and you just keep writing more and more lines. What you're after is this:
while read x; do
something
done
Here's a working version of your script with some other improvements such as proper quoting and avoiding CAPS for your variable names:
#!/bin/bash
#add column to csv
orig_file="new-users2.csv"
new_file="Output.csv"
printf "%s,%s\n" "$(head -1 "$orig_file")" "One Time Password" > "$new_file"
tail -n +2 "$orig_file" |
while read -r x; do
OneTimePass="$(openssl rand -base64 14 | head -c 6)"
printf '%s,%s\n' "$x" "$OneTimePass"
done >> "$new_file"
Personally, I would avoid hardcoding the file names since that makes the script harder to use and less versatile. I would instead take the input file name as an argument, and then print to standard output so you can choose whatever output file you want:
#!/bin/bash
#add column to csv
orig_file=$1
printf "%s,%s\n" "$(head -1 "$orig_file")" "One Time Password"
tail -n +2 "$orig_file" |
while read -r x; do
OneTimePass="$(openssl rand -base64 14 | head -c 6)"
printf '%s,%s\n' "$x" "$OneTimePass"
done
You can then run it like this:
foo.sh new-users2.csv > Output.csv
I tested with this input file:
$ cat new-users2.csv
name,age
Bob,45
Alice,36
Which results in:
$ foo.sh new-users2.csv
name,age,One Time Password
Bob,45,BTkLQW
Alice,36,CzQa4U
https://shellcheck.net, a syntax checker, or installshellchecklocally. Make usingshellcheckpart of your development process.