1

I'm trying to append the address information to a file. I am getting an error message of

tee: /OR_595.txt: Permission denied

I'm using the following code to create the file.

cstates=($(awk -v FS=^ '{print $5}' "$1"))

for i in "${cstates[@]}"
do

    :

if [[ ! -f "./$2/$i/${i}_595.txt" ]]; then

    echo "Making ${i}_595.txt File"
    touch "./$2/$i/${i}_595.txt"
    chmod a+x "./$2/$i/${i}_595.txt"
    else echo "File ${i}_595.txt already exists"


fi
done

This code is writing to the file.

file_name="$1"

while IFS=^ read -r company_name address1 address2 city state zip phone
do

printf "Company Name: %s\nCompany Address: %s%s, %s, %s, %s\nCompany Phone Number: %s\n\n" \
      "${company_name}" "${address1}" "${address2}" "${city}" "${state}" "${zip}" "${phone}" | tee -a "${outputdir}/${state}_595.txt" > /dev/null

done < $file_name

I've checked the permissions on the each of the folders, subfolders and the file

drwxrwxr-x 56 jh78454 jh78454 4096 Feb 19 14:58 States
drwxrwxr-x 2 jh78454 jh78454 4096 Feb 19 15:14 WA
-rwxrwxr-x 1 jh78454 jh78454 0 Feb 19 15:14 WA_595.txt

I've looked at the permissions via WinSCP for several of the folders and files and they are all the same. Not sure why I'm getting permission denied error.

6
  • 2
    Where is ${outputdir} defined? Feb 19 '20 at 22:24
  • 2
    I search all of your code, but I can't find where you use tee. Feb 19 '20 at 22:27
  • Also edit question to tell us what operating system, and file-system you are using. Feb 19 '20 at 22:29
  • 6
    Inspect your error message. "outputdir" is undefined or empty. Consequently, it is trying to write to a file in the root directory, for Oregon. "/OR_595.txt" is a bit of a give-away. Feb 19 '20 at 22:35
  • Why are you using tee but discarding part of its output? That makes no sense - use printf "..." >> "${outputdir}/${state}_595.txt" instead.
    – roaima
    Mar 16 '20 at 20:24
3

Not sure why I'm getting permission denied error.

Apparently ${outputdir} expands to an empty string (the variable is not defined or empty) and ${state} expands to OR. This way ${outputdir}/${state}_595.txt expands to /OR_595.txt.

/OR_595.txt points to a file named OR_595.txt in the root directory /. This file probably doesn't exist. It's normal a regular user cannot create "random" files in /.

Define outputdir so ${outputdir}/${state}_595.txt points to a file you can write to. You're using tee -a so maybe the design is the file already exists. I guess the first snippet is supposed to create the file. It uses ./$2/$i/, so you need to set outputdir in the second snippet accordingly.

2
  • Correct, the ${outputdir} should have been $2 and it was missing a subfolder. $2/$5/${state}_595.txt is correct instead of ${outputdir}/${state}_595.txt I keep looking at the code and completely missed it.
    – Kyle Heath
    Feb 19 '20 at 22:47
  • Your use of tee in | tee -a "${outputdir}/${state}_595.txt" > /dev/null is not necessary. It appends a copy of the output to the file, and then discards the output. Shell appends to files with the >> operator. That whole pipeline extension could be written >> "${outputdir}/${state}_595.txt". In fact, that whole output could be moved outside the while block, like done < "$file_name" >> "${outputdir}/${state}_595.txt". That avoids the output being opened and closed for each line added. Feb 20 '20 at 0:00

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.