Creating a file inside a script

Hi,

How can I create a file inside a script. The thing is that, I have to create the file inside the script and it will be acting as a log file the script.Whenever I execute the script the file has to be created and the output has to be directed into that file.

Tahnks in advance

You can do something like this.

#! /bin/ksh
[ ! -f "$0.log" ] && > $0.log

function log
{
  echo "$@" >> "$0.log"
}

Where ever you want to log the output, invoke the function as

log "Value is $value"

Hi ,
There are many was to create any file. Since your requirement is to create a file for keeping information log it would be advisable to have same name as the script name.

to create a new file use any one of the following :

> file_name
echo '' > filename

for appending any thing you can use " >> filename ".

Below is the sample code which would help you creating your own script.

script_name=$(basename "$0")
base_file_name=`echo $script_name | cut -f 1 -d "."`
logfile=${base_file_name}.log

echo " Started executing ${script_name} at `date` \n > ${logfile}

echo "Start appening msg to the file like this " >> ${logfile}

-Manish Jha.

Hi.

Thanks. I got it. But I have a script that has defined a log file like this.

The name of the script is verify.sh
Inside the script there is some thing like this.
LOG=/usr/verify
TDATE=`date "+%m%d%y"$$`
LOGFILE=$LOG.$TDATE.

and inside the script it has been written as
echo "This is to verify" | tee -a $LOGFILE.

I understood that for every run this script will generate the mog file with the date appended to it. But I am not getting to know whether I have to create the file before running the script or the script will itself create the file. If I run the script as it is, it doesnt create the file.

Please put some light on how to run this script.

Thanks in advance.

It will create the file if it doesnt exist. From info tee

   If a file being written to does not already exist, it is created.
