2

I have a bash script that reads filenames, takes a selection of data, build a table, and then adds the header. Unfortunately, at the point to add the header and give the output file, I have the following error message:

./big_table_rcp.sh: line 153: /tmp/out: Permission denied

It is linked with the following line:

| cat - out_${scenario}.txt > /tmp/out && mv /tmp/out ${gauge}_${scenario}.txt

Does anyone know how to give access to the output file?

2
  • 3
    You should probably use a unique filename. The problem is that /tmp/out was already used by someone else, and you can't overwrite their file.
    – Barmar
    Oct 12 '15 at 20:15
  • does /tmp/out already exist? can you write to it?
    – rogerdpack
    May 16 '19 at 21:39
8

You may be getting permission errors because you don't have permissions to access /tmp/out or the /tmp directory.

Before the offending line, include somehting like ls -l /tmp | grep out to see what permissions the /tmp/out file has.

In addition, instead of using /tmp/out, use mktemp.

tmpfile=`mktemp`
your code here | cat - out_${scenario}.txt > "$tmpfile" && mv "$tmpfile" ${gauge}_${scenario}.txt

From man mktemp:

Create a temporary file or directory, safely, and print its name.

4

Do not use /tmp/out, but rather /tmp/out.$$ this will not conflict in case of concurrent running, or leftover process, better still, use /tmp/out.${LOGNAME}.$$ which will allow for a user level cleanup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.