Osy
Osy

Reputation: 115

Running external unix commands in Scala

Trying to run some external commands through a scala code.

This is a line from it

import sys.process._
("hdfs dfs -cat /data/test/zipfiletest/filename")!

Here instead of directly specifying the filename I have this filename in a scala variable. How do I append this filename to the existing command.

Tried something like :

("hdfs dfs -cat /data/test/zipfiletest/")+filename!

But this doesn't work. Is there another way to do it?

Upvotes: 1

Views: 3381

Answers (3)

Brian Agnew
Brian Agnew

Reputation: 272287

You can use string interpolation e.g.

s"hdfs dfs -cat $myVariable"

but beware if your filename contains a space! For that reason you may prefer to invoke the Unix command using an API call taking each argument separately (rather than letting the API parse out each argument separately - which will fail in the above scenario)

Upvotes: 1

pedrorijo91
pedrorijo91

Reputation: 7845

you can use string interpolation or string concatenation

string interpolation:

import sys.process._
(s"hdfs dfs -cat /data/test/zipfiletest/$filenameVariable")!

string concatenation:

import sys.process._
("hdfs dfs -cat /data/test/zipfiletest/" + filename)!

personally I would go for the string interpolation, is more readable

Upvotes: 2

Nonontb
Nonontb

Reputation: 476

try like this :

import sys.process._
(s"hdfs dfs -cat /data/test/zipfiletest/$filename")!

Upvotes: 2

Related Questions