[opensuse] awk/bash for password generation
I have a list of usernames and passwords in a csv file, and what I'm trying to do is loop through each user, run the password through htpasswd, and write both to a file for Apache directory restriction. The usernames and passwords are in fields 9 and 10 of the file respectively, and I was trying to do the loop via: for user in $(awk 'BEGIN { FS = ";" } ; {print $9}' user_pw.csv) do password="awk 'BEGIN { FS = ";" } ; {print $10}' user_pw.csv) echo "User $user has password $password" done But that gives an error: bash: syntax error near unexpected token `" } ; {print $10}' user_pw.csv"' I've tried escaping the ";" in different ways, but none of them seem to work. Can anyone suggest a way to get bash to do what I want? -- Pob hwyl / Best wishes Kevin Donnelly www.kyfieithu.co.uk - KDE yn Gymraeg www.eurfa.org.uk - Geiriadur rhydd i'r Gymraeg www.rhedadur.org.uk - Rhedeg berfau Cymraeg www.cymrux.org.uk - Linux Cymraeg ar un CD --------------------------------------------------------------------- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Kevin, On Tuesday 14 November 2006 15:33, Kevin Donnelly wrote:
I have a list of usernames and passwords in a csv file, and what I'm trying to do is loop through each user, run the password through htpasswd, and write both to a file for Apache directory restriction.
The usernames and passwords are in fields 9 and 10 of the file respectively, and I was trying to do the loop via: for user in $(awk 'BEGIN { FS = ";" } ; {print $9}' user_pw.csv) do password="awk 'BEGIN { FS = ";" } ; {print $10}' user_pw.csv)
The outer-most (first) double quote on this line is unclosed. BASH continues on to the next line looking for its mate. If I put a quote at the end of that line, target /etc/passwd, change the FS to ':' and change the field numbers to accommodate that file's format, the script appears to work.
echo "User $user has password $password" done
Randall Schulz --------------------------------------------------------------------- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Kevin, On Tuesday 14 November 2006 16:18, Randall R Schulz wrote:
Kevin,
On Tuesday 14 November 2006 15:33, Kevin Donnelly wrote:
I have a list of usernames and passwords in a csv file, and what I'm trying to do is loop through each user, run the password through htpasswd, and write both to a file for Apache directory restriction.
The usernames and passwords are in fields 9 and 10 of the file respectively, and I was trying to do the loop via: for user in $(awk 'BEGIN { FS = ";" } ; {print $9}' user_pw.csv) do password="awk 'BEGIN { FS = ";" } ; {print $10}' user_pw.csv)
The outer-most (first) double quote on this line is unclosed. BASH continues on to the next line looking for its mate. If I put a quote at the end of that line, target /etc/passwd, change the FS to ':' and change the field numbers to accommodate that file's format, the script appears to work.
echo "User $user has password $password" done
Gack. That was all wrong. Try this: for user in $(awk 'BEGIN { FS = ":" } ; {print $1}' /etc/passwd) do password="$(egrep "^$user" /etc/passwd |awk 'BEGIN { FS = ":" } ; {print $4}')" echo "User $user has password $password" done Of course, that's not very efficient, since it reads all of the password file (in your case, user_pw.csv) for each user, but for one-off purposes, it's adequate. Randall Schulz --------------------------------------------------------------------- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 15 November 2006 00:33, Kevin Donnelly wrote:
I have a list of usernames and passwords in a csv file, and what I'm trying to do is loop through each user, run the password through htpasswd, and write both to a file for Apache directory restriction.
The usernames and passwords are in fields 9 and 10 of the file respectively, and I was trying to do the loop via: for user in $(awk 'BEGIN { FS = ";" } ; {print $9}' user_pw.csv) do password="awk 'BEGIN { FS = ";" } ; {print $10}' user_pw.csv) echo "User $user has password $password" done But that gives an error: bash: syntax error near unexpected token `" } ; {print $10}' user_pw.csv"' I've tried escaping the ";" in different ways, but none of them seem to work.
Can anyone suggest a way to get bash to do what I want?
declare -a foo while IFS=':' read -a foo; do echo user ${foo[8]} has password ${foo[9]} done --------------------------------------------------------------------- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 15 November 2006 01:55, Anders Johansson wrote:
On Wednesday 15 November 2006 00:33, Kevin Donnelly wrote:
I have a list of usernames and passwords in a csv file, and what I'm trying to do is loop through each user, run the password through htpasswd, and write both to a file for Apache directory restriction.
The usernames and passwords are in fields 9 and 10 of the file respectively, and I was trying to do the loop via: for user in $(awk 'BEGIN { FS = ";" } ; {print $9}' user_pw.csv) do password="awk 'BEGIN { FS = ";" } ; {print $10}' user_pw.csv) echo "User $user has password $password" done But that gives an error: bash: syntax error near unexpected token `" } ; {print $10}' user_pw.csv"' I've tried escaping the ";" in different ways, but none of them seem to work.
Can anyone suggest a way to get bash to do what I want?
declare -a foo
while IFS=':' read -a foo; do echo user ${foo[8]} has password ${foo[9]} done
oops, forgot to actually read something. How about declare -a foo while IFS=':' read -a foo; do echo user ${foo[8]} has password ${foo[9]} done < user_pw.csv --------------------------------------------------------------------- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 15 November 2006 00:57, Anders Johansson wrote:
oops, forgot to actually read something. How about declare -a foo while IFS=':' read -a foo; do echo user ${foo[8]} has password ${foo[9]} done < user_pw.csv
Thanks to you and Randall. Unfortunately, neither of these worked (I was entering the various suggestions on one command-line, but that shouldn't really make a difference, should it?). In the event I just added the info by hand. I'll have to look into awk in more detail. -- Pob hwyl / Best wishes Kevin Donnelly www.kyfieithu.co.uk - KDE yn Gymraeg www.eurfa.org.uk - Geiriadur rhydd i'r Gymraeg www.rhedadur.org.uk - Rhedeg berfau Cymraeg www.cymrux.org.uk - Linux Cymraeg ar un CD -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 21 November 2006 14:41, Kevin Donnelly wrote:
On Wednesday 15 November 2006 00:57, Anders Johansson wrote:
oops, forgot to actually read something. How about declare -a foo while IFS=':' read -a foo; do echo user ${foo[8]} has password ${foo[9]} done < user_pw.csv
Thanks to you and Randall. Unfortunately, neither of these worked (I was entering the various suggestions on one command-line, but that shouldn't really make a difference, should it?). In the event I just added the info by hand. I'll have to look into awk in more detail.
It works, I actually tried it before I sent it. On a single command line, it would look like while IFS=';' read -a foo; do echo user ${foo[8]} has password ${foo[9]}; done < user_pw.csv When you tried it, did you make sure you typed everything correctly? Don't bother with awk for simple things like this, as my example above shows, bash can do it all far simpler and easier -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 21 November 2006 14:07, Anders Johansson wrote:
It works, I actually tried it before I sent it. On a single command line, it would look like while IFS=';' read -a foo; do echo user ${foo[8]} has password ${foo[9]}; done < user_pw.csv When you tried it, did you make sure you typed everything correctly?
Yes, I cut and pasted (and also changed the column numbers to the correct ones). I get the following: kevin@baby:~/data/websites/paid/wps_aelodau_dynamic/assets/wps> while IFS=';' read -a foo; do echo user ${foo[9]} has password ${foo[10]}; done user has password user has password user has password user has password user has password That is, every time I press Return I get the "user has password" line. It's obviously not reading the contents of the file.
Don't bother with awk for simple things like this, as my example above shows, bash can do it all far simpler and easier.
OK. -- Pob hwyl / Best wishes Kevin Donnelly www.kyfieithu.co.uk - KDE yn Gymraeg www.eurfa.org.uk - Geiriadur rhydd i'r Gymraeg www.rhedadur.org.uk - Rhedeg berfau Cymraeg www.cymrux.org.uk - Linux Cymraeg ar un CD -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 21 November 2006 15:30, Kevin Donnelly wrote:
On Tuesday 21 November 2006 14:07, Anders Johansson wrote:
It works, I actually tried it before I sent it. On a single command line, it would look like while IFS=';' read -a foo; do echo user ${foo[8]} has password ${foo[9]}; done < user_pw.csv When you tried it, did you make sure you typed everything correctly?
Yes, I cut and pasted (and also changed the column numbers to the correct ones). I get the following:
Note that the numbering starts at 0, you said columns 9 and 10, so the indexes should be 8 and 9 as I had them.
kevin@baby:~/data/websites/paid/wps_aelodau_dynamic/assets/wps> while IFS=';' read -a foo; do echo user ${foo[9]} has password ${foo[10]}; done
user has password
user has password
user has password
user has password
user has password
That is, every time I press Return I get the "user has password" line. It's obviously not reading the contents of the file.
You don't seem to read what I wrote. that "< filename" I had at the end wasn't a typo, that is how it reads the file. Try it again, and this time don't leave anything out -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 21 November 2006 15:54, Anders Johansson wrote:
On Tuesday 21 November 2006 15:30, Kevin Donnelly wrote: Note that the numbering starts at 0, you said columns 9 and 10, so the indexes should be 8 and 9 as I had them.
Yes, point taken ...
You don't seem to read what I wrote. that "< filename" I had at the end wasn't a typo, that is how it reads the file. Try it again, and this time don't leave anything out
I know! I am donning sackcloth as we speak :-) -- Pob hwyl / Best wishes Kevin Donnelly www.kyfieithu.co.uk - KDE yn Gymraeg www.eurfa.org.uk - Geiriadur rhydd i'r Gymraeg www.rhedadur.org.uk - Rhedeg berfau Cymraeg www.cymrux.org.uk - Linux Cymraeg ar un CD -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Kevin, On Tuesday 21 November 2006 13:39, Kevin Donnelly wrote:
On Tuesday 21 November 2006 15:54, Anders Johansson wrote:
On Tuesday 21 November 2006 15:30, Kevin Donnelly wrote: Note that the numbering starts at 0, you said columns 9 and 10, so the indexes should be 8 and 9 as I had them.
Yes, point taken ...
You don't seem to read what I wrote. that "< filename" I had at the end wasn't a typo, that is how it reads the file. Try it again, and this time don't leave anything out
I know! I am donning sackcloth as we speak :-)
As a general rule, all but the very simplest one-off commands should not be entered directly to an interactive command line. Instead, create a script and work on the invocation there. Likewise, even if you think a command should be simple enough to enter directly to the shell prompt, if you don't get it right after a couple of tries, put it in a script. Lastly, if there's even an outside chance that the command will prove useful again in the future, stash it away to save time the next time you need to do the same thing or something similar.
--
Kevin Donnelly
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 21 November 2006 22:00, Randall R Schulz wrote:
As a general rule, all but the very simplest one-off commands should not be entered directly to an interactive command line. Instead, create a script and work on the invocation there. Likewise, even if you think a command should be simple enough to enter directly to the shell prompt, if you don't get it right after a couple of tries, put it in a script. Lastly, if there's even an outside chance that the command will prove useful again in the future, stash it away to save time the next time you need to do the same thing or something similar.
Great advice, Randall - I'll use it from now on. -- Pob hwyl / Best wishes Kevin Donnelly www.kyfieithu.co.uk - KDE yn Gymraeg www.eurfa.org.uk - Geiriadur rhydd i'r Gymraeg www.rhedadur.org.uk - Rhedeg berfau Cymraeg www.cymrux.org.uk - Linux Cymraeg ar un CD -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Kevin, On Tuesday 21 November 2006 05:41, Kevin Donnelly wrote:
...
Thanks to you and Randall. Unfortunately, neither of these worked (I was entering the various suggestions on one command-line, but that shouldn't really make a difference, should it?). In the event I just added the info by hand. I'll have to look into awk in more detail.
Which shell are you using? When you put the commands in a script, what shebang line do you use? If it looks like this: #!/bin/sh then you'll get BASH emulating the Bourne Shell, which does not have many of the feature of BASH, some of which are used in the various examples we gave. I recommend this: #!/bin/bash --norc The "--norc" speeds start-up and makes the script immune from interference from settings and definitions established or changed in your ~/.bashrc file.
-- Pob hwyl / Best wishes
Kevin Donnelly
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
Anders Johansson
-
Anders Johansson
-
Kevin Donnelly
-
Randall R Schulz