Skip to content
Advertisement

How to call variables from ip.txt file to .sh file?

I have list of ip addresses in one txt file which I have named ip.txt

I want to call those ip in my .sh file, I am not sure how to proceed here..

here I did soemthing

#!/bin/bash -x
var='ip.txt'
for i in $var; do
echo snmpwalk -v 2c -c rcgoips $i 1.3.6.1.3.83.1.4.5.0
done

Please tell me how to do that?

Advertisement

Answer

This should make it:

#!/bin/bash -x
var='ip.txt'
while read line; do
  echo snmpwalk -v 2c -c rcgoips $line 1.3.6.1.3.83.1.4.5.0
done < $var

or using for as you do

#!/bin/bash -x
var='ip.txt'
for line in $(<$var); do
  echo snmpwalk -v 2c -c rcgoips $line 1.3.6.1.3.83.1.4.5.0
done

because the syntax is: for i in $(<file); do echo $i; done

Advertisement