[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash array



On Thu, Jan 25, 2018 at 03:35:35PM +0300, Gokan Atmaca wrote:
> The problem is: I can pull the users one by one. I can not do the same
> for passwords.

Why not?

> Do I need to use "Mapfile" for this?

How would we know?  You haven't told us what is in the files, or what
you want to DO with the contents of the files.

> passwords=(cat $pass.list)

Is wrong.

> mapfile -t pass < "$passwords"

Would be OK if $passwords contains the NAME of the file.  Is WRONG if
$passwords is a string containing the CONTENTS of the file.

Is also WRONG if passwords is an array containing the word "cat" and the
expansion of "$pass" with the string .list appended to it, which is what
your previous line created.

> example-script:
>    -- user.list
>       a
>       b
>       c
> 
>   -- pass.list
>      xxx
>      yyy
>      zzz

With all that whitespace?  Or should I just assume that the "xxx" is
NOT indented as shown, but is instead an entire line?

What do you want to DO with these files?

> script:
> 
> #!/bin/bash
> 
> 
> pass=$(cat pass.list)

Now, you see, this is ENTIRELY DIFFERENT from what you showed up above.

Do you see the difference?  Look at the two lines again:

> passwords=(cat $pass.list)

> pass=$(cat pass.list)

If you can't see the difference, you can't write computer programs.

Moving on....

> for i in user=$(cat user.list); do exampleporagram $i $pass; done

Syntax errors.  Logic errors.  Cannot even make SENSE of this.

Are you trying to read ONE USERNAME and ONE PASSWORD at a time, and
pass each PAIR to your "exampleprogram"?


> On Mon, Jan 15, 2018 at 5:39 PM, Greg Wooledge <wooledg@eeg.ccf.org> wrote:
> > What kind of array do you want?  Why do you even want an array?
> >
> > What is your program supposed to do with these users and passwords?

Can't you just write ONE SENTENCE to tell us the objective of the script?

Let's go out on a limb and assume "I want to read one username and
one password at a time, and pass them as a pair of arguments to
'exampleprogram'."

The way you would do that is to open the two files, and then read a
line at a time from each file, and then close the files.

Like this:

===========================================================
#!/bin/sh
exec 3< "user.list"
exec 4< "pass.list"

while IFS= read -r user <&3 && IFS= read -r pass <&4
do
  exampleprogram "$user" "$pass"
done

exec 3<&-
exec 4<&-
===========================================================


See the following pages:

http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/FileDescriptor
http://mywiki.wooledge.org/BashFAQ/001

Then read this page again, because I know you didn't read it the
first time:

http://mywiki.wooledge.org/Quotes

Then keep reading it over and over until you UNDERSTAND it.


You do not need an array for this.  You don't even need bash features.
I used pure POSIX sh up there, hence the #!/bin/sh shebang.


P.S. read http://mywiki.wooledge.org/Quotes

Also, be sure you quote properly.

I'm serious.

Quotes.

They are NOT optional.


Reply to: