Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting current working directory when executing a shell process

Tags:

I would like to call sbt update within my source-code, to update multiple sbt projects. In the shell this is easy:

cd /path/to/project && sbt update 

But if I use scala.sys.process within my code, it won't remember the cd therefore sbt is called in the wrong directory. Code like this:

import scala.sys.process._ ("cd /path/to/project" #&& "sbt update").!! 

And I didn't find in the documentation any possibility to set sbt's project path via console. It would be nice if something like this works:

"sbt -projectPath /path/to/project update".!! 

If something like that is possible, this would save me a lot of mess! (Especially that it runs on UNIX and Windows.)

like image 984
Themerius Avatar asked Jul 20 '12 09:07

Themerius


People also ask

How do I change the working directory in shell?

To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.

When working in a shell what is meant by the current working directory?

The working directory. The shell always identifies a particular directory within which you are assumed to be working. This directory is known as the working directory (also known as the current working directory). To work with a file within your working directory, you need specify only the file name with a command.

How do I change the working directory of a shell in C?

The chdir command is a system function (system call) which is used to change the current working directory. On some systems, this command is used as an alias for the shell command cd. chdir changes the current working directory of the calling process to the directory specified in path.

How do I set current directory in Bash?

By default, bash shows just your current directory, not the entire path. To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd.


1 Answers

Use one of the ProcessBuilder factory methods on the Process object:

sys.process.Process(Seq("sbt","update"), new java.io.File("/path/to/project")).!! 

For more documentation, see the scaladoc file for the sys.process package. Unfortunately, it does not mention the 'current working directory' arguments, but they are in the documentation of object Process.

like image 104
0__ Avatar answered Oct 25 '22 00:10

0__