Here is a one liner to find the number of days in previous month (works in bash and KSH)
cal $((`date '+%m'`-1)) `date '+%Y'` | grep . | fmt -1 | tail -1
Here is a one liner to find the number of days in previous month (works in bash and KSH)
cal $((`date '+%m'`-1)) `date '+%Y'` | grep . | fmt -1 | tail -1
One interesting API in Java is to manipulate key-value configuration files known as ‘properties’
I do not know if shell script gurus like to use property files. However, if you are looking for some code to read a key-value pair property file, here is something:
#!/bin/sh
#---------------------
# arg1: prop file
# arg2: prop key
# returns property value if found,
# nothing if not found
#---------------------
get_prop(){
propfile=$1
key=$2
grep "^${2}=" ${1}| sed "s%${2}=\(.*\)%\1%"
}
Suppose you have a properties file such as : test.properties below
user.name=Ranjith init.dir=/tmp
A test call to get value for “user.name” would look like
get_prop test.properties user.name
Sometimes you would want to print boxed messages such as the following from a script:
+-----------------------------------+
|A word is defined as a string of |
|characters delimited by spaces, |
|tabs, or newline characters. |
+-----------------------------------+
If you want to have a re-usable routine to do this, things get a bit complex. There comes print formatting and word wrapping. While print formatting is easy in shell with printf, word wrapping would require writing a lot of lines of code and trickery. But you can do this real quick using Perl. Here is a little program.
To execute, pass string to be boxed and width of the box as commandline arguments as below
perl box.pl "A word is defined as a string of characters delimited by spaces, tabs, or newline characters." 35
use Text::Wrap;
$Text::Wrap::columns = $ARGV[1];
print ( "+" . ( "-" x $ARGV[1] ) . "+\n" );
foreach(split('\n',wrap('', '', $ARGV[0])))
{
printf "|%-$ARGV[1]s|\n", $_;
}
print ( "+" . ( "-" x $ARGV[1] ) . "+\n" );
Here is my Google project to demo hibernate search features.
http://code.google.com/p/hb-search-demo/
OK, so you want to edit/view all the files matching certain pattern on vi from command prompt. How would you do it?
Search files using usual “find” command, then translate new line in result to space using “tr” command and feed the list to vi – simple! Here you go:
vi `find . -name *.sh | tr '\n' ' '`
Good! so, the following code will not impact your method signature
def doSomething(n:Int) = {
if (n < 10) {
throw new Exception("Only values between 1 and 10")
}
...
vim as of version 7 had introduced built-in spellchecker. I had not used till date, but recently checked it out. So, to turn it on, type command
set spell
If you have some text on your vim and you turn sell on with above command, you will see red underlines for words that vim thinks are incorrectly spelled. Of course, while coding you might not want spell check, so you can turn it off by
set nospell
Now, about navigation and correction.
To go to next spelling error, the keyboard command is ]s to go to previous error, the command is [s. To see spelling suggestions, while the cursor is on word with error, press =z and it presents a list of possible words to choose from. Just type the number corresponding to the word that you are interested in, and you are all set.
Ok, so you are very excited about Generics and would “Generify” whatever you can. But here some interesting pointers.
One: This will cause a compilation error
Set<String> set = new HashSet<String>();
set.add("Testing");
set.add("Testing again");
set.add("Testing again and again");
set.add(new Integer(1));
Okay, that we all know.
Two: Now, what will happen with this?
public static void main(String[] args){
Set<String> set = new HashSet<String>();
set.add("Testing");
set.add("Testing again");
set.add("Testing again and again");
doSomething(set);
System.out.println(set);
}
private static void doSomething(Set s){
s.add(new Integer(21));
}
}
This will compile fine (With a warning actually, that we all would ignore) and print contents of the set, including a number.
Three: Well, how to fix this ? How to throw an error when a legacy code is attempting to change the sole purpose of your HashSet? The answer is to declare your set as
Set<String> set = Collections.checkedSet(new HashSet<String>(),String.class);
public class Employee{
private String name;
Employee(String str ){
this.name= str;
}
}
public class Test {
public static void main(String argv[]){
TreeSet myMap = new TreeSet();
Employee s3 = new Employee("abcdef");
Employee s4 = new Employee("abcdef");
myMap.add(s3);
myMap.add(s4);
This code will throw a
Exception in thread "main" java.lang.ClassCastException
Unless Employee implements comparable