I have been trying to write a bash script to log dmidecode results in the a log file.
我一直在尝试编写一个bash脚本来记录日志文件中的dmidecode结果。
Till now, this is how far I got.
直到现在,这是我有多远。
#!/bin/bash
for i in `seq 0 5`; do
sudo su <<HERE
dmidecode -t $i >> system.log
HERE
done
When I execute the above script, it asks for my password. As soon as I enter the password, I get the following error 6 times.
当我执行上面的脚本时,它会询问我的密码。输入密码后,我会收到以下错误6次。
bash: line 1: system.log: Permission denied
Anybody has any idea on how to solve this and log the dmidecode details. Any help is much appreciated. Thanks
任何人都知道如何解决这个问题并记录dmidecode细节。任何帮助深表感谢。谢谢
1 个解决方案
#1
Your command as written should work, but it can be simplified:
您编写的命令应该可以工作,但可以简化:
#!/bin/bash
for i in {0..5}; do
sudo -s <<<"dmidecode -t $i >> system.log"
done
If the above doesn't work, let us know.
如果上述方法无效,请告知我们。
-
sudo -s
takes a shell command to execute as root from stdin[1], so there's no need forsu
; since the command is simple enough, I'm using a here-string (<<<
) instead of a here-document.-
Note how the output redirection,
>> system log
, is part of the command string, as it would otherwise happen in the context of the current user:-
sudo -s <<<"dmidecode -t $i" >> system.log
would actually produce the error you describe, if you executed it in a directory in which the current user is not allowed to create files.
sudo -s <<<“dmidecode -t $ i”>> system.log实际上会产生你描述的错误,如果你在不允许当前用户创建文件的目录中执行它。
-
- The upshot is that the resulting
system.log
file will be owned byroot
.
注意输出重定向>>系统日志是命令字符串的一部分,否则会在当前用户的上下文中发生:sudo -s <<<“dmidecode -t $ i”>> system.log将实际产生您描述的错误,如果您在不允许当前用户创建文件的目录中执行它。
结果是生成的system.log文件将由root拥有。
-
Note how the output redirection,
-
{0..5}
uses a brace expansion to drive the iteration, which is more efficient.
sudo -s接受一个shell命令从stdin [1]以root身份执行,因此不需要su;由于命令很简单,我使用的是here-string(<<<)而不是here-document。注意输出重定向>>系统日志是命令字符串的一部分,否则会在当前用户的上下文中发生:sudo -s <<<“dmidecode -t $ i”>> system.log将实际产生您描述的错误,如果您在不允许当前用户创建文件的目录中执行它。结果是生成的system.log文件将由root拥有。
{0..5}使用大括号扩展来驱动迭代,这样效率更高。
[1] sudo -s
also accepts a command as additional arguments, but such a command passed this way is limited to simple commands without redirections, so it wouldn't work in this case.
Additionally, you cannot pass such a command as a single string, which requires passing - and quoting - its arguments individually.
Older versions of sudo
- such as found on macOS up to 10.11 - did allow passing an entire shell command line as a single argument, but that should no longer be used - use stdin input.
[1] sudo -s也接受一个命令作为附加参数,但是这样传递的命令仅限于没有重定向的简单命令,所以在这种情况下它不起作用。此外,您不能将这样的命令作为单个字符串传递,这需要单独传递 - 并引用 - 它的参数。较旧版本的sudo(例如在macOS上发现的最高10.11)确实允许将整个shell命令行作为单个参数传递,但不应再使用它 - 使用stdin输入。
#1
Your command as written should work, but it can be simplified:
您编写的命令应该可以工作,但可以简化:
#!/bin/bash
for i in {0..5}; do
sudo -s <<<"dmidecode -t $i >> system.log"
done
If the above doesn't work, let us know.
如果上述方法无效,请告知我们。
-
sudo -s
takes a shell command to execute as root from stdin[1], so there's no need forsu
; since the command is simple enough, I'm using a here-string (<<<
) instead of a here-document.-
Note how the output redirection,
>> system log
, is part of the command string, as it would otherwise happen in the context of the current user:-
sudo -s <<<"dmidecode -t $i" >> system.log
would actually produce the error you describe, if you executed it in a directory in which the current user is not allowed to create files.
sudo -s <<<“dmidecode -t $ i”>> system.log实际上会产生你描述的错误,如果你在不允许当前用户创建文件的目录中执行它。
-
- The upshot is that the resulting
system.log
file will be owned byroot
.
注意输出重定向>>系统日志是命令字符串的一部分,否则会在当前用户的上下文中发生:sudo -s <<<“dmidecode -t $ i”>> system.log将实际产生您描述的错误,如果您在不允许当前用户创建文件的目录中执行它。
结果是生成的system.log文件将由root拥有。
-
Note how the output redirection,
-
{0..5}
uses a brace expansion to drive the iteration, which is more efficient.
sudo -s接受一个shell命令从stdin [1]以root身份执行,因此不需要su;由于命令很简单,我使用的是here-string(<<<)而不是here-document。注意输出重定向>>系统日志是命令字符串的一部分,否则会在当前用户的上下文中发生:sudo -s <<<“dmidecode -t $ i”>> system.log将实际产生您描述的错误,如果您在不允许当前用户创建文件的目录中执行它。结果是生成的system.log文件将由root拥有。
{0..5}使用大括号扩展来驱动迭代,这样效率更高。
[1] sudo -s
also accepts a command as additional arguments, but such a command passed this way is limited to simple commands without redirections, so it wouldn't work in this case.
Additionally, you cannot pass such a command as a single string, which requires passing - and quoting - its arguments individually.
Older versions of sudo
- such as found on macOS up to 10.11 - did allow passing an entire shell command line as a single argument, but that should no longer be used - use stdin input.
[1] sudo -s也接受一个命令作为附加参数,但是这样传递的命令仅限于没有重定向的简单命令,所以在这种情况下它不起作用。此外,您不能将这样的命令作为单个字符串传递,这需要单独传递 - 并引用 - 它的参数。较旧版本的sudo(例如在macOS上发现的最高10.11)确实允许将整个shell命令行作为单个参数传递,但不应再使用它 - 使用stdin输入。