-1

I have a script with multiple temp files. Multiple users can run the script. However, I need to avoid overwriting input files when multiple users run the script at the same time.

What is the best possible thing I can use?

cat inputfile | while read j
do
sed 's/beth/Beth/' >> temp1
cat temp2 | awk '{print $2}' >> temp2
cat temp2
done

the input file shoudn't override if other user update it before i run my script

Beth Mckey
  • 17
  • 6
  • 1
    This might help: `man mktemp` – Cyrus Mar 28 '19 at 05:21
  • Instead of input file , I need to use mktemp ? I should also delete them once the script runs for the next time & will it not override my input file & output as well when another user runs it at same time – Beth Mckey Mar 28 '19 at 05:31
  • Is first `temp2` a typo? – Cyrus Mar 28 '19 at 05:34
  • No, i have one i/p file .... 2 temp files (temp1 & temp 2) ..temp 2 being o/p file – Beth Mckey Mar 28 '19 at 05:36
  • Use `mktemp` to create temporary files with unique names (replacing temp1 and temp2). – Gordon Davisson Mar 28 '19 at 05:48
  • But still, my input file is a static one. When another user update that file before I run the script, it will take their values. Any solution? – Beth Mckey Mar 29 '19 at 04:25
  • @BethMckey Are you having the users copy their data to "inputfile" (overwriting whatever's there) before running the script? If so, don't do that. Have each user store their own data files separately, then supply the filename (for *their* input data) to the script as a parameter. And you *also* need to generate unique names for the temp files (i.e. `mktemp`). – Gordon Davisson Mar 29 '19 at 07:25
  • +1 @GordonDavisson , can u please show me an example of supply the filename (for their input data) to the script as a parameter. Also, if I use an array, will it be working to avoid overwriting ? – Beth Mckey Mar 30 '19 at 04:40
  • @BethMckey In the script, use something like `cat "$1"` or `somecommand <"$1"` (or `somecommand "$1" if the command allows it) to read the file (and also `mktemp` for temp files), and then run the script with something like `scriptname bethsinputfile` or `scriptname /path/to/bethsinputfile`. BTW, you should also use pipes instead of temp files where possible, like `sed 's/beth/Beth/' "$1" | awk '{print $2}'` – Gordon Davisson Mar 30 '19 at 05:52

1 Answers1

0

try this

TEMP=$(mktemp)                  # create unique tempfile
while read line; do             # read one line from inputfile
    # do what you want, e.g
    echo "($line)" >> $TEMP     # only an example
done < inputfile                # use inputfile in while-loop
cat $TEMP                       
rm $TEMP                        # remove tempfile
UtLox
  • 3,744
  • 2
  • 10
  • 13
  • You want to move the output redirect outside the loop, after the `done` so you don't open for appending, write, close on every iteration. (Of course in this case there is absolutely no need to collect standard output to a file so you can copy it to standard output.) – tripleee Mar 28 '19 at 05:55