我正在使用亚马逊 Linux。我想在我的 node.js Selnium 测试中运行一个无头 Chrome 浏览器。所以我像这样启动我的 Xvfb 服务器......
if ! pidof /usr/bin/Xvfb; then Xvfb :0 -screen 5 1024x768x8 & fi
Run Code Online (Sandbox Code Playgroud)
但是,当我在测试失败后尝试截取屏幕截图时,
DISPLAY=:0 import -window root /tmp/screenshot.png
Run Code Online (Sandbox Code Playgroud)
我收到下面提到的错误...
+ DISPLAY=:0
+ import -window root /tmp/screenshot.png
import: unable to open X server `:0' @ error/import.c/ImportImageCommand/369.
Run Code Online (Sandbox Code Playgroud)
如何截取 Xvfb 缓冲区的屏幕截图?
我正在尝试将一个变量传递到 jq 中,到目前为止'.Linux.date.$var'
我已经尝试按名称引用它们,效果很好。但我想使用变量来调用它们。
我有这个,运行良好
exectime=$(date -d now);
cp $check_exec_history $check_exec_history.tmp
jq --arg key1 true --arg key2 "$exectime" --arg name "$name" '.Linux.script_executed.first = $key1 | .Linux.date_executed.first = $key2' $check_exec_history.tmp > $check_exec_history;
rm $check_exec_history.tmp;
Run Code Online (Sandbox Code Playgroud)
我想做到这一点,但没有工作:
name=first;
exectime=$(date -d now);
cp $check_exec_history $check_exec_history.tmp
jq --arg key1 true --arg key2 "$exectime" --arg name "$name" ".Linux.script_executed.$name = $key1 | .Linux.date_executed.$name = $key2" $check_exec_history.tmp > $check_exec_history;
rm $check_exec_history.tmp;
Run Code Online (Sandbox Code Playgroud)
我到目前为止:使用这个答案/sf/ask/2801917681/但我不确定我在哪里做错了。
name=first;
exectime=$(date -d now);
cp $check_exec_history $check_exec_history.tmp
jq --arg key1 true --arg key2 …
Run Code Online (Sandbox Code Playgroud) 我制作了一些脚本,其中包含一些设计需要 sudo 权限的功能。我在.bashrc
forLinux
和.bash_profile
for 中添加了这些路径,MacOS
以便可以从任何地方调用它。
但我不希望用户sudo
每次想要调用这些脚本函数时都键入。有什么方法可以暗示sudo
每当调用这些函数时,终端都会假设它是从 root 用户调用的?
我想我应该sudo -i
在脚本的开头添加还是在每个函数的开头添加?或者有没有其他替代方式来暗示sudo
?此外,很高兴知道您是否认为暗示 sudo 会很糟糕或危险,以及是否不推荐这样做。
dangerous-function
包含一些功能的脚本示例,我试图在不指定的情况下完成sudo
#!/bin/bash
start-one()
{
## do dangerous stuff with sudo
systemctl start dangerous.service
}
start-two()
{
systemctl start dangerous1.service
}
start-launchwizard()
{
systemctl start dangerous2.service
}
## Calling functions one by one...
"$@"
Run Code Online (Sandbox Code Playgroud)
我不想打电话给他们,sudo dangerous-function start-one
我只想打电话给他们,dangerous-function start-one
但仍然得到与前一个相同的结果。
这是一个简单的脚本,它卷曲https://unix.stackexchange.com/并将结果存储到一个数组中,它工作正常。
#!/usr/local/bin/bash
[ -f pgtoscrap ] && { rm pgtoscrap; };
curl -o pgtoscrap https://unix.stackexchange.com/;
declare -a arr;
fileName="pgtoscrap";
exec 10<&0
exec < $fileName
let count=0
while read LINE; do
arr[$count]=$LINE
((count++))
done
exec 0<10 10<&-
Run Code Online (Sandbox Code Playgroud)
但是,每次我运行这个脚本时;我收到错误的文件描述符的一些错误。
./shcrap
./shcrap: line 14: 10: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我想我不太明白如何exec
在循环中正确使用命令。有人可以解释一下吗?
-- 在mapfile
为 Bash 4实现之后更新它变得更简单 --
#!/usr/local/bin/bash
## Pass a parameter as e.g. ./linkscrapping.bash https://unix.stackexchange.com/
mapfile -t arr < <(curl -s $1); ## Doing exec stuff with …
Run Code Online (Sandbox Code Playgroud) bash ×3
shell ×2
bashrc ×1
chrome ×1
display ×1
exec ×1
function ×1
imagemagick ×1
jq ×1
json ×1
screenshot ×1
shell-script ×1
sudo ×1
ubuntu ×1
xvfb ×1