How do you extract a JAR in a UNIX filesystem with a single command and specify its target directory using the JAR command?

前端 未结 4 671
囚心锁ツ
囚心锁ツ 2020-12-23 16:34

I am creating a Python script within which I am executing UNIX system commands. I have a war archive named Binaries.war which is within an ear archive named Portal.ear

4条回答
  •  眼角桃花
    2020-12-23 16:58

    If this is a personal script, rather than one you're planning on distributing, it might be simpler to write a shell function for this:

    function warextract { jar xf $1 $2 && mv $2 $3 }
    

    which you could then call from python like so:

    warextract /home/foo/bar/Portal.ear Binaries.war /home/foo/bar/baz/
    

    If you really feel like it, you could use sed to parse out the filename from the path, so that you'd be able to call it with

    warextract /home/foo/bar/Portal.ear /home/foo/bar/baz/Binaries.war
    

    I'll leave that as an excercise to the reader, though.

    Of course, since this will extract the .war out into the current directory first, and then move it, it has the possibility of overwriting something with the same name where you are.

    Changing directory, extracting it, and cd-ing back is a bit cleaner, but I find myself using little one-line shell functions like this all the time when I want to reduce code clutter.

提交回复
热议问题