home / infca / admin languages (navigation links) .

Amunt! Top Amunt!
Objectiu

Fem la mateixa feina en diferents llenguatges d'administracio per veure quin és mes adient ...

M'interessa un bon "parser" !

Primera llista :


Amunt! Top Amunt!
Feineta curta

entrada : comanda "ps -ef |
to-do: la eina ha de llegir una linia que és la sortida del "ps -ef" i buscar-hi un camp anomenat "PID" que es troba en un cert lloc de la linia
sortida : "kill -9 "

JavaScript

var parametre = process.argv[2]; var pslist=shell.exec('pslist '+parametre).output; var pid=pslist.split("\r\n").slice(8)[0].split(/\s+/)[1]; shell.exec('pskill '+pid);
Ruby

Probado en windows, no probado en linux:

def getPidForWin(pname) lines = IO.popen('tasklist /nh').readlines found = lines.find do |line| line.match('^' + pname + '\s+\d+') end if found thePid = found.match(/\d+/) puts "pid is #{thePid}" else puts "Process named #{pname} not found" end end def getPidForLinux(pname) lines = IO.popen('ps h').readlines found = lines.find do |line| line.match('\d+\s+.*\s+' + pname + '$') end if found thePid = found.match(/\d+/) puts "pid is #{thePid}" else puts "Process named #{pname} not found" end end isWindows = ENV['OS'] == 'Windows_NT' if ARGV.length <= 0 puts 'Parameter required: process name' exit end if isWindows getPidForWin(ARGV[0]) else getPidForLinux(ARGV[0]) end

El código queda ms bonito con colorines, por ejemplo con notepad++

Por lo visto la 1.8.7 tambin soporta el IO.popen, que es el meollo del código que ejecuta un comando externo y retorna la salida.

He puesto el código en {RH-v61-64b}

~sebas/ruby-tests/test-ps.rb

Puedes probarlo con

ruby test-ps.rb [nombre-del-proceso]

Por ejemplo:

ruby test-ps.rb -bash

Amunt! Top Amunt!
Feineta llarga

Hauria de ser una serie de passos com:


Amunt! Top Amunt!
Ruby install (RH v6.1 64bit)

Lluis requires 1.9 (+)

[root] $ yum install ruby

Amunt! Top Amunt!
python install (t400)

Homepage ; tutorial 3.3; nice tutorial v2; pattern matching ;

On T400 I have version 3.3:

Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:25:12) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>

Establish python access to own modules:

c:\> echo %PYTHONPATH% c:\Sebas\python\codi;

Pero tinc problemes inicial greus :

>>> import fibo Traceback (most recent call last): File "", line 1, in File "c:\Sebas\python\codi\fibo.py", line 6 print b, ^ SyntaxError: invalid syntax

S'arregla amb conversio explicita i parentesi:

def fib(n): # write Fibonacci series up to n a, b = 0, 1 s = "The initial value is " + str(n) # explicit conversion print (s) while b < n: print ( str(b) ), # a trailing comma avoids the newline after the output a, b = b, a+b if __name__ == "__main__": import sys fib(int(sys.argv[1]))

I tambe

>>> reload(fibo) Traceback (most recent call last): File "", line 1, in NameError: name 'reload' is not defined

Algo funciona

>>> import fibo >>> fibo.fib2(10000) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]

Remember: indentation is Pythons way of grouping statements.
Use PEP 8 coding conventions !


Amunt! Top Amunt!
node.js install (patan)

Lluis url : nodejs.org


Amunt! Top Amunt!
Links

Overview


Ep ! Valid HTML 4.01!   Valid CSS! Escriu-me !
Updated 20130808 (a)  
Uf !