Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux bash:使用sudo权限打开文件描述符_Linux_Bash_Sudo - Fatal编程技术网

Linux bash:使用sudo权限打开文件描述符

Linux bash:使用sudo权限打开文件描述符,linux,bash,sudo,Linux,Bash,Sudo,在我的脚本中,我想打开一个特定的(设备驱动程序)文件作为fd3。 exec 3您能做如下操作吗 # open the file with root privileges for reading exec 3< <(sudo cat /dev/tpm0) # read three characters from open file descriptor read -n3 somechars <&3 # read a line from the open file de

在我的脚本中,我想打开一个特定的(设备驱动程序)文件作为fd3。
exec 3您能做如下操作吗

# open the file with root privileges for reading
exec 3< <(sudo cat /dev/tpm0)

# read three characters from open file descriptor
read -n3 somechars <&3

# read a line from the open file descriptor
read line <&3

# close the file descriptor
exec 3<&-
#以root权限打开文件进行读取

exec 3<如果您向我们展示一些演示您试图解决的问题的代码,这将更容易回答。好的,我想要实现的是:*打开/dev/tpm0,它需要根权限*执行脚本需要执行的任何操作,对于用户权限*关闭/dev/tpm0,这背后的原因是我必须在脚本运行时阻止对/dev/tpm0的访问/dev/tpm0是一个阻止字符设备。为什么您只能以root用户身份打开fd?该文件由root用户和root组拥有,权限为0600。更改权限可能是另一种选择,但确实不是首选。谢谢larsks,这看起来非常接近我需要的-谢谢!还有两个问题——输入sudo的密码不起作用(但这可以通过事先使用sudo执行其他操作来避免),我不知道如何检测打开是否成功。正如您所说,使用
sudo
(包括(a))有多种方法来解决此问题允许在没有密码的情况下执行此特定命令,或者(b)在脚本的前面获取凭据,等等)。检测打开是否成功有点困难,因为我们没有从失败的i/o重定向中获得返回代码。我添加了几个选项。一个完全不同的解决方案是运行一个单独的进程(如
root
),该进程打开
/dev/tpm0
,并提供一些API(例如,通过unix套接字)这允许您的非根脚本以受控的方式访问它。我需要使用类似于
tail-f
的东西来保持它的打开状态,但如果成功,它将永远不会返回。。。如果您可以选择使用除
bash
之外的其他工具,则可以利用。
exec 3< <(sudo cat /dev/tpm0 || echo FAILEDCODE)
rm -f /tmp/itfailed
exec 3< <(sudo cat /dev/tpm0 || touch /tmp/itfailed)