Notes by Dr. Xi

Displaying notes 101 - 110
Created by Dr. Xi on February 28, 2011 12:29:19    Last update: February 28, 2011 12:30:40
The Unix shell passes these parameters to the shell script: Variable Meaning $* A single string representing all command line arguments separated by $IFS (internal field separator, usually a space) $@ A sequence of strings representing the command line arguments. $1,$2...$n $1 is the first argument, $2 is the second argument, and so on... $0 The name of the script itself. $# The number of arguments. Example shell script ( sharg.sh ): #!/bin/sh echo $# arguments passed to $0: $@ ... Output for ./sharg.sh "a b c d" : 1 arguments passed to ./sharg.sh: a b c d here ... Output for ./sharg.sh a "b c" d : 3 arguments passed to ./sharg.sh: a b c d here ...
Created by Dr. Xi on February 25, 2011 08:43:40    Last update: February 28, 2011 11:16:10
If you think the JavaScript code below is simple, think again. Try it in different browsers and see if you can explain what you see. What is displayed in the browser window with this HTML page? <!doctype html> <html> <body> <p>befor... What is displayed inside the iframe? Try different browsers. <!doctype html> <html> <head> <style t...
Created by Dr. Xi on February 17, 2011 16:09:49    Last update: February 17, 2011 16:09:49
This is a common problem with shell scripts running from cron. Everything works perfectly fine from the command prompt, but fails when running from cron. In the worst cases, the job fails silently, even giving a return code of 0! Usually, these are caused by differences between the execution environments: the interactive shell (command line) has more environment variables defined/exported (through .kshrc , .bashrc etc.) than the shell started by cron. A simple way to resolve the differences is to run set in the command prompt and compare the output with the output of set from cron (add a single line to the shell script). You can also make the shell script more verbose by adding the -x switch: #!/bin/sh -x
Created by Dr. Xi on February 17, 2011 14:06:22    Last update: February 17, 2011 14:06:22
This is the crontab to run a cron job every 15 minutes: */15 * * * * /path/to/executable >/redirect/output... or, 0,15,30,45 * * * * /path/to/executable >/redirect/... In general, the format is: minute hour day_of_month month day_of_week command Valid values are: Field Value minute 0-59 hour 0-23 day of month 1-31 month 1-12 day of week 0-6 (Sunday = 0) Reference: crontab(5) - Linux man page
Created by Dr. Xi on February 17, 2011 13:23:16    Last update: February 17, 2011 13:23:16
To find the PID of the tomcat instance that is currently running: ps -ef | grep java | grep tomcat | awk '{print $2}...
Created by Dr. Xi on February 17, 2011 12:59:47    Last update: February 17, 2011 12:59:47
Operator Meaning -e file1 file1 exists -b file1 file1 is a block special file -c file1 file1 is a character special file -d file1 file1 is a directory -f file1 file1 is a regular -g file1 Set Group ID (sgid) bit is set for file1 -h file1 file1 is a symbolic link -k file1 Sticky bit is set for file1 -L file1 file1 is a symbolic link -p file1 file1 is a named pipe (FIFO) -r file1 file1 is readable by the current process -s file1 file1 has a size greater than zero. -t FileDescriptor FileDescriptor is open and associated with a terminal. -u file1 Set User ID (suid) bit is set for file1 -w file1 Write flag (w) is on for file1 -x file1...
Created by Dr. Xi on February 09, 2011 15:35:41    Last update: February 09, 2011 15:36:08
Perl BEGIN and END blocks are executed at the beginning and at the end of a running Perl program. By the Perl doc : A BEGIN code block is executed a s soon as possible , that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed . You may have multiple BEGIN blocks within a file (or eval'ed string); they will execute in order of definition. Because a BEGIN code block executes immediately, it can pull in definitions of subroutines and such from other files in time to be visible to the rest of the compile and run time. Once a BEGIN has run, it is immediately undefined and any code it used is returned...
Created by Dr. Xi on February 09, 2011 13:47:50    Last update: February 09, 2011 13:47:50
This note demonstrates a technique to load a module generated at runtime, whose name is pre-determined but path is created at runtime. Sample module code: # DO NOT EDIT! # This module is generated at ru... Method one (use require ): C:\>perl $rpath = 'runtimepath'; require "${... Method two (use use with eval ): C:\>perl $rpath = 'runtimepath'; eval("use l...
Created by Dr. Xi on February 09, 2011 13:27:51    Last update: February 09, 2011 13:27:51
By the perldoc , use Module LIST; is exactly equivalent to: BEGIN { require Module; Module->import( LIST); } Because of the BEGIN block, use is executed immediately. Therefore, it is not suitable for lazy loading of modules at runtime. use does not work with a runtime variable: C:\>perl $cgi = "CGI"; require $cgi; prin... require works: C:\>perl $cgi = 'CGI'; require "${cgi}.pm"; ... Also, file extension is required if require is not passed a bareword: // this works require CGI; // so does th...
Created by Dr. Xi on November 15, 2010 22:00:53    Last update: February 09, 2011 12:55:51
Perl does not offer a trim or strip function to remove the leading or trailing spaces of a string. Three tricks are offered by the Perl faq : One line (this is the slowest): $string =~ s/^\s*(.*?)\s*$/$1/; Two lines: $string =~ s/^\s+//; $string =~ s/\s+$//; For loop: for ($string) { s/^\s+//; s/... or s/^\s+//, s/\s+$// for $string; Trim a batch: # trim whitespace in the scalar, the array, # ...
Previous  6 7 8 9 10 11 12 13 14 15 Next