Author Topic: [Bash]For loop isn't looping  (Read 886 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
[Bash]For loop isn't looping
« on: June 18, 2014, 08:01:10 am »
I am a complete novice with bash(much to my dismay), and I got bored and tried writing some trivial script. It's not really working the way I want it to and I can't really figure out why.

Code: (bash) [Select]
#!/bin/sh

OUT=($(cat domains.txt))

for HOSTNAME in $OUT
do
echo "Getting name servers for [$HOSTNAME]"
  nslookup -type=ns $HOSTAME 8.8.8.8
done
The file it's working with:
Code: [Select]
example.com
example.org
example.net
However it only posts the results for example.com. I'm missing something simple but don't see it.

EDIT: Nvm, fixed it by storing lines into an array first.

Code: (bash) [Select]
#!/bin/sh

array=()

getArray() {
  i=0
    while read line
    do
        array[i]=$line
        i=$(($i + 1))
    done < $1
}

getArray "domains.txt"

for HOSTNAME in "${array[@]}"
do
echo "Getting name servers for [$HOSTNAME]"
  nslookup -type=ns $HOSTAME 8.8.8.8
done
Though I admit to getting help from StackOverflow. I don't 100% understand how the array works.
« Last Edit: June 18, 2014, 08:17:01 am by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline chris_NVT

  • /dev/null
  • *
  • Posts: 14
  • Cookies: 0
    • View Profile
Re: [Bash]For loop isn't looping
« Reply #1 on: June 18, 2014, 12:34:39 pm »
I can give you a quick look into how arrays work.

I will take your example:

The way your array is filled when reading the domain.txt

array[0] => example.com
array[1] => example.org
array[2] => example.net

This means that for every line it reads from domains.txt it will create a new entry, arrays always start with 0 if you use numeric values. You can also use names if you like.

Example
array['name'] => john doe
array[émail'] => johndoe@example.com

You can also have multidimensional arrays (arrays in arrays).

Example

array[0] => array[0] => test, array[1] => some text, array[2] => a bit more
array[1] => just some text here

Note that this is a very straight forward answer and not going into depth with arrays. There are a lot of tuts out there that will explain arrays in much more detail.

Offline frog

  • Knight
  • **
  • Posts: 232
  • Cookies: 16
    • View Profile
Re: [Bash]For loop isn't looping
« Reply #2 on: June 18, 2014, 01:46:54 pm »
I would have done it like this to save typing. For example you can concatenate your file into an array on the fly almost like you did initially. Bash just didn't like your syntax. You can also use a backtick `  rather than a money bracket $()  to cat the file into an array on the fly. Using for loops like this comes in handy.

Code: (bash) [Select]
#!/bin/bash

for item in $(cat file.txt); do
  echo "Getting name servers for $item"
  nslookup -type=ns $item 8.8.8.8
done

It works above as below:

Code: (bash) [Select]
for item in `cat file.txt`; do echo "Getting name servers for $item"; nslookup -type=ns $item 8.8.8.8; done

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [Bash]For loop isn't looping
« Reply #3 on: June 20, 2014, 03:53:45 am »
I can give you a quick look into how arrays work.

I will take your example:

The way your array is filled when reading the domain.txt

array[0] => example.com
array[1] => example.org
array[2] => example.net

This means that for every line it reads from domains.txt it will create a new entry, arrays always start with 0 if you use numeric values. You can also use names if you like.

Example
array['name'] => john doe
array[émail'] => johndoe@example.com

You can also have multidimensional arrays (arrays in arrays).

Example

array[0] => array[0] => test, array[1] => some text, array[2] => a bit more
array[1] => just some text here

Note that this is a very straight forward answer and not going into depth with arrays. There are a lot of tuts out there that will explain arrays in much more detail.
Thanks, but I know how arrays work. I was confused about some of the weird syntax of bash.

@frog - Thanks, that is much cleaner and helps me actually understand what is going on. Things like that make me really appreciate bash even more.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline chris_NVT

  • /dev/null
  • *
  • Posts: 14
  • Cookies: 0
    • View Profile
Re: [Bash]For loop isn't looping
« Reply #4 on: June 20, 2014, 08:09:02 am »
I don't 100% understand how the array works.

Sorry, my bad i guess. I read this as not knowing how an array exactly worked so i posted that.

Offline frog

  • Knight
  • **
  • Posts: 232
  • Cookies: 16
    • View Profile
Re: [Bash]For loop isn't looping
« Reply #5 on: June 20, 2014, 07:34:13 pm »
@frog - Thanks, that is much cleaner and helps me actually understand what is going on. Things like that make me really appreciate bash even more.

You're welcome. I like bash because it's clean and somewhat easy to read. It's perfectly suited for what it is.. the nexus between other programs on the system. I change my default shell on freebsd to bash because 'sh' doesn't have the same syntax.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [Bash]For loop isn't looping
« Reply #6 on: June 20, 2014, 09:18:27 pm »
Sorry, my bad i guess. I read this as not knowing how an array exactly worked so i posted that.
If that were the case I would have said that I don't 100% understand how arrays work, not the array. It's ok though, misunderstandings happen.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: [Bash]For loop isn't looping
« Reply #7 on: June 25, 2014, 04:01:36 am »
Nope, it's been figured out. I swear we need to start adding a [Solved] tag on our threads like the Arch forums.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python