Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:04:20+00:00 2026-06-04T10:04:20+00:00

I need to develop an API for users not familiar with scala (neither Java)

  • 0

I need to develop an API for users not familiar with scala (neither Java) but familiar with Shell. They will, basically write shell scripts inside a scala class (I know I could just call external shell scripts, but come on! Also, later we will have some functions for common shell tasks).

I was hoping to accomplish something like:

1 object MyCoolScript extends MyMagicTrait {
2   $ "mkdir /tmp/test"
3   $ "cd /tmp/test"
4   $ "wget some-url"   
5 }

Being more direct, how can I turn lines 2-4 (or a possibly less concise version) into Seq[String] that I could process in MyMagicTrait?

I know about sys.process.stringToProcess but if I have:

object MyCoolScript extends MyMagicTrait {
  "mkdir /tmp/test" !!
  "cd /tmp/test" !!
  "wget some-url" !!  
}

How can I get the result of each command in a concise way? also, I was hoping for a $ “xxx” notation.

Post Answers Update:

Thanks to @debilski, @tenshi and @daniel-c-sobral I was able to come up a very close to the desired implementation: https://gist.github.com/2777994

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:04:22+00:00Added an answer on June 4, 2026 at 10:04 am

    Seems that string interpolation that comes with Scala 2.10 can help you here. At first you can implement simple $ method, that simply executes command immediately. In order to make it you need to add this custom method on StringContext:

    object ShellSupport {
        implicit class ShellStrings(sc: StringContext) {
            def $(args: Any*) = 
                sc.s(args: _*) split "\n" map (_.trim) filterNot (_.isEmpty) foreach { cmd =>
                    // your excution logic goes here
                    println(s"Executing: $cmd")
                }
    
        }
    } 
    

    Now you can use it like this:

    import ShellSupport._
    
    val testDir = "/tmp/test"
    
    $"mkdir $testDir"
    $"cd $testDir" 
    $"""
        wget some-url
        wget another-url
     """ 
    

    You can take advantage of it’s syntax (it’s only downside, is that you can’t add space between $ and ") and string interpolation within command.


    Now let’s try to implement your magic trait. It’s generally the same idea, but I’m also using DelayedInit in order to properly define commands and then automatically execute them during class creation.

    trait MyMagicTrait extends DelayedInit {
        private var cmds: List[String] = Nil
    
        def commands = cmds
    
        implicit class ShellStrings(sc: StringContext) {
            def $(args: Any*) = {
                val newCmds = sc.s(args: _*) split "\n" map (_.trim) filterNot (_.isEmpty)
                cmds = cmds ++ newCmds
            }
        }
    
        def delayedInit(x: => Unit) {
            // your excution logic goes here
            x
            cmds map ("Excutintg: " + _) foreach println
        }
    }
    

    And it’s usage:

    class MyCoolScript extends MyMagicTrait {
      val downloader = "wget"
    
      $"mkdir /tmp/test"
      $"cd /tmp/test" 
      $"""
        $downloader some-url
        $downloader another-url
       """ 
    }
    
    new MyCoolScript
    

    Both of these solutions produce the same output:

    Executing: mkdir /tmp/test
    Executing: cd /tmp/test
    Executing: wget some-url
    Executing: wget another-url
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on Java Mail API and i need to develop a general
I need to develop a single routine that will be fired each 5 minutes
I need to develop a point of sale app that will be used on
I'm trying to develop a site that will allow users to pay for services
I need to develop a Stock Tracker Application using Google Finance API . i
I want to develop a LBS app for Android,but I do not know how
I want to develop a Web App for Mobile users, which will provide users
I need to develop a pretty complicated Flash site based on the Facebook API,
I need to develop a classic asp application on vista, and would like to
I need to develop some sort of application featuring Editing Movie slices, Adding and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.