Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

What will be the best way to create a temporary text file for bash output processing and delete the file after the process ends

I am trying to find the proper solution to delete the temporary file that I used to get the result of SELECT query of sqlplus using bash. As I can not create the desired output file as per the format, I first store the result in a file and then process it and append it to the target file. How should I name the temp file inorder to delete the exact file of the current bash process? I even named the temp file by appending the process id name, but the processes are so many that the same process id file can be created at the same time.

SQL_FILE=/sql/transaction.sql

#Create tep
Pid=$$
TEMP_FILE=${TEMP_FILE_PATH}/output_$Pid.txt

$ORACLE_HOME/bin/sqlplus ${ORA_USER}/${ORA_PASS}@${SQL_PLUS_IP}:${SQL_PLUS_PORT}/${ORA_SID} @${SQL_FILE} ${TEMP_FILE}

#After Processing
rm -f ${TEMP_FILE}

exit 0

The output_$Pid.txt temp file is used to spool the output of the sql query,.

question from:https://stackoverflow.com/questions/66059142/what-will-be-the-best-way-to-create-a-temporary-text-file-for-bash-output-proces

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Continuing from the comment, in addition to creating the temporary file and setting a trap to remove the temp file on termination, interrupt or exit, you should validate each step along the way. You should validate mktemp provides a zero exit code. You should validate that the temp file is in fact created. Then you can use the temp file with confidence that no intervening error took place.

A short example would be:

#!/bin/bash

tmpfile=$(mktemp)       ## create temporary file

[ "$?" -eq 0 ] || {     ## validate zero exit code from mktemp
    printf "error: mktemp had non-zero exit code.
" >&2
    exit 1
}

[ -f "$tmpfile" ] || {  ## validate temp file actually created
    printf "error: tempfile does not exist.
" >&2
    exit 1
}

## set trap to remove temp file on termination, interrupt or exit
trap 'rm -f "$tmpfile"' SIGTERM SIGINT EXIT

## Your Script Content Goes Here, below simply exercises the temp file as an example

## fill temp file with heredoc to test
cat > "$tmpfile" << eof
The temporary file was successfully create at: '$tmpfile'.
The temporary file will be removed by a trap function when this
script is terminated or interrupted, or when this script exits
normally.
eof

cat "$tmpfile"          ## output temp file to terminal

There are several ways to approach this. This is just a plain-Jane, non-exiting way to put the pieces together and the validations to ensure it all happens as you intend.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...