Creating a file inside a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a file inside a script
# 1  
Old 03-21-2006
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
# 2  
Old 03-21-2006
You can do something like this.

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

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

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

Code:
log "Value is $value"

# 3  
Old 03-21-2006
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.
# 4  
Old 03-21-2006
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.
# 5  
Old 03-21-2006
Quote:
Originally Posted by sendhilmani
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
Code:
   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.

# 6  
Old 03-21-2006
The thing is that Its not creating the file if the file doesnt exist.
# 7  
Old 03-21-2006
We cant say what is wrong without seeing the script.

Anyway, you can use add this line as well.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating new file inside a for loop

Hi, I have list of files present in a folder. I want to search for a particular keyword sequentially and create a file which will be later used by some other program. Input files: $ ls a.dsx b.dsx c.dsx Dataline_.txt Dataline.txt loop.sh $ cat *.dsx help tyiis global for i in... (4 Replies)
Discussion started by: mac4rfree
4 Replies

2. Shell Programming and Scripting

Script to validate dates inside a file

Hi All, Looking forward a script with below requirement.Please let me know if any pointers. - there is a file which contains date - script will go inside the file and validate date is T-1 and for Friday T-3 - Send email with status -Shell is Ksh (2 Replies)
Discussion started by: dattatraya
2 Replies

3. Shell Programming and Scripting

Running .sh file inside a shell script

Hello, You might help a newbie like me, I am trying to run a .sh inside my shell script. After running that I need to execute below commands. Here's how my scripts looks like. Hope you can help: #!/bin/sh cd $ORACLE_HOME/owb/bin/unix ./OMBPlus.sh ---> goes to OMB+> directory cd... (10 Replies)
Discussion started by: aderamos12
10 Replies

4. Shell Programming and Scripting

Creating loops inside a file and extracting and loading data

Help needed (1 Reply)
Discussion started by: Chand Shrestha
1 Replies

5. UNIX for Dummies Questions & Answers

File operations are failing inside the script

Hi, does any one know the environmental parameter that I have to set so as to make sure the file operations run properly within the script. right now when I am doing a cat from within the script nothing happens, same is the case when I do a grep. when I am doing awk '{print $0 }' its printing... (1 Reply)
Discussion started by: ahmedwaseem2000
1 Replies

6. Shell Programming and Scripting

Need help in creating file restoration script from a backup script.

Hi all i am struggling in creating a restore of env files while doing applications clone. the first file i created for copying the important configurations file which is running perfect now for reverting the changes i mean when i am restoring these files to its original places i have to do... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

7. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

8. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

9. Shell Programming and Scripting

script to check for a condition inside a file

Hi I am writing a script file which sends the log files along with their size in a folder named log to a file called temp.log using the following cmd: ls -st 190_GSTV_HUX_003QISCGSK026** >> /home/user/temp.log the temp.log looks like this: 16 190_GSTV_HUX_003QISCGSK026_message070321.log ... (11 Replies)
Discussion started by: kiran1112
11 Replies

10. Shell Programming and Scripting

how to find Script file location inside script

I have to find out the file system location of the script file inside script. for example a script "abc.sh" placed anywhere in the file system when executed shold tell by itself the location of it. example #pwd / #./abc this is / #cd /root #./abc this is /root #cd / #/root/abc this... (10 Replies)
Discussion started by: asami
10 Replies
Login or Register to Ask a Question