Wednesday, March 4, 2015

Linux Bash



http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
for i in $( ls ); do
echo item: $i
done
for i in `seq 1 10`;
do
echo $i
done

COUNTER=0
while [  $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
COUNTER=20
until [  $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done

http://tldp.org/LDP/abs/html/loops1.html
for arg in [list] ; do
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_01.html
for i in `cat list`; do cp "$i" "$i".bak ; done
for i in "$LIST"; do

http://www.cyberciti.biz/faq/bash-for-loop/
for i in {1..5}
for i in {0..10..2}
The seq command (outdated)
for i in $(seq 1 2 20)
Three-expression bash for loops syntax
for (( c=1; c<=5; c++ ))
do
   echo "Welcome $c times"
done
How do I use for as infinite loops?
for (( ; ; ))

http://code.tutsplus.com/tutorials/how-to-customize-the-command-prompt--net-20586
PS1='->'
source ~/.bashrc
PROMPT_COMMAND='echo "comes before the prompt"'
print_before_the_prompt () {
  echo "$USER: $PWD"
}

PROMPT_COMMAND=print_before_the_prompt
http://compositecode.com/2014/10/09/bashit-just-a-custom-bash-prompt-setup-for-git/

https://medium.com/@mandymadethis/pimp-out-your-command-line-b317cf42e953
alias sub=’open -a “Sublime Text”’

http://stackoverflow.com/questions/20296664/how-to-uninstall-bash-it


http://mywiki.wooledge.org/BashFAQ/001
while IFS= read -r line; do
   printf '%s\n' "$line"
done < "$file"
In the scenario above IFS= prevents trimming of leading and trailing whitespace. Remove it if you want this effect.
while read -r line; do
  printf '%s\n' "$line"
done <<< "$var"
while read -r line; do
  printf '%s\n' "$line"
done <<EOF
$var
EOF
while read -r line; do
  [[ $line = \#* ]] && continue
  printf '%s\n' "$line"
done < "$file"
# Input file has 3 columns separated by white space.
while read -r first_name last_name phone; do
  # Only print the last name (second column)
  printf '%s\n' "$last_name"
done < "$file"
# Extract the username and its shell from /etc/passwd:
while IFS=: read -r user pass uid gid gecos home shell; do
  printf '%s: %s\n' "$user" "$shell"
done < /etc/passwd
For tab-delimited files, use IFS=$'\t'.
read -r first last junk <<< 'Bob Smith 123 Main Street Elk Grove Iowa 123-555-6789'
some command | while read -r line; do
  printf '%s\n' "$line"
done
find . -type f -print0 | while IFS= read -r -d '' file; do
    mv "$file" "${file// /_}"
done

Note the usage of -print0 in the find command, which uses NUL bytes as filename delimiters; and -d '' in the read command to instruct it to read all text into the file variable until it finds a NUL byte. By default, find and read delimit their input with newlines; however, since filenames can potentially contain newlines themselves, this default behaviour will split up those filenames at the newlines and cause the loop body to fail. Additionally it is necessary to set IFS to an empty string, because otherwise read would still strip leading and trailing whitespace.
http://mywiki.wooledge.org/IFS
IFS = input field separator
In the read command, if multiple variable-name arguments are specified, IFS is used to split the line of input so that each variable gets a single field of the input. (The last variable gets all the remaining fields, if there are more fields than variables.)
When performing WordSplitting on an unquoted expansion, IFS is used to split the value of the expansion into multiple words.

Better Logging



http://perf4j.codehaus.org/devguide.html
http://pramod-musings.blogspot.com/2012/06/using-perf4j-to-time-methods.html
http://www.infoq.com/articles/perf4j
http://heshans.blogspot.com/2014/01/aspect-oriented-programming-with-java.html

http://www.nurkiewicz.com/2010/05/clean-code-clean-logs-use-appropriate.html
http://vasir.net/blog/development/how-logging-made-me-a-better-developer
Visibility into code helps manage complexity.
Communication

http://www.javacodegeeks.com/2011/01/10-tips-proper-application-logging.html
SLF4J is the best logging API available, mostly because of a great pattern substitution support
log.debug("Found {} records matching filter: '{}'", records, filter);
SLF4J is just a façade. As an implementation I would recommend the Logback framework.
Perf4J

3) Do you know what you are logging?
read your logs often to spot incorrectly formatted messages.
avoid NPE
logging collections
log.debug("Returning users: {}", users);
It is a much better idea to log, for example, only ids of domain objects (or even only size of the collection).
in Java we can emulate it using the Commons Beanutils library:
    return CollectionUtils.collect(collection, new BeanToPropertyValueTransformer(propertyName));
the improper implementation or usage of toString(). First, create toString() for each class that appears anywhere in logging statements, preferably using ToStringBuilder (but not its reflective counterpart). Secondly, watch out for arrays and non-typical collections. Arrays and some strange collections might not have toString() implemented calling toString() of each item. Use Arrays #deepToString JDK utility method.

Avoid side effects

5) Be concise and descriptive
log.debug("Message with id '{}' processed", message.getJMSMessageID());
Don't log passwords and any personal information

6) Tune your pattern
logging date when your logs roll every hour is pointless as the date is already included in the log file name. On the contrary, without logging the thread name you would be unable to track any process using logs when two threads work concurrently – the logs will overlap.
current time (without date, milliseconds precision), logging level, name of the thread, simple logger name (not fully qualified) and the message.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} %-5level [%thread][%logger{0}] %m%n</pattern>
    </encoder>
</appender>
You should never include file name, class name and line number, although it’s very tempting.
Besides, logging class name, method name and/or line number has a serious performance impact.

7) Log method arguments and return values
You can even use a simple AOP aspect to log a wide range of methods in your code. This reduces code duplication, but be careful, since it may lead to enormous amount of huge logs.

8) Watch out for external systems

9) Log exceptions properly
Avoid logging exceptions, let your framework or container (whatever it is) do it for you.
Log, or wrap and throw back (which is preferable), never both, otherwise your logs will be confusing.
log.error("Error reading configuration file", e);        //L

10) Logs easy to read, easy to parse
avoid formatting of numbers, use patterns that can be easily recognized by regular expressions, etc. If it is not possible, print the data in two formats:
log.debug("Request TTL set to: {} ({})", new Date(ttl), ttl);
final String duration = DurationFormatUtils.formatDurationWords(durationMillis, true, true);
log.info("Importing took: {}ms ({})", durationMillis, duration);

Log4j MDC(Mapped Diagnostic Context)
http://veerasundar.com/blog/2009/10/log4j-mdc-mapped-diagnostic-context-what-and-why/
http://veerasundar.com/blog/2009/11/log4j-mdc-mapped-diagnostic-context-example-code/
http://logging.apache.org/log4j/2.x/manual/thread-context.html
ThreadContext.push(UUID.randomUUID().toString()); // Add the fishtag;
ThreadContext.pop();
ThreadContext.put("id", UUID.randomUUID().toString(); // Add the fishtag;
ThreadContext.clear();
A Filter to put the user name in MDC for every request call
import org.apache.log4j.MDC;
MDC.put("userName", "veera");
MDC.remove("userName");
log4j.appender.consoleAppender.layout.ConversionPattern = %-4r [%t] %5p %c %x - %m - %X{userName}%n
https://lizdouglass.wordpress.com/tag/log4j-ndc/
NDC is an object that Log4j manages per thread as a stack of contextual information.