If a file being written to already exists, the data it previously
contained is overwritten unless the `-a' option is used.

The thing is that Its not creating the file if the file doesnt exist.

We cant say what is wrong without seeing the script.

Anyway, you can use add this line as well.

[ ! -f "$LOGNAME" ] &&  > "$LOGNAME"

This is the script:

DEFINE_ENV_VAR()
{
. /usr/dacscan/bin/DSCANenv
LOG=/dacscan/words/shdsl_mrp_verify_eqpt
DEFINE_ENV_VAR()
{
. /usr/dacscan/bin/DSCANenv
LOG=/dacscan/words/shdsl_mrp_verify_eqpt
ORACLE_HOME=/opt/oracle
ORACLE_SID=S
export ORACLE_HOME
export ORACLE_SID
TDATE=`date "+%m%d%y"$$`
DEFINE_ENV_VAR()
{
. /usr/dacscan/bin/DSCANenv
LOG=/dacscan/words/shdsl_mrp_verify_eqpt
ORACLE_HOME=/opt/oracle
ORACLE_SID=S
export ORACLE_HOME
export ORACLE_SID
TDATE=`date "+%m%d%y"$$`
LOGFILE=$log.$TDATE
}
user=`echo $LOGNAME`
if [ $user != dacscan ]
then
echo "Please login as 'dacscan' to run this tool" > $LOGFILE
exit 1
else
echo "U r a valid user and now the check for single instance is going on" | tee -a $LOGFILE
echo "U r user validation is successful"
fi

if [ ! -f /tmp/shdsl_mrp_verify_eqpt.tmp ]
then
ps -ef | grep shdsl_mrp_verify_eqpt.sh | awk '{print $8}' > /tmp/shdsl_mrp_verify_eqpt.tmp
else
echo "Another instance of the tool is already running. Only one instance of the tool can be run at a given point of time" | tee -a $LOGFILE
echo "Cannot run the tool as already another instance of the tool is running"
exit
fi

Create_Proc()
{
create=`sqlplus -s dacscan/mnc <<!
set serveroutput on;
start shdsl_mrp_verify_eqpt.sql;
!`
if [[ $(echo $create | grep "ORA-") != "" ]]
then
echo "Error occured while creating the procedure. Check the Activity log file for further details" | tee -a $LOGFILE
rm /tmp/shdsl_mrp_verify_eqpt.tmp
exit 1
fi
}

Run_Proc()
{
status=`sqlplus -s dacscan/mnc <<!
set serveroutput on;
spool shdsl_mrp_verify_eqpt;
exec EQPT_VERIFY;
spool off;
!`
if (($?!=0))
then
echo "Unable to connect to oracle for running/executing the procedure" |tee -a $LOGFILE
rm /tmp/shdsl_mrp_verify_eqpt.tmp
exit 1
elif [[ $(echo $status | grep "ORA-") != "" ]]
then
echo "Error occured while executing the procedure" | tee -a $LOGFILE
rm /tmp/shdsl_mrp_verify_eqpt.tmp
exit 1
fi
}
rm /tmp/shdsl_mrp_verify_eqpt.tmp
DEFINE_ENV_VAR
Create_Proc
Run_Proc

Now tell me what do I have to do.

Thanks in advance

I assume your script starts like this and not as shown above.

DEFINE_ENV_VAR()
{
. /usr/dacscan/bin/DSCANenv
LOG=/dacscan/words/shdsl_mrp_verify_eqpt
ORACLE_HOME=/opt/oracle
ORACLE_SID=S
export ORACLE_HOME
export ORACLE_SID
TDATE=`date "+%m%d%y"$$`
LOGFILE=$log.$TDATE
}
user=`echo $LOGNAME`
if [ $user != dacscan ]
then
echo "Please login as 'dacscan' to run this tool" > $LOGFILE
exit 1
else
echo "U r a valid user and now the check for single instance is going on" | tee -a $LOGFILE
echo "U r user validation is successful"
fi

if [ ! -f /tmp/shdsl_mrp_verify_eqpt.tmp ]
then
ps -ef | grep shdsl_mrp_verify_eqpt.sh | awk '{print $8}' > /tmp/shdsl_mrp_verify_eqpt.tmp
else
echo "Another instance of the tool is already running. Only one instance of the tool can be run at a given point of time" | tee -a $LOGFILE
echo "Cannot run the tool as already another instance of the tool is running"
exit
fi

Create_Proc()
{
create=`sqlplus -s dacscan/mnc <<!
set serveroutput on;
start shdsl_mrp_verify_eqpt.sql;
!`
if [[ $(echo $create | grep "ORA-") != "" ]]
then
echo "Error occured while creating the procedure. Check the Activity log file for further details" | tee -a $LOGFILE
rm /tmp/shdsl_mrp_verify_eqpt.tmp
exit 1
fi
}

Run_Proc()
{
status=`sqlplus -s dacscan/mnc <<!
set serveroutput on;
spool shdsl_mrp_verify_eqpt;
exec EQPT_VERIFY;
spool off;
!`
if (($?!=0))
then
echo "Unable to connect to oracle for running/executing the procedure" |tee -a $LOGFILE
rm /tmp/shdsl_mrp_verify_eqpt.tmp
exit 1
elif [[ $(echo $status | grep "ORA-") != "" ]]
then
echo "Error occured while executing the procedure" | tee -a $LOGFILE
rm /tmp/shdsl_mrp_verify_eqpt.tmp
exit 1
fi
}
rm /tmp/shdsl_mrp_verify_eqpt.tmp
DEFINE_ENV_VAR
Create_Proc
Run_Proc

Please put your code within code tags for easy reading.

Yeah,
you are right. Actually I made a mistake while pasting it here. But I just want to know where am I going wrong as the file doesnt get created.

These line

echo "Please login as 'dacscan' to run this tool" > $LOGFILE
exit 1
else
echo "U r a valid user and now the check for single instance is going on" | tee -a $LOGFILE

will never go into $LOGFILE. Because, they appear before the invocation of DEFINE_ENV_VAR. $LOGFILE doesnt have a name till DEFINE_ENV_VAR is called.

Exactly. Just now I changed it and now its working fine. Thanks a lot.