0

I have a bash script which getting the data by this:

$ ./script.sh var1 var2 var3

What I want is to have the variables in a text file in the same format like I type in the command line.

input.txt

var1 var2 var3

And I want somehow to feed this txt to my bash script in the command line, not in the bash script:

./script.sh input.txt

How is it possible?

Update:

Here is my script:

#!/bin/bash

DOMAINS=( '.com' '.biz' )

VALUE=$(<input.txt)
INPUT=(`echo "$VALUE"`)

ELEMENTS=${#DOMAINS[@]}

while (( "$#" )); do

  for (( i=0;i<$ELEMENTS;i++)); do
      whois $1${DOMAINS[${i}]} | grep --perl-regexp --text --null --only-matching --quiet 'Creation Date'
    if [ $? -eq 0 ]; then
        echo -e "$1${DOMAINS[${i}]}\tregistered\t"
    else
        echo -e "$1${DOMAINS[${i}]}\tavailable\t"
    fi
  done

shift

done

As you can see, I tried to implement in the script also, but I'm not a programmer.

The perfect solution would be to remove the used data from input.txt. So if it reads var1 from input.txt, than remove it from there! This can be solved?

0

1 Answer 1

3

There are various ways to do this. If your script doesn't need to take more than one argument, just use a while loop to read the file. I would also remove some of the needless grep options (you don't need --only--matching when using --quiet, you're never seeing the output; you don't need perl regex since you're not using any of their features and you don't need --text since you're not going to be parsing binary files) and using the shorter option names for brevity.

So, a working (and slightly improved) version of your script would be:

#!/bin/bash

DOMAINS=( '.com' '.biz' )

while read input; do
    for (( i=0;i<${#DOMAINS[@]};i++)); do
    whois "$input${DOMAINS[$i]}" | grep -q 'Creation Date'
    if [ $? -eq 0 ]; then
            echo -e "$input${DOMAINS[$i]}\tregistered\t"
    else
            echo -e "$input${DOMAINS[$i]}\tavailable\t"
    fi
    done
done < "$1"

If you run that on a file containing:

google
foogle

You get:

$ foo.sh file
google.com  registered  
google.biz  available   
gaagle.com  registered  
gaagle.biz  available   
5
  • I got the following error when I try to run this script: -bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory. The command was: ./script.sh input.txt. Input.txt is in the same folder just like the bash script.
    – Lanti
    Commented Nov 15, 2015 at 14:53
  • If I change the headline to this #!/usr/bin/env bash than I got the following error: : No such file or directory
    – Lanti
    Commented Nov 15, 2015 at 14:59
  • 2
    @user1442219 you're working on a Windows system then, or edited the file on Windows. Don't. That inserted a carriage return character (\r) which is needed to define newlines in Windows (they use \r\n while *nix uses \n). To fix it, run sed -i 's/\r//g' script.sh and then try again.
    – terdon
    Commented Nov 15, 2015 at 15:08
  • Sorry, that was the problem. I forget to set the line endings on the new file where I copied your script. Works great!
    – Lanti
    Commented Nov 15, 2015 at 15:17
  • 1
    you might just do: for i in .com .biz; do .... that could be a little more simple. why assign to an array to iterate that array when the for incorporates an array assignment in the syntax?
    – mikeserv
    Commented Nov 15, 2015 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .