Unix: get the file date

ls -1 | cpio -o | cpio -ivt | awk ‘{print $NF, $(NF-1), $(NF-4), $(NF-3) }’

Warning: I/O expensive for the large files!

Perl:

@a = localtime((stat($my_file))[9]); [...]

Redirect script output to the log

If the whole output of the complex script should be redirected to the log, the following trick could be used.

if [ "$1" != "-log" ] ; then                                  
   $0 -log "$@" 2>&1 | tee the_log_file.$$.log         [...]

grep in find command: how to display file names

Here is very simple trick to force the grep command to display file name, when it’s used together with find operation.
Just write /dev/null as the “second file”

find . -type f -exec grep somestring {} [...]

Using pattern lists in Unix

Here is the small reminder about the syntax of the “case” command and the usage of the pattern lists.

#!/bin/ksh
print -n "Please enter the line: "
read line

case "$line" in
 ?(dog|cat)  ) print "zero or one occurrence of any pattern" ;;
 *(low|high) ) print "zero or more occurrences of any pattern" ;;
 @(duncan|methos) ) print "exactly one occurrence of any pattern" [...]

Print the PATH directories in the readable format

echo $PATH| awk -v RS=":" ‘{ print $0 }’

echo $LD_LIBRARY_PATH |awk -v RS=":" ‘{ system ( "ls -rltd " $0 ) }’

Warning!
As far as the option ‘-v’ is used, the new awk(nawk in some systems) should be used.

To check if the new version of awk is installed:

awk 1 /dev/null

The output will be empty for new awk.
You [...]

Parsing script parameters

Quick and dirty parsing procedure for unix shell scripts

parse_command_line ()
{
typeset -i user_opt help_opt password_opt verbose_opt
typeset errmsg

arg_cou=$#

while [ "$#" -gt 0 ]
do
<strong>case "$1" in</strong>
-@([U]) )  let user_opt=user_opt+1 ;;
-P       )    let password_opt=password_opt+1 ; ask_pass=0 ;;
-option1    )    ;;
-option2    )    ;;
-option3    )    ;;
-@([hH?])) help_opt=1;;
-v*(erbose)) let verbose_opt=verbose_opt+1 ;;
*)
if   [ user_opt [...]

Rev function and comma-separated output

Here is the shell command snippet to display comma-separated output:

ls -lrt | rev | sed ‘s/\\([0-9][0-9][0-9]\\)/\\1,/g’ | rev | sed ‘s/\\([\^0-9]\\),\\([0-9]\\)/\\1\\2/g;s/\^,\\([0-9]\\)/\\1/g’

Example:
-rw-r—– 1 sybase dba 1,572,872,192 Feb 2 07:09 master.dbf

Rev in awk

#!/bin/ksh
nawk ‘{ l=length($0) ; for(i=l;i>0;i–) { printf "%s", substr($0,i,1) } ; print "" }’

Rev function (absent on SunOS) :

(Warning! [...]