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.
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.
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.
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.
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
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.
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.