Reputation: 12347
I have a code which executes a command in Linux box (ls - latr /home/ars | awk '{if(NR>1)print}'
) which gives me list of directories along with its information. How do I put it into some array or list so that depending on each line I can get filename, permissions etc from the (list or array)!!
Here is my code which prints an output at the bottom of my question
here cmd=ls - latr /home/ars | awk '{if(NR>1)print}'
function calling
HashMap<String,String> params = IntermediateResults.get("userparams")
Map env=AppContext.get(AppCtxProperties.environmentVariables)
def fClass = new GroovyClassLoader().parseClass( new File( 'plugins/infa9/Infa9CommandExecUtil.groovy' ) )
String cmd="ls -latr "+rrs.get("linkpath")+" | awk '{if(NR>1)print}'"
String res=fClass.newInstance().fetchInformation( params, env, cmd )
my function called
public String fetchInformation( Map<String,String> params, Map env, String cmd )
{
try
{
Process proc = Runtime.getRuntime().exec(cmd);
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
result.append(line);
println "$line" // This output is given at the end
}
int exitVal = proc.waitFor();
}
catch (IOException io)
{
io.printStackTrace();
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
//println("\n\n"+result)
return result
}
my output
/data/u01/app/oracle/10.2.0/db_1:
total 260
-rwxr-xr-x 1 oracle dba 0 Jun 7 2005 root.sh.old
drwxr-xr-x 4 oracle dba 4096 Jan 17 2007 xdk
drwxr-xr-x 4 oracle dba 4096 Jan 17 2007 uix
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4tera
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4sybs
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4ingr
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4ifmx
So how can I put the above output in some List so that on each line I can get permissions, hardlink,owner, group,filesize,month, date, time, year and finally filename?
Update
This is what I am trying to do, is there a better way may be by using maps?
List<List<String>> frows = new ArrayList<List<String>>()
while ((line = br.readLine()) != null)
{
List<String> fileList= new ArrayList<String>()
result.append(line);
String[] strs = line.split(" ")
for(item in strs)
{
//print "$item "
fileList.add(item)
}
frows.add(fileList)
}
for (List<String> l : frows)
{
for (String s : l) {
print "$s"
}
println ""
}
Upvotes: 0
Views: 519
Reputation: 171084
An alternative would be to use JNA to access the native stat
call for your platform...
This works for me under OS X. I saved it as stattest.groovy
, and when executed by groovy stattest.groovy
, it prints out the details about itself:
@Grab( 'net.java.dev.jna:jna:3.3.0')
@Grab( 'org.jruby.ext.posix:jna-posix:1.0.3' )
import org.jruby.ext.posix.*
File f = new File( 'stattest.groovy' )
POSIX posix = POSIXFactory.getPOSIX( [ isVerbose:{ false } ] as POSIXHandler , true)
FileStat s = posix.stat( f.absolutePath )
s.properties.each { name, val ->
println "$name: $val"
}
[ 'atime', 'blocks', 'blockSize', 'ctime', 'dev', 'gid', 'ino', 'mode', 'mtime', 'nlink', 'rdev', 'st_size', 'uid' ].each {
println "$it() -> ${s."$it"()}"
}
prints out:
13:23:46 [tyates@mac] JNA $ groovy stattest.groovy
symlink: false
file: true
setuid: false
byteBuffer: java.nio.HeapByteBuffer[pos=0 lim=120 cap=120]
executableReal: false
structSize: 120
namedPipe: false
empty: false
blockDev: false
executable: false
fifo: false
class: class org.jruby.ext.posix.MacOSHeapFileStat
setgid: false
sticky: false
charDev: false
owned: true
directory: false
ROwned: true
readableReal: true
writableReal: true
readable: true
writable: true
groupOwned: false
socket: false
atime() -> 1317644640
blocks() -> 8
blockSize() -> 4096
ctime() -> 1317644636
dev() -> 234881026
gid() -> 1255
ino() -> 62213399
mode() -> 33204
mtime() -> 1317644636
nlink() -> 1
rdev() -> 0
st_size() -> 527
uid() -> 1114
Upvotes: 1
Reputation: 4935
Create a class say FileDetail
with properties permissions, hardlink,owner, group,filesize,month, date, time, year and filename. Inside your method
List<FileDetail> fileDetails = new ArrayList<FileDetail>();
while ((line = br.readLine()) != null)
{
FileDetail file = new FileDetail();
println "$line" // This output is given at the end
// parse - use space delimiter and String.split() API
String[] strs = line.split(" ");
// set values to "file"
// add to list
fileDetails .add(file);
}
return fileDetails;
Using Map,
List<Map<String, String>> files = new ArrayList<Map<String, String>>()
while ((line = br.readLine()) != null) {
Map<String, String> file = new Map<String, String>()
String[] strs = line.split(" ")
// Find the order of fields, but it is system dependent, may not work in futrue
file.add("PERMISSIONS", strs[0]); // and so on for next and you may need to trim map value
files.add(file);
println "$line"
}
return files;
Upvotes: 2
Reputation: 36229
You shouldn't use the system dependent ls-command, and parse it's error prone output. Consider a filename "foo\n drwxr-xr-x 4 oracle dba 4096 Jan 17 2007 uix" for example.
Use java.io.File instead, read the directory, get File objects with names, time attributes.
You use a list like this:
List <File> result = new List<File> ();
// and in the loop:
result.add (file);
Upvotes: 4