Unix shell: workaround for loop problem
Friday, July 11th, 2008It’s not possible to get the value of the loop variables in some versions of ksh.
Example:
#!/bin/ksh
num=0
cat $0 | while read line ; do
let num=num+1
done
echo “Number=$num”
This script will return “Number=0″ as the result.
Here is the workaround for the problem: You should change the redirection method for the input file.
#!/bin/ksh
l=0
while read line ; do
let l=l+1
done < $0
echo [...]
Oracle: export/import using compressed file
Friday, September 7th, 2007Export to the compressed file
/etc/mknod my_own_pipe p
gzip -c -9 < my_own_pipe > mydump.gz &
exp username/password …… file=my_own_pipe
rm -f my_own_pipe
Import from a compressed file:
/etc/mknod my_own_pipe p
uncompress < mydump.Z > my_own_pipe &
imp username/password ……. file=my_own_pipe
rm -f my_own_pipe
MySQL: start SQL from shell script
Monday, March 19th, 2007The following code starts the SQL from the shell script directly, without additional SQL file
#!/bin/sh
–/. &> /dev/null; exec mysql “$@”
The detailed explanation of the trick is here
Unix: get the file date
Thursday, March 15th, 2007ls -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]); $a[4]++;
printf “%02d%02d%02d”,@a[5,4,3];
Automatic MySQL backup script
Wednesday, March 15th, 2006#!/bin/bash
#
# MySQL Backup Script
# VER. 2.5 - http://sourceforge.net/projects/automysqlbackup/
# Copyright (c) 2002-2003 wipe_out@lycos.co.uk
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program [...]
Redirect script output to the log
Wednesday, March 8th, 2006If 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
echo “The log file for the current session: the_log_file.$$.log”
exit 0
fi
shift # remove “-log” [...]
grep in find command: how to display file names
Thursday, February 23rd, 2006Here 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 {} /dev/null \;
Using pattern lists in Unix
Monday, February 20th, 2006Here 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 [...]
Print the PATH directories in the readable format
Thursday, February 16th, 2006echo $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 [...]
Find the files not in use in Unix
Friday, February 10th, 2006find . -name “*” -exec /usr/sbin/fuser {} 2>&1 \; | grep ‘: *$’
« Previous Entries