Connecting Tech Pros Worldwide Forums | Help | Site Map

Shell Script to List Node Definitions

Colloid Snake's Avatar
Familiar Sight
 
Join Date: Nov 2006
Posts: 145
#1: May 6 '08
Hello everyone-

I am working on a shell script to parse out a Node.def file that contains the hostname, version, IP address, and date updated. There is a separate .def file for each node (of which there are several hundred), and each node is in a separate directory.

I am attempting to extract the node name and IP address from each .def file and compile these into a single file, but I'm getting an error, no matter what I alter, so I think I'm just not setting this up properly. Can anyone help?

Expand|Select|Wrap|Line Numbers
  1. echo "`date` - List of names and correlating IPs for `hostname`" > SensorList.txt
  2.  
  3. set file_list = `find . -name "*.def" -type f`
  4.  
  5. for $file in file_list 
  6.    # extract name and ip from file
  7. do
  8.    `cat $file | grep "NAME=*"` >> SensorList.txt
  9.    `cat $file | grep "IP=*"` >> SensorList.txt
  10. done
  11.  
Error:
# ./CreateSensorList.sh
./CreateSensorList.sh: file_list: command not found
cat: file_list: No such file or directory
cat: file_list: No such file or directory

I have attempted to move around the `` to encompass different things, add/remove the $ in the for, etc... And I'm currently out of ideas, so any help would be appreciated.

ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 390
#2: May 7 '08

re: Shell Script to List Node Definitions


Quote:

Originally Posted by Colloid Snake

Hello everyone-

I am working on a shell script to parse out a Node.def file that contains the hostname, version, IP address, and date updated. There is a separate .def file for each node (of which there are several hundred), and each node is in a separate directory.

I am attempting to extract the node name and IP address from each .def file and compile these into a single file, but I'm getting an error, no matter what I alter, so I think I'm just not setting this up properly. Can anyone help?

Expand|Select|Wrap|Line Numbers
  1. echo "`date` - List of names and correlating IPs for `hostname`" > SensorList.txt
  2.  
  3. set file_list = `find . -name "*.def" -type f`
  4.  
  5. for $file in file_list 
  6.    # extract name and ip from file
  7. do
  8.    `cat $file | grep "NAME=*"` >> SensorList.txt
  9.    `cat $file | grep "IP=*"` >> SensorList.txt
  10. done
  11.  
Error:
# ./CreateSensorList.sh
./CreateSensorList.sh: file_list: command not found
cat: file_list: No such file or directory
cat: file_list: No such file or directory

I have attempted to move around the `` to encompass different things, add/remove the $ in the for, etc... And I'm currently out of ideas, so any help would be appreciated.




please have a look at following script..it should work

Expand|Select|Wrap|Line Numbers
  1.  
  2. echo "`date` - List of names and correlating IPs for `hostname`" > SensorList.txt
  3. file_list=`find . -name "*.def" -type f`
  4. for file in $file_list
  5. # extract name and ip from file
  6. do
  7.   echo $file
  8.   `cat $file | grep "NAME=*"` >> SensorList.txt
  9.   `cat $file | grep "IP=*"` >> SensorList.txt
  10. done
  11.  
  12.  
check out the second line.
you don't have to use 'set' to initialize the local variable..it is used to set the environmental variables.
Don't put spaces before and after '=' operator.
In third line use '$file_list' instead 'file_list'

Rest is fine.....
Colloid Snake's Avatar
Familiar Sight
 
Join Date: Nov 2006
Posts: 145
#3: May 7 '08

re: Shell Script to List Node Definitions


Ah, thank you very much! I also removed both of the `` around the cat $file... lines to get it to run (and the ='s are in search strings, that's how the file is set up so I left those in) and it's working now.

Thanks again

~Snake
Reply