Re: New line in bash variables pain

2006-11-14 Thread Oron Peled
On Tuesday, 14 בNovember 2006 22:31, Maxim Vexler wrote:
> Thanks to everyone for the help, all solution worked.
> To sum up the tips:

Hey, what's the rush? I didn't have my take yet ;-)

Let's do it in simple one liner:

  sed -e N -e 's/\n/ = /' passwd.fake

Cheers,

-- 
Oron Peled Voice/Fax: +972-4-8228492
[EMAIL PROTECTED]  http://www.actcom.co.il/~oron
ICQ UIN: 16527398

"Beware of bugs in the above code;
 I have only proved it correct, not tried it."
  -- Donald E. Knuth

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: New line in bash variables pain

2006-11-14 Thread michael

This was a very educational thread. Thanks for posing the question and the
summary, and thanks everyone for the useful answers. I don't have a need for
this just yet, but I have saved the thread as I am sure the answers will be
useful sooner or later.

I eagerly await the next educational thread. Who's going to ask the question?

Michael



On Tue, 14 Nov 2006, Maxim Vexler wrote:


On 11/14/06, Oded Arbel <[EMAIL PROTECTED]> wrote:
[snip]


 (IFS="$(echo)"; \
 for pair in `awk '/^[^[].+[^\n]$/ {print $1,$3}' passwd.fake`; do
 echo "$pair"; done)

 In the second example, I force the record separator to be only the new
 line character (the output from 'echo'. I can probably use \n, but I
 wanted to play it safe). Do mind the wrapping of the second form in
 parenthesis, otherwise you clobber your global IFS, which is something
 you want to avoid.

 --
 Oded
: : ..
 We make a living by what we get, but we make a life by what we give.
 -- Winston Churchill



Thanks to everyone for the help, all solution worked.
To sum up the tips:

By Oded Arbel:
a. Use a subshell to avoid mistakenly over riding your shell variables.
b. Use "$(echo)" as portable(?) newline variable scripting style.

By Ehud Karni:
a. Pipeing into bash subshell can be accepted inside the shell with read.
b. using a "while read VAR1 VAR2 VAR3..." is a convenient method to
accepting stdin data.
c. awk has system() !!

By Amos Shapira:
a. General work around is to construct the whole command as text, then
use either piping to sh or bash buildin "expr".

By Omer Shapira:
a. xargs -n switch can be used to "collect" variables separated by
either of [\n\t ].

By Valery Reznic:
a. set -- "space delimited word list" can be used as a quick method
for assigning value to number variables ($1..$9). [question: Really?
this does not seem to work for me].
b. bash while loop can get stdin from file IO redirection.

Ariel Biener doesn't understand the need for voodoo in modern life... ;)


Thanks guys for an educational thread.
Maxim.

--
Cheers,
Maxim Vexler

"Free as in Freedom" - Do u GNU ?

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]





=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: New line in bash variables pain

2006-11-14 Thread Maxim Vexler

On 11/14/06, Oded Arbel <[EMAIL PROTECTED]> wrote:
[snip]


(IFS="$(echo)"; \
for pair in `awk '/^[^[].+[^\n]$/ {print $1,$3}' passwd.fake`; do
echo "$pair"; done)

In the second example, I force the record separator to be only the new
line character (the output from 'echo'. I can probably use \n, but I
wanted to play it safe). Do mind the wrapping of the second form in
parenthesis, otherwise you clobber your global IFS, which is something
you want to avoid.

--
Oded
::..
We make a living by what we get, but we make a life by what we give.
-- Winston Churchill



Thanks to everyone for the help, all solution worked.
To sum up the tips:

By Oded Arbel:
a. Use a subshell to avoid mistakenly over riding your shell variables.
b. Use "$(echo)" as portable(?) newline variable scripting style.

By Ehud Karni:
a. Pipeing into bash subshell can be accepted inside the shell with read.
b. using a "while read VAR1 VAR2 VAR3..." is a convenient method to
accepting stdin data.
c. awk has system() !!

By Amos Shapira:
a. General work around is to construct the whole command as text, then
use either piping to sh or bash buildin "expr".

By Omer Shapira:
a. xargs -n switch can be used to "collect" variables separated by
either of [\n\t ].

By Valery Reznic:
a. set -- "space delimited word list" can be used as a quick method
for assigning value to number variables ($1..$9). [question: Really?
this does not seem to work for me].
b. bash while loop can get stdin from file IO redirection.

Ariel Biener doesn't understand the need for voodoo in modern life... ;)


Thanks guys for an educational thread.
Maxim.

--
Cheers,
Maxim Vexler

