Advanced Shell Scripting

Yesterday I delivered a lesson for the advanced Linux course arranged by LUG Roma3 at Roma Tre University on voluntary base. Here are the slides I sketched out for this occasion.




It’s not really “advanced” stuff, but sounds just better than “basics +1″ ;)


Change keyboard layout in Linux

Keybord gth There are several ways to change the keyboard layout in linux.
First, you can load a given keyboard translation tables with :

loadkeys <tablecode>

Alternatively, if you are in a X11 env, you can set the keyboard map using the X Keyboard Extension with:

setxkbmap <kbmapcode>

But if you want to permanently change the console keyboard layout, e.g. in Debian, you can reconfigure console-data and use the wizard to select the desired layout with:

dpkg-reconfigure console-data

How to open .7z files in Mac Os X

7z is a compressed archive file format that supports several data compression methods, featuring hight compression ratio, encryption and support for large files (up to 16 exabytes). Although it does not store UNIX permissions, it may be used to compress other archive formats such as tar streams, and it is often used to pack stuffs like VM appliances or other huge files. In Mac Os X, you can handle .7z archives with the 7za utility, provided by the p7zip package, a 7-Zip implementation, available via MacPorts.

port install p7zip
# ...
7za [ l | x | a | .. ] 7zarchive.7z

To be told, if you plan to use this format for backups, consider that it lacks of recovery records, that means even if just limited file corruption occur, data may be lost.


How to use XPath in Java

Need to use XPath in Java?
Here is a quick example, to get something from an XHTML file:

import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.w3c.tidy.Tidy;
import java.io.File;
import java.io.FileInputStream;
 
File file = new File("/abs/path/to/file");
FileInputStream xhtml = new FileInputStream(file);
 
Tidy tidy = new Tidy();
tidy.setQuiet(true);
tidy.setShowWarnings(false);
Document doc = tidy.parseDOM(xhtml, null);
 
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
 
NodeList nodes = (NodeList) xpath.evaluate("XPath Expr", doc, 
                                          XPathConstants.NODESET);
 
// NodeList nodes contains now nodes.getLength() nodes, e.g.
System.out.println(nodes.item(0).getNodeValue);
System.out.println(nodes.item(1).getTextContent();
String fst_child = nodes.item(2).getChildNodes().item(0).getNodeValue();

For more, take a look at the javax.xml.xpath package javadoc and at the NodeList interface.


Regular Expressions in Java

Need to use regular expressions (regex) for common tasks in Java?
Here is a quick example:

import java.util.regex.Pattern
import java.util.regex.Matcher
 
// ...
 
Pattern pattern = Pattern.compile("pattern");
Matcher matcher = pattern.matcher("subject");
 
// find (sub)matches
while(matcher.find()) {
    String nth_match = matcher.group();
}
 
// veryfy that the whole subject matches the pattern
Boolean does_it_matches = matcher.matches()
 
// replace all the occurrence of a regex in a String (no import needed)
"string instance".replaceAll("regex","replacement");

For other tasks take a look at the Matcher class javadoc.


Innovation Lab 2010

Innovation Lab 2010, Young Go-Getter Team

Does this look like a successful team ?
I spent the last days ( or weeks ? ) working with these guys at the business idea we submitted for this great chance. Even if we started with another completely different idea that we discarded in the middle of the timeline, the current idea has been in my mind for a couple of years, and my hope was to put it into a concrete form. We really worked hard on it, think about that: we spent last sunday and yesterday night till midnight at a MacDonald because in that period of time it was the only place with an internet connection we can stay and work :P

Anyway, the Innovation Lab 2010, that yesterday hitted wired news (it) is close to its grand final: tomorrow is the big day! Everyone interested is welcome from 9:30 for the Green Brand and from 14:30 for the Innovation Lab speeches and presentations. The place is the Business College “Federico Caffè” in Rome ( here ). Will follow the upstart drink by Upstart Roma.



Return top

About me