Writing common script for windows and unix..... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Writing common script for windows and unix.....

Prakash_RadhaPrakash_Radha Posts: 14Member
hi, i want to know how to write scripts such that i can use them in both windows and unix.....

For example windows uses back-slash() whereas unix uses forward-slash(/) for specifying the path.....

Another example could be using commands such as fc and diff which are file comparison commands in UNIX and DOS respectively.....

Is there any way to specify that if the OS detected is Windows then use one script and if the OS specified is UNIX or Linux then use another script???

Thanks,
prakash.

Comments

  • JonathanJonathan Posts: 2,914Member
    : hi, i want to know how to write scripts such that i can use them in
    : both windows and unix.....
    :
    Mostly, by avoiding stuff that doesn't work in both places.

    : For example windows uses back-slash() whereas unix uses forward-
    : slash(/) for specifying the path.....
    :
    I've generally found you can get away with using forward slashes on Windows and it won't complain. Especially if you're just using relative paths etc. You may be interested in a module called File::Spec.

    : Another example could be using commands such as fc and diff which
    : are file comparison commands in UNIX and DOS respectively.....
    :
    If you are going to be depending on external programs, you inherently risk cross-platform issues. It's often a good idea to see if you can manage a pure-Perl implementation. And remember to check out http://search.cpan.org/ as someone else may have done some of the hard work for you.

    : Is there any way to specify that if the OS detected is Windows then
    : use one script and if the OS specified is UNIX or Linux then use
    : another script???
    :
    [code]if ($^O =~ /MSWin32/) {
    # do Win32 stuff
    } elsif ($^O =~ /linux/) {
    # do Linux stuff
    # other checks
    } else {
    # we have no idea - do something generic.
    }[/code]

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • Prakash_RadhaPrakash_Radha Posts: 14Member
    hi, now i'm able to run my code on both windows as well as solaris..... but the only problem is the first line of the script where u specify the perl library has to be changed manually.....

    ex: suppose the perl directory is in D: in windows u have to specify
    #!D:\perl
    where as in unix if its in the usr/bin/ directory then u have to specify
    #!/usr/bin/perl

    Is there anyway to overcome this?

    Thanks,
    prakash.
  • JonathanJonathan Posts: 2,914Member
    : hi, now i'm able to run my code on both windows as well as
    : solaris..... but the only problem is the first line of the script
    : where u specify the perl library has to be changed manually.....
    :
    : ex: suppose the perl directory is in D: in windows u have to specify
    : #!D:\perl
    : where as in unix if its in the usr/bin/ directory then u have to specify
    : #!/usr/bin/perl
    :
    : Is there anyway to overcome this?
    :
    #! lines are generally only relevant to UNIX systems - Windows as an OS doesn't make use of them to decide what interpreter to use. So there's a good chance you can get away with leaving it as #!/usr/bin/perl. Of course, if Perl isn't there on a UNIX system you're in for some fun. There's always the assume it's in the system path trick and just put #!perl though I don't really recommend this idea (if it was a really good one, everyone would just do it).

    I guess in some situations you'll just have to change it!

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.