Need help to create a script...

Hi,

Am very new to scripting and yet to start learning... I need a help to create a script which should run in my unix boxes to acheive the following...

  1. Need to copy the hosts file and save it as hostDDMMYYY
  2. Should comment a line which matches the string cmpd
  3. Should add a new entry in the hosts file....

Can some one help me to get this...

Thanks,
Sarav :b:

I'll start with some hints and can elaborate as required :slight_smile:
Have a look at the output from these commands to see what's useful for you:

  • date +%d%m%Y
  • echo "some text"
    echo "some text" | sed 's/^some/#&/'
  • touch somefile.txt
    cat somefile.txt
    echo "a line of text" >> somefile.txt
    cat somefile.txt
    echo "another line of text" >> somefile.txt
    cat somefile.txt
    echo "a third line of text but a different > command" > somefile.txt
    cat somefile.txt

thank you....But I need to run this remotely....

Any help appreciated!

Thanks,Sarav

rsh/ssh

Sorry rsh is blocked ....

Did you try ssh?

If you wish to run commands against multiple hosts, create a file that lists each of your hosts (one per line). In the script example below, I've used "hostfile".

I've decided to connect to each host via ssh and run a couple of commands. In order to do so, they are passed to the ssh command INSIDE quotation marks. There are many limitations to this - you are welcome to experiment.

Start by running something simple:

for host in `cat hostfile`; do ssh $host "uname -n; touch /tmp/file.`date +%d%m%y`"; done;

Then test it by running:

for host in `cat hostfile`; do echo $host; ssh $host ls -trail /tmp; done

Expand and test where you can. The command `date +%d%m%y` is getting the date from the host where you are running the script - not from the host that you are ssh'ing in to.

Good luck...