Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #191060 > unrolled thread

bash array

Started byGokan Atmaca <linux.gokan@gmail.com>
First post2018-01-14 13:50 +0100
Last post2018-01-25 15:00 +0100
Articles 6 — 3 participants

Back to article view | Back to linux.debian.user


Contents

  bash array Gokan Atmaca <linux.gokan@gmail.com> - 2018-01-14 13:50 +0100
    Re: bash array <tomas@tuxteam.de> - 2018-01-14 15:10 +0100
    Re: bash array Greg Wooledge <wooledg@eeg.ccf.org> - 2018-01-15 15:50 +0100
      Re: bash array Gokan Atmaca <linux.gokan@gmail.com> - 2018-01-25 13:40 +0100
        Re: bash array Greg Wooledge <wooledg@eeg.ccf.org> - 2018-01-25 14:40 +0100
          Re: bash array Gokan Atmaca <linux.gokan@gmail.com> - 2018-01-25 15:00 +0100

#191060 — bash array

FromGokan Atmaca <linux.gokan@gmail.com>
Date2018-01-14 13:50 +0100
Subjectbash array
Message-ID<v7PTA-3ro-5@gated-at.bofh.it>
Hello

I have the user list and the password list. I shot them with BASH. I
want to give passwords to the usernames in these separate files in
order.

File names:
Users.list
Passwords.list

In a loop, I have to not throw them into users of these passwords. I
did this for it, but it did not work.

#!bin/bash
passwords = $ (cat passwords.list)
for i in $ (cat passwordlist); do my program $ i $passwords; done


How do I make an Array? Or how can I solve it?

[toc] | [next] | [standalone]


#191063

From<tomas@tuxteam.de>
Date2018-01-14 15:10 +0100
Message-ID<v7R8Z-4oY-1@gated-at.bofh.it>
In reply to#191060
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, Jan 14, 2018 at 03:46:06PM +0300, Gokan Atmaca wrote:
> Hello
> 
> I have the user list and the password list. I shot them with BASH. I
> want to give passwords to the usernames in these separate files in
> order.
> 
> File names:
> Users.list
> Passwords.list
> 
> In a loop, I have to not throw them into users of these passwords. I
> did this for it, but it did not work.
> 
> #!bin/bash
> passwords = $ (cat passwords.list)
> for i in $ (cat passwordlist); do my program $ i $passwords; done

Too little context; what exactly doesn't work?

I'd suggest going in little chunks. First try:

  for i in $(cat passwords.list) ; do echo "<$i>" ; done

Does that do what you want?

Unrelated question: why are you doing in your first line

  passwords=$(cat passwords.list)

when in the second line you're doing $(cat passwordlist) anyway?
And shouldn't the second line be $(cat passwords.list?

(NOTE: to assign a shell variable, *no* space before the '='; this
won't work: the shell will read first 'passwords' then <space> and
try to interpret passwords as a command; but this first line seems
superfluous anyway).

> How do I make an Array? Or how can I solve it?

I don't think you need an array for what you are trying. But it
would make sense to get the shell basics straight, first.

Come back with more questions!

Cheers
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlpbY1cACgkQBcgs9XrR2kbYKACfZp0o9RthqaaibDo3Agva/UVz
47gAnjnzfrc7U8m5oUIKqtf6Hd9EufCd
=M0N3
-----END PGP SIGNATURE-----

[toc] | [prev] | [next] | [standalone]


#191136

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2018-01-15 15:50 +0100
Message-ID<v8eff-2SW-3@gated-at.bofh.it>
In reply to#191060
On Sun, Jan 14, 2018 at 03:46:06PM +0300, Gokan Atmaca wrote:
> I have the user list and the password list. I shot them with BASH. I
> want to give passwords to the usernames in these separate files in
> order.
> 
> File names:
> Users.list
> Passwords.list
> 
> In a loop, I have to not throw them into users of these passwords.

We need to know the contents of these files, not their names.

> I did this for it, but it did not work.
> 
> #!bin/bash
> passwords = $ (cat passwords.list)
> for i in $ (cat passwordlist); do my program $ i $passwords; done

There are many mistakes here, even without knowing what the files
contain or what you are trying to do with them.

First, your shebang is wrong.  It must be #!/bin/bash rather than
#!bin/bash.

Second, your assignment is wrong.  It must be var=value rather than
var = value.  You CANNOT have spaces around the = sign.

