Results 1 to 7 of 7

Thread: Help convert a win shell script to linux

  1. #1
    Join Date
    Jul 2007
    Location
    UK
    Beans
    24
    Distro
    Kubuntu 10.04 Lucid Lynx

    Question Help convert a win shell script to linux

    I was wondering if there's anyone out there who could help with this. The point of it is to be able to launch Jython on a USB stick (using standalone jython.jar, as a cross-platform Portable Python) whether it's on windows or linux. I'm less familiar with linux so I'm wondering if anyone could help translate this windows script:

    Part 1 read in arguments list
    Code:
    @echo off
    
    set ARGS=
    :loop
    if [%1] == [] goto end
        set ARGS=%ARGS% %1
        shift
        goto loop
    :end
    Part 2 What DIR am I in?
    Code:
    rem get current dir
    set CURDIR=%~dp0
    Part 3
    current DIR has my jython.jar and a subdirectory called 'jars' which are the files I want to include on my class path.
    Code:
    rem add jython to classpath
    set MYCP="%CURDIR%jython.jar"
    rem set jars dir
    set JARS=%CURDIR%jars\
    Part 4
    Get a list of all of the jar files in the /jars direcory and add them to a parameter in the format "/path/to/jars/jar1.jar";"path/to/jars/jar2.jar"
    Code:
    rem add jars to the classpath
    for /F %%a in ('dir %JARS% /a /b /-p /o') do set MYCP=%MYCP%;"%JARS%%%a"
    Part 5 java launch command
    Code:
    rem java launch command
    java -classpath %MYCP% org.python.util.jython %ARGS%

  2. #2
    Join Date
    Oct 2006
    Location
    Lodz, Poland
    Beans
    31
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Help convert a win shell script to linux

    Most parts of the script look completely unnecessary, but... what I've just wrote seems to be possibly most similar to your version (I don't use jython, so I'm not sure if it shall work... but it should ).

    Code:
    #!/bin/bash
    
    #Part 1 read in arguments list
    ARGS="$@"
    
    #Part 2 What DIR am I in?
    CURDIR="$PWD"
    
    #Part 3 current DIR has my jython.jar and a subdirectory called 'jars' which are the files I want to include on my class path.
    MYCP="$CURDIR/jython.jar"
    JARS="$CURDIR/jars/"
    
    #Part 4 Get a list of all of the jar files in the /jars direcory and add them to a parameter in the format "/path/to/jars/jar1.jar";"path/to/jars/jar2.jar"
    for a in $(ls -A)
    do
    MYCP="$MYCP;$JARS$a"
    done
    
    #Part 5 java launch command
    java -classpath "$MYCP" org.python.util.jython "$ARGS"

  3. #3
    Join Date
    Jul 2007
    Location
    UK
    Beans
    24
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Help convert a win shell script to linux

    Many thanks, it mostly works, the only part is part 4, adding the list of files to the class path, it doesn't add any of the files from the sub directory to the list.

  4. #4
    Join Date
    Oct 2006
    Location
    Lodz, Poland
    Beans
    31
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Help convert a win shell script to linux

    Whoops! Of course, I'm listing a wrong directory (the current one instead of JARS)... Change that line (beginning of the loop):

    Code:
    for a in $(ls -A "$JARS")

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Help convert a win shell script to linux

    Quote Originally Posted by Pablo89 View Post
    Whoops! Of course, I'm listing a wrong directory (the current one instead of JARS)... Change that line (beginning of the loop):

    Code:
    for a in $(ls -A "$JARS")
    better to use a while and read loop in case of spaces in $JARS
    Code:
    ls -A | while read line
    do
     .....
    done

  6. #6
    Join Date
    Jul 2007
    Location
    UK
    Beans
    24
    Distro
    Kubuntu 10.04 Lucid Lynx

    Smile Re: Help convert a win shell script to linux

    Thanks for the help guys.

    I've still not quite got it sussed, but will post back the answers when I get back after a long weekend. I've had some help from a guy at work with using sed to do the job.

    But first,

    I never wanted $PWD, instead I required

    Code:
    CURDIR=$(dirname $0)
    Gives the directory that the file is located in, as opposed to the working directory, which could be anywhere.

  7. #7
    Join Date
    Jul 2007
    Location
    UK
    Beans
    24
    Distro
    Kubuntu 10.04 Lucid Lynx

    Lightbulb Re: Help convert a win shell script to linux

    OK I have the answer. The main problem I was getting was that the java classpath was not converted.

    Linux uses : as a separator
    while win uses ;

    (no wonder the script wouldn't work )

    Therefore the final working script was:

    Code:
    #!/bin/sh
    
    # get the current directory:
    CURDIR=$(dirname $0)
    
    # set the jar lib directory
    JARS=$CURDIR/jars/
    
    # add all of the jars to the classpath, separated with a colon
    MYCP=$(ls $JARS/ | sed "s#^#$JARS&#" | sed "s/$/:/" | xargs echo | sed 's/: */:/')
    
    # add the jython.jar
    MYCP="$MYCP$CURDIR/jython.jar"
    
    # launch within rlwrap to help with L/R UP/DOWN chars and command recall
    rlwrap -r java -classpath "$MYCP" org.python.util.jython "$@"
    Anyway, thanks guys for your help.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •