Tuesday, September 12, 2006

help with unix scripts

To define date and time

DATE=`date '+%y%m%d'`
TIME=`date '+%k%M'`

using awk

LIST=`awk '{print $1}' file`
for OBJECT in ${LIST}
do
echo OBJECT
done

the default awk uses a space, using other separators, use a -F


calling sqlplus

sqlplus -s /nolog </dev/null
connect / as sysdba
set echo off embedded on feedback off heading off linesize 120 pagesize 0 recsep off verify off termout off
EOF


calling ftp

ftp -nv 147.132.19.52 < user userid password
prompt
asc
get file
bye
EOF


mail alerts

mailx -r "reply.to@host.com" -s "Subject" mail.to@host.com < file_contents


using varibles from command line

#!/bin/sh
# Example script to copy files to different hosts
# Assumes same user is on all other servers

if [ $# -lt 1 ]; then
echo "\nUsage: script file [newfile]\n"
exit 1
fi

file=$1
echo $file | grep -q '^/'
if [ $? -ne 0 ]; then
file=$PWD/$file
fi

if [ $# -gt 1 ]; then
newfile=$2
else
newfile=$file
fi
thishost=`uname -n`
user=`id -un`
hostlist="host1 host2"

if [ ! -f $file ]; then
echo "\nCannot copy $file (doesn't exist?)\n"
exit 1
fi


# Copy the file to all the (other) hosts

echo Copying $file to $newfile
for host in $hostlist; do
if [ $host != $thishost ]; then
echo "Copying to $host:"
scp $file ${user}@$host:$newfile
fi
done

No comments:

Splitting the home network

Who wouldn't want to separate the traffic at home for security reasons. The more common ones include wireless guest and wireless users. ...