Third, your command substitution is wrong.  It must be $(command)
rather than $ (command).  You CANNOT have a space between the $ and (.

Fourth, if your passwords.list file contains spaces (which many
good passwords WILL contain), your entire algorithm is wrong.  Your
use of $(cat ...) splits the contents of the file on ALL whitespace,
not just newlines.  A password with a single space in it will be
treated as two words, and the loop will iterate once for each of
those words.

Also (let's call this bug number 4.5), any globbing characters in
the password.list file (like ? or * or [...]) will break things with
the algorithm you've chosen.

And then there's your for loop ... ugh.  No.  It's just unbearable.
This isn't valid code.  It's just random characters.

> How do I make an Array? Or how can I solve it?

Start from the beginning: WHAT IS IN EACH FILE?

Suppose users.list looks like this:

fred
barney
wilma
betty

And suppose passwords.list looks like this:

2^7djfnc5
yabba dabba doo
U(n  jv7s^&
password

Now suppose we are told "each line in users.list is one user, and each
line in passwords.list is one password".

Suppose we are told "there must be the same number of lines in both
files".

Suppose we are told "line N of users.list corresponds to line N of
passwords.list".

THEN you have enough information to actually write a program.

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?

Do you just need to create a single output file which combines them
together?  In that case, you can read a line at a time from each
file and never store them all in an array.  Just process sequentially.

Do you need to create a lookup table that you will refer to again and
again during some sort of GUI?  In that case, sure, an array might
make sense.  But you still need to define what you're doing.  Are you
planning to look up the password of a user GIVEN the user's name?
Then use an associative array which is indexed by the user's name and
contains the passwords.

Do you plan to look up the user's name and password GIVEN an index
number of some kind, perhaps chosen from a menu?  Then use two
indexed arrays, one that maps the index number to the username, and
the other that maps the index number to the password.

Until we know what the input files contain, and what your program is
supposed to do with them, nobody can tell you how to write your
program.

Start with these pages:

http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/BashFAQ/001

Then, later, when you're ready:

http://mywiki.wooledge.org/BashProgramming

[toc] | [prev] | [next] | [standalone]


#191522

FromGokan Atmaca <linux.gokan@gmail.com>
Date2018-01-25 13:40 +0100
Message-ID<vbOYV-5TE-3@gated-at.bofh.it>
In reply to#191136
>> I have the user list and the password list. I shot them with BASH. I
>> want to give passwords to the usernames in these separate files in
>> order.
>>
>> File names:
>> Users.list
>> Passwords.list

Hello

I'm sorry for the late reply.
The problem is: I can pull the users one by one. I can not do the same
for passwords.

Do I need to use "Mapfile" for this?

example-mapfile:

passwords=(cat $pass.list)
mapfile -t pass < "$passwords"


example-script:
   -- user.list
      a
      b
      c

  -- pass.list
     xxx
     yyy
     zzz


script:

#!/bin/bash


pass=$(cat pass.list)

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








On Mon, Jan 15, 2018 at 5:39 PM, Greg Wooledge <wooledg@eeg.ccf.org> wrote:
> On Sun, Jan 14, 2018 at 03:46:06PM +0300, Gokan Atmaca wrote:
>> I have the user list and the password list. I shot them with BASH. I
>> want to give passwords to the usernames in these separate files in
>> order.
>>
>> File names:
>> Users.list
>> Passwords.list
>>
>> In a loop, I have to not throw them into users of these passwords.
>
> We need to know the contents of these files, not their names.
>
>> I did this for it, but it did not work.
>>
>> #!bin/bash
>> passwords = $ (cat passwords.list)
>> for i in $ (cat passwordlist); do my program $ i $passwords; done
>
> There are many mistakes here, even without knowing what the files
> contain or what you are trying to do with them.
>
> First, your shebang is wrong.  It must be #!/bin/bash rather than
> #!bin/bash.
>
> Second, your assignment is wrong.  It must be var=value rather than
> var = value.  You CANNOT have spaces around the = sign.
>
> Third, your command substitution is wrong.  It must be $(command)
> rather than $ (command).  You CANNOT have a space between the $ and (.
>
> Fourth, if your passwords.list file contains spaces (which many
> good passwords WILL contain), your entire algorithm is wrong.  Your
> use of $(cat ...) splits the contents of the file on ALL whitespace,
> not just newlines.  A password with a single space in it will be
> treated as two words, and the loop will iterate once for each of
> those words.
>
> Also (let's call this bug number 4.5), any globbing characters in
> the password.list file (like ? or * or [...]) will break things with
> the algorithm you've chosen.
>
> And then there's your for loop ... ugh.  No.  It's just unbearable.
> This isn't valid code.  It's just random characters.
>
>> How do I make an Array? Or how can I solve it?
>
> Start from the beginning: WHAT IS IN EACH FILE?
>
> Suppose users.list looks like this:
>
> fred
> barney
> wilma
> betty
>
> And suppose passwords.list looks like this:
>
> 2^7djfnc5
> yabba dabba doo
> U(n  jv7s^&
> password
>
> Now suppose we are told "each line in users.list is one user, and each
> line in passwords.list is one password".
>
> Suppose we are told "there must be the same number of lines in both
> files".
>
> Suppose we are told "line N of users.list corresponds to line N of
> passwords.list".
>
> THEN you have enough information to actually write a program.
>
> 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?
>
> Do you just need to create a single output file which combines them
> together?  In that case, you can read a line at a time from each
> file and never store them all in an array.  Just process sequentially.
>
> Do you need to create a lookup table that you will refer to again and
> again during some sort of GUI?  In that case, sure, an array might
> make sense.  But you still need to define what you're doing.  Are you
> planning to look up the password of a user GIVEN the user's name?
> Then use an associative array which is indexed by the user's name and
> contains the passwords.
>
> Do you plan to look up the user's name and password GIVEN an index
> number of some kind, perhaps chosen from a menu?  Then use two
> indexed arrays, one that maps the index number to the username, and
> the other that maps the index number to the password.
>
> Until we know what the input files contain, and what your program is
> supposed to do with them, nobody can tell you how to write your
> program.
>
> Start with these pages:
>
> http://mywiki.wooledge.org/BashGuide
> http://mywiki.wooledge.org/BashPitfalls
> http://mywiki.wooledge.org/BashFAQ/001
>
> Then, later, when you're ready:
>
> http://mywiki.wooledge.org/BashProgramming
>

[toc] | [prev] | [next] | [standalone]


#191529

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2018-01-25 14:40 +0100
Message-ID<vbPV0-6zn-13@gated-at.bofh.it>
In reply to#191522
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.

[toc] | [prev] | [next] | [standalone]


#191531

FromGokan Atmaca <linux.gokan@gmail.com>
Date2018-01-25 15:00 +0100
Message-ID<vbQem-6FO-5@gated-at.bofh.it>
In reply to#191529
> ===========================================================
> #!/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<&-

I arranged according to this example. Very thanks.

On Thu, Jan 25, 2018 at 4:34 PM, Greg Wooledge <wooledg@eeg.ccf.org> wrote:
> 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.
>

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web