0

Let's say i have a file(Input.txt) with some following dig commands.

dig NS google.com
dig NS box.com
dig NS dropbox.com
dig NS cnn.com
dig NS bbc.co.uk

I want to execute the dig commands from the file only and which has to be read line by line.

Currently i tried using

for i in $(cat Input.txt);do $i;done

I'm not sure,how this can be executed and i get the following message as command not found.

Any suggestions please on resolving this issue ?

Arun
  • 1,160
  • 3
  • 17
  • 33

3 Answers3

2

Try:

. Input.txt

A dot followed by a space and file name will execute each line in the file.

Your for loop doesn't work because it is splitting up the input by word, not by line. So it's trying to execute the command google.com, for instance, hence the "command not found" messages.

twm
  • 1,448
  • 11
  • 19
  • 1
    Nicely done; to complement your answer, here's [a link](http://mywiki.wooledge.org/DontReadLinesWithFor) that explains the problem with using `for` to parse command in more detail and how to do it right (_if_ it is needed). – mklement0 Oct 23 '15 at 04:03
0

You can create a .sh file and place commands redirecting the outputs, eg:

#!/bin/bash

dig NS google.com >> ~/google.log
dig NS box.com >> ~/box.log
dig NS dropbox.com
dig NS cnn.com
dig NS bbc.co.uk
Roknauta
  • 178
  • 10
-2

You're looking for the eval keyword. So, with your general example:

cat Input.txt | while read i;do eval $i;done

Personally, I'd be inclined to suggest that the better way to do this specific thing would be to have your list of domains, and do something like:

xargs -n 1 -a /file/with/domains dig NS

Depending on your data source, the xargs solution might be significantly more secure than just running whatever command is in a file. But that obviously depends upon what's in your real data file.

dannysauer
  • 3,793
  • 1
  • 23
  • 30