"Free as in Freedom" - Do u GNU ?

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: New line in bash variables pain

2006-11-14 Thread Ariel Biener
On Tuesday 14 November 2006 12:34, Ehud Karni wrote:

I don't understand why all this voodoo is needed. If you have a list
of spaced delimited values and want to use a for or while loop to
read them, just fix $IFS locally (the default of IFS is tab or space or
newline). You can make $IFS only be newline for the local process
(IFS=something; your for loop here), and it will work.

If that is too much (man bash), then you can just use awk. I am not sure
why the '/^[^[].+[^\n]$/' gives you what you want, since you have not
said much about your input (except a hint that it may be in the shape
of user = password). More information about your input is needed in
order to formulate the right awk recipe for you.

--Ariel
 --
 Ariel Biener, CISO
 Tel-Aviv University CIT div.
 e-mail: [EMAIL PROTECTED] phone: 03-6406086
 PGP key:http://www.tau.ac.il/~ariel/pgp.html

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: New line in bash variables pain

2006-11-14 Thread Valery Reznic
What about something like following:

while read line; do
case "x$line" in
   x)
   # empty line, do nothing
   ;; 

   x[ | x])
   # you don't like brackets, do nothing too
   ;;

   *)
   # Everything else
   set -- $line
   # Now $1 == user, $3 == passwd (if any)
   # do whatever you like with them
   ;;
esac
done < passwd.fake


Valery
--- Maxim Vexler <[EMAIL PROTECTED]> wrote:

> Hi list, any bash gurus in the house ?
> 
> I'm having the most annoying issue with bash, one
> related to space
> delimited variables.
> I'd like to get a list in the form of :
> <<<
> user1 password1
> user2 password2
> >>>
> 
> Instead I'm getting:
> <<<
> user1
> password1
> user2
> password2
> >>>
> 
> Here's an example:
> 
> san-svn:/var/lib/svn# cat passwd.fake
> [users]
> user1 = password1
> user2 = password2
> 
> 
> san-svn:/var/lib/svn#
> 
> I'd like to automate this the import from this file
> into something like
> san-svn:# htpasswd -b passwd.real user1 password1
> 
> For this I've tried this voodoo:
> 
> san-svn:/var/lib/svn# for pair in `awk
> '/^[^[].+[^\n]$/ {print $1,
> $3}' passwd.fake`; do htpasswd -b passwd.true
> "$pair"; done
> 
> This does not work for the following reason:
> 
> san-svn:/var/lib/svn# for pair in `awk
> '/^[^[].+[^\n]$/ {print $1,
> $3}' passwd.fake`; do echo "$pair"; done
> user1
> password1
> user2
> password2
> 
> 
> I've tried the following workarounds, that didn't
> worked:
> 
> san-svn:/var/lib/svn# IFS='\n' for pair in `awk
> '/^[^[].+[^\n]$/
> {print $1, $3}' passwd`; do echo $pair; done
> -bash: syntax error near unexpected token `do'
> 
> san-svn:/var/lib/svn# IFS='!\n' for pair in `awk
> '/^[^[].+[^\n]$/
> {print $1, $3}' passwd`;! do echo $pair;! done
> -bash: syntax error near unexpected token `do'
> 
> san-svn:/var/lib/svn# for pair in `awk
> '/^[^[].+[^\n]$/ {print $1,
> $3}' passwd.fake`; do xargs "$pair" | echo; done
> 
> san-svn:/var/lib/svn# for pair in `awk
> '/^[^[].+[^\n]$/ {print $1,
> $3}' passwd.fake`; do xargs "$pair" | echo -; done
> -
> 
> 
> I did found the following work around :
> <<<
> san-svn:/var/lib/svn# for pair in `awk
> '/^[^[].+[^\n]$/ {print
> $1"_"$3}' passwd.fake`; do echo "$pair" | tr  _ ' '
> | cat; done
> user1 password1
> user2 password2
> >>>
> 
> But it's broken because "_" can be a valid character
> in a password /
> usernmae name and besides - I'd to find a smarter
> solution.
> 
> Any help / pokes to right direction would be highly
> appreciated.
> 
> Thank you,
> Maxim.
> 
> -- 
> Cheers,
> Maxim Vexler
> 
> "Free as in Freedom" - Do u GNU ?
> 
>
=
> To unsubscribe, send mail to
> [EMAIL PROTECTED] with
> the word "unsubscribe" in the message body, e.g.,
> run the command
> echo unsubscribe | mail
> [EMAIL PROTECTED]
> 
> 



 

Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: New line in bash variables pain

2006-11-14 Thread Maxim Vexler

On 11/14/06, Maxim Vexler <[EMAIL PROTECTED]> wrote:
[snip]


san-svn:/var/lib/svn# for pair in `awk '/^[^[].+[^\n]$/ {print $1,
$3}' passwd.fake`; do xargs "$pair" | echo; done

san-svn:/var/lib/svn# for pair in `awk '/^[^[].+[^\n]$/ {print $1,
$3}' passwd.fake`; do xargs "$pair" | echo -; done
-


Correction:
The xargs line should look some thing like:

san-svn:/var/lib/svn# for pair in `awk '/^[^[].+[^\n]$/ {print $1,$3}'
passwd.fake`; do echo "$pair" | xargs echo ; done
user1
password1
user2
password2

But as you can see, it still does not give the request output.

--
Cheers,
Maxim Vexler

"Free as in Freedom" - Do u GNU ?

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: New line in bash variables pain

2006-11-14 Thread Ehud Karni
On Tue, 14 Nov 2006 11:56:18 Maxim Vexler wrote:
>
> I'm having the most annoying issue with bash, one related to space
> delimited variables.
 [snip]
>
> Here's an example:
>
> san-svn:/var/lib/svn# cat passwd.fake
> [users]
> user1 = password1
> user2 = password2
>
 [snip]
>
> I'd like to automate this the import from this file into something like
> san-svn:# htpasswd -b passwd.real user1 password1


Your using the awk/bash/xarg combination in a wrong way.

You can do in bash ALONE very simply like this:

PWF="your-fake-pass-file"

cat $PWF | (
while read USR EQ PAS REST
do
if [ "$EQ" = "=" ] ; then
   ##  echo "$USR $PAS"# just to get pairs
   htpasswd -b passwd.real $USR $PAS   # what you really want
fi
done  )


Or, you can do by awk ALONE like this:

PWF="your-fake-pass-file"
awk '/^[^[].+[^\n]$/ { system( \
"htpasswd -b passwd.real " $1 " " $3 ) }' $PWF


Ehud.


--
 Ehud Karni   Tel: +972-3-7966-561  /"\
 Mivtach - Simon  Fax: +972-3-7966-667  \ /  ASCII Ribbon Campaign
 Insurance agencies   (USA) voice mail and   X   Against   HTML   Mail
 http://www.mvs.co.il  FAX:  1-815-5509341  / \
 GnuPG: 98EA398D Better Safe Than Sorry

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: New line in bash variables pain

2006-11-14 Thread Amos Shapira
On 14/11/06, Maxim Vexler <[EMAIL PROTECTED]> wrote:
Hi list, any bash gurus in the house ?I'm having the most annoying issue with bash, one related to spacedelimited variables.I'd like to get a list in the form of :<<>>Instead I'm getting:<<>>Here's an example:san-svn:/var/lib/svn# cat passwd.fake[users]user1 = password1
user2 = password2san-svn:/var/lib/svn#I'd like to automate this the import from this file into something likesan-svn:# htpasswd -b passwd.real user1 password1Two ways I could think of almost stright away:
1.# awk '/^[^[].+[^\n]$/ {print "htpasswd -b passwd.real", $1, $3 }' passwd.fake | sh2.# awk '/^[^[].+[^\n]$/ {print $1, $3 }' passwd.fake | xargs -n 2 htpasswd -b passwd.real
Maybe it's cheating but it should do the job... :)Thanks for the quiz,--Amos


Re: New line in bash variables pain

2006-11-14 Thread Oded Arbel
On Tue, 2006-11-14 at 12:05 +0200, Maxim Vexler wrote:
> On 11/14/06, Maxim Vexler <[EMAIL PROTECTED]> wrote:
> san-svn:/var/lib/svn# for pair in `awk '/^[^[].+[^\n]$/ {print $1,$3}'
> passwd.fake`; do echo "$pair" | xargs echo ; done
> user1
> password1
> user2
> password2

I think you are approaching this the wrong way, and you should use $IFS
(bash record separator characters) for this purpose. Compare this:

for pair in `awk '/^[^[].+[^\n]$/ {print $1,$3}' passwd.fake`; do 
echo "$pair"; done

versus

(IFS="$(echo)"; \
for pair in `awk '/^[^[].+[^\n]$/ {print $1,$3}' passwd.fake`; do
echo "$pair"; done)

In the second example, I force the record separator to be only the new
line character (the output from 'echo'. I can probably use \n, but I
wanted to play it safe). Do mind the wrapping of the second form in
parenthesis, otherwise you clobber your global IFS, which is something
you want to avoid.

--
Oded
::..
We make a living by what we get, but we make a life by what we give.
-- Winston Churchill



=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]