LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash fail use file descriptor assigned a file that resides in /tmp (https://www.linuxquestions.org/questions/programming-9/bash-fail-use-file-descriptor-assigned-a-file-that-resides-in-tmp-4175726918/)

zeroone 07-13-2023 06:26 PM

Bash fail use file descriptor assigned a file that resides in /tmp
 
Does one experienced too that
Bash cannot use file descriptor assigned a file that resides in /tmp ?
i.e. inside script:
Code:

...
exec 3< <(/tmp/dls1)

while read -r -u3 f1; read -r f2
do
 # ...
done < <(cat /tmp/dls2)
...

output:
...

bash: /tmp/dls1: Permission denied


While the second file is read into f2 works. Please advise the wisest solution

michaelk 07-13-2023 07:25 PM

Code:

exec 3< <(/tmp/dls1)
I have not tested your code but using process substitution for the redirect is probably the cause of your script not working. Instead try the following:
Code:

#!/bin/bash

while read -r f1 && read -r -u 3 f2
do
  echo "$f1 $f2"
done <"/tmp/dls1" 3<"/tmp/dls2"

I assume your user has read permissions for both files?

NevemTeve 07-13-2023 11:30 PM

@OP Are these synonyms or not? If not, what is the difference?
Code:

exec 3</tmp/dls1
exec 3< <(/tmp/dls1)


pan64 07-14-2023 01:01 AM

Quote:

Originally Posted by zeroone (Post 6441919)
Bash cannot use file descriptor assigned a file that resides in /tmp ?

this is a definitely incorrect conclusion.

It can be a real permission issue (permission is really denied), but I think you missed the cat in the first line.
Code:

exec 3< <(cat /tmp/dls1)
But cat is not required at all (see uuoc), the previous suggestions are much better.

Additionally would be better to use shellcheck to catch errors like this.

michaelk 07-14-2023 04:17 AM

Quote:

Originally Posted by NevemTeve (Post 6441952)
@OP Are these synonyms or not? If not, what is the difference?
Code:

exec 3</tmp/dls1
exec 3< <(/tmp/dls1)


No, the first is a redirect and the second is process substitution. It isn't technically a syntax error so shellcheck will not report an error. I believe it fails because /tmp/dls1 isn't an executable and that as posted you are missing the cat command. Using process substitution is not required as I posted above.

zeroone 07-15-2023 01:36 AM

Quote:

Originally Posted by NevemTeve (Post 6441952)
@OP Are these synonyms or not? If not, what is the difference?
Code:

exec 3</tmp/dls1
exec 3< <(/tmp/dls1)


Solved by solution of your
Code:

exec 3</tmp/dls1
its synonym to corrected 2nd one: exec 3< <(cat /tmp/dls1)

MadeInGermany 07-16-2023 05:48 AM

Not a synonym,
but much more complex, and yes, with the same result.

Further, a lasting open with exec (until there is a close with another exec) is more complicated than redirecting a code block a la post#2
The loop is a code block; when redirected then the open occurs when the code block starts, and the close occurs when the code block ends.
BTW a { } forms a custom code block:
Code:

{
read line1
read line2
} </etc/group



All times are GMT -5. The time now is 11:00 PM.