Ksh scripting

Ksh scripting

Post by Chris Vida » Fri, 02 May 2003 21:21:20



I am on a HPUX 11.0 box using ksh.

I need to use the  /etc/host file to create another file used elsewhere.
The format of the new file should be

router <hostname> ATAP <Company Name>
ip        <ip address>

router   widhost1   ATAP WIDGET COMPANY
ip         10.10.10.2

# WIDGET COMPANY
widhost1 10.10.10.2
widhost2 10.10.11.3
widhost3 10.10.12.4
widhost4 10.10.13.5
#
# DIDGET COMPANY
didhost1 10.11.10.2
didhost2 10.11.11.2
didhost3 10.11.12.2
#

I am having difficulty setting the company name delimiter -- basically, read
first line, print the host, ip and company name until the company name
changes.

begin 666 cvidal.vcf
M0D5'24XZ5D-!4D0-"E9%4E-)3TXZ,BXQ#0I..CMC=FED86P-"D9..F-V:61A
M; T*14U!24P[4%)%1CM)3E1%4DY%5#IC=FED86Q 871T+F-O;0T*4D56.C(P
;,#,P-3 Q5#$R,C$R,%H-"D5.1#I60T%21 T*
`
end

 
 
 

Ksh scripting

Post by Hyo Joon Yo » Mon, 05 May 2003 04:04:32


Dear Chris:


> I am on a HPUX 11.0 box using ksh.

> I need to use the  /etc/host file to create another file used elsewhere.
> The format of the new file should be

You mean your '/etc/hosts' not '/etc/host' file.

Quote:

> router <hostname> ATAP <Company Name>
> ip        <ip address>

> router   widhost1   ATAP WIDGET COMPANY
> ip         10.10.10.2

This is the format of your desired output.

Quote:> # WIDGET COMPANY
> widhost1 10.10.10.2
> widhost2 10.10.11.3
> widhost3 10.10.12.4
> widhost4 10.10.13.5
> #
> # DIDGET COMPANY
> didhost1 10.11.10.2
> didhost2 10.11.11.2
> didhost3 10.11.12.2
> #

And this is a sample of what your '/etc/hosts' looks like.

Quote:> I am having difficulty setting the company name delimiter -- basically, read
> first line, print the host, ip and company name until the company name
> changes.

The assumptions for the logic is as follows.
1. Each line contains at most two fields.
    a. '#' followed by company name or just '#'.
    b. Hostname followed by IP.
2. Context of line is set by whether the first field is '#' or not.

The logic for detecting change in company name is in the determination
of context.
If '#' is detected as $hostname, then check $ip.  If it is non-empty,
then change the update the variable company.

Code is as follows.

company=''
while read hostname ip; do
     if [ "$hostname" = '#' ]; then
        #
        # It might be a line containing company name.
        #
        if [ -n "$ip" ]; then
            company=$ip
        fi
     else
        #
        # It is a line with hostname and ip.
        # Print only iff company name has been defined.
        #
        [ -n "$company" ] && cat <<EOF
router $hostname ATAP $company
ip $ip
EOF
     fi
done

Hope this helps you,

hjy

 
 
 

Ksh scripting

Post by rakesh shar » Tue, 06 May 2003 05:56:02



> I am on a HPUX 11.0 box using ksh.

> I need to use the  /etc/host file to create another file used elsewhere.
> The format of the new file should be

> router <hostname> ATAP <Company Name>
> ip        <ip address>

> router   widhost1   ATAP WIDGET COMPANY
> ip         10.10.10.2

> # WIDGET COMPANY
> widhost1 10.10.10.2
> widhost2 10.10.11.3
> widhost3 10.10.12.4
> widhost4 10.10.13.5
> #
> # DIDGET COMPANY
> didhost1 10.11.10.2
> didhost2 10.11.11.2
> didhost3 10.11.12.2
> #

> I am having difficulty setting the company name delimiter -- basically, read
> first line, print the host, ip and company name until the company name
> changes.

Here's the perl solution:

#!/bin/ksh -u

perl -wlane 'BEGIN{$,="\t";}
if(/^#\s+/../^#$/){
        next if /^#$/;
        ($company=$_)=~s/^#\s+/ATAP\t/ and next if /^#\s+/;
        print "router",$F[0],$company;
        print "ip",$F[1];

Quote:}' InputFile

Here's the sed solution:

#!/bin/ksh -u

sed -ne '
        ## transfrom all TABs to spaces
        y/      / /
        s/^ *//
        s/ *$//
        /^# /,/^#$/{
                /^# /{
                        s/^# //
                        h
                        d
                }
                /^#$/{
                        d
                }
                s/ / ATAP /
                G
                s/ATAP \([^ ]*\)\n\(.*\)$/ATAP \2\
ip \1/
                s/^/router /
                ## transform all spaces to TABs
                y/ /    /
                p
        }' InputFile

 
 
 

1. ksh script problem: pwd works differently for ksh then linux binary file

Hi,

I have a pdksh installed on Linux RedHat 6.2.
From an interactive shell, when I type pwd in a directory which is a link,
I get the directory name (not hte one linked to), as I expect.
When /bin/pwd is run, I get the linked directory name !
from a ksh script, it seems it will always run /bin/pwd for pwd rather
then the ksh implementation.
Any one knows how to override this problems in ksh scripts to it will run
the pwd of the ksh command ?

thanks,
Shai

--
Posted via CNET Help.com
http://www.help.com/

2. unix & linux dual boot system

3. 'script' in a cron initiated ksh script

4. can't boot kernel 2.4.2 on rh7

5. Sourcing a csh script from a ksh script

6. Why do functions show up in HISTFILE?

7. Converting ksh scripts to csh scripts

8. system power for a IPsec box?

9. ksh script --> sh script problems

10. How to "source" a csh script from ksh script

11. csh scripts inside ksh script?

12. ksh scripted sed needs a control J - but I can't write the script!

13. Start multiple scripts from one ksh script