我正在尝试将 Expect 脚本实现到 bash 脚本中。请耐心等待,因为我对 bash/expect 还很陌生。
这是按预期工作的期望脚本:
log_user 0
file delete foo.txt
set fh [open foo.txt a]
set servers {xxx@server1 xxx@server2}
foreach s $servers {
spawn ssh $s
expect "password: "
send "PASSWORD\r"
expect "$ "
send "grep "something" /some/log/file.log"
expect "$ " { puts $fh "$expect_out(buffer)"}
send "exit\r"
}
close $fh
Run Code Online (Sandbox Code Playgroud)
现在,我希望将此 Expect 脚本包含在 bash 脚本中,但它没有按预期工作。
这是我到目前为止所拥有的:
#!/bin/bash
XYZ=$(expect -c "
file delete foo.txt
set fh [open foo.txt a]
set servers {xxx@server1 xxx@server2}
foreach s $servers {
spawn …Run Code Online (Sandbox Code Playgroud) 我正在使用 http-server 来将http://localhost:8484加载到特定文件夹上。(出于测试目的)我在代码中运行的操作系统命令是:
http-server -p 8484 test/
Run Code Online (Sandbox Code Playgroud)
下载完我运行的任何内容后:
http-server stop
Run Code Online (Sandbox Code Playgroud)
然而,测试完成后,我看到端口8484的http服务器仍然存在!
通过跑步ps aux | grep http
我应该运行什么命令才能停止它?
我在 erlang 中的代码:
my_test_() ->
Pid = spawn(fun() ->
Info = os:cmd("http-server -p 8484 test/resources"),
io:format(user,"*** Server: ~p~n",[Info])
end),
%%% Do some job %%%
Pid2 = spawn(fun() ->
Info = os:cmd("http-server stop"),
timer:sleep(200),
io:format(user,"*** Server stop: ~p~n",[Info])
end),
timer:sleep(200),
exit(Pid2, kill),
exit(Pid, kill).
Run Code Online (Sandbox Code Playgroud) 我正在尝试从 php 执行 bash 脚本并实时获取其输出。
我正在应用在这里找到的答案:
然而他们不为我工作。
当我以这种方式调用 .sh 脚本时,它工作正常:
<?php
$output = shell_exec("./test.sh");
echo "<pre>$output</pre>";
?>
Run Code Online (Sandbox Code Playgroud)
然而,当做:
<?php
echo '<pre>';
passthru(./test.sh);
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
或者:
<?php
while (@ ob_end_flush()); // end all output buffers if any
$proc = popen(./test.sh, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
@ flush();
}
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
我的浏览器中没有输出。
我还尝试在这两种情况下调用变量而不是脚本,我的意思是:
<?php
$output = shell_exec("./test.sh");
echo '<pre>';
passthru($output);
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
这是我的 test.sh 脚本:
#!/bin/bash
whoami
sleep 3
dmesg
Run Code Online (Sandbox Code Playgroud) 我试图用另一个文件中的位置或行号替换一个文件中的行,其中包含第一个文件的成员的唯一元素。
例如
file1.txt
----------
A
A
A
B
B
C
C
C
Run Code Online (Sandbox Code Playgroud)
第二个文件是
labels.txt
----------
A
B
C
Run Code Online (Sandbox Code Playgroud)
然后我试图获得以下输出:
1
1
1
2
2
3
3
3
Run Code Online (Sandbox Code Playgroud)
我尝试了一些变体grep,但没有任何一个可以正确执行此操作。
获取 labels.txt 中 file1.txt 中每个字母的行号可以正常工作:grep -n A labels.txt或grep -n B labels.txt
但是当我尝试使用这两个文件作为输入时:grep -n file1.txt labels.txt控制台上没有输出。
这在 Python 或其他语言中当然非常简单,但是它的 shell 版本会非常有用。(在从各种数据集的字符串制作分类标签时经常会出现这样的情况,这些标签可用于训练分类模型等。 )。
我有一个 Makefile,需要使用 awk 从文本文件 `someText.txt 中提取一些信息。这是一个例子:
在控制台中手动对我的文本文件执行 awk 效果很好:
$ cat someText.txt | awk '/Total number:/ {print $NF}'
$ 42
Run Code Online (Sandbox Code Playgroud)
但在我的 makefile 中使用它不起作用:
MYVAR=$(shell cat someText.txt | awk '/Total number:/ {print $NF}')
all:
@echo Count: $(MYVAR)
Run Code Online (Sandbox Code Playgroud)
输出是:Count:
但我建议输出是Count: 42
看起来set -v在子 shell 中不起作用?为什么?
(set -x确实可以在子 shell 中工作,但输出有点忙)。
$ cat foo.sh
set -v
date
set +v > /dev/null 2>&1
$ bash foo.sh
date
Fri Mar 3 14:52:34 PST 2017
set +v > /dev/null 2>&1
$ cat foo_subshell.sh
(
set -v
date
)
$ bash foo_subshell.sh
Fri Mar 3 14:52:42 PST 2017
Run Code Online (Sandbox Code Playgroud) 我感兴趣的是在 bash 脚本中使用动态生成的参数调用一些函数。
但是,如果某些参数有空格,则它似乎不起作用。
让我们看一下这个测试脚本:
#!/bin/bash
# file uu.sh
[[ -z $1 ]] || echo '$1' $1
[[ -z $2 ]] || echo '$2' $2
[[ -z $3 ]] || echo '$3' $3
[[ -z $4 ]] || echo '$4' $4
[[ -z $5 ]] || echo '$5' $5
[[ -z $6 ]] || echo '$6' $6
[[ -z $7 ]] || echo '$7' $7
[[ -z $8 ]] || echo '$8' $8
[[ -z $9 ]] || echo '$9' …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Shellcheck 对 shell 脚本进行静态代码分析。我想知道用于执行分析的规则。我在哪里可以得到它们?
以下是我从简单的 helloworld 程序获得的示例输出:
In C:\Users\~\Desktop\hello.sh line 1:
#!/bin/sh
^-- SC1017: Literal carriage return. Run script through tr -d '\r' .
In C:\Users\~\Desktop\hello.sh line 2:
# This is a comment!
^-- SC1017: Literal carriage return. Run script through tr -d '\r' .
Run Code Online (Sandbox Code Playgroud) 如果 ICMP 回显失败,我想忽略此 IF 语句中的错误。
示例代码:
if ping -q -c 1 -W 1 1.2.3.4 >/dev/null; then
echo -e "PING OK"
else
echo -e "PING NOK"
fi
Run Code Online (Sandbox Code Playgroud)
如果 ping 成功或者您在脚本之外运行命令,它会完美运行,但如果没有响应,则会给出以下输出。
PING 1.2.3.4 (1.2.3.4): 56 data bytes
--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100.0% packet loss
PING NOK
Run Code Online (Sandbox Code Playgroud)
我已经看到了引用 2>/dev/null 的答案,但这会在输出中显示整个 ping 查询,无论成功与否!2>/dev/null 的示例如下。
PING 1.2.3.4 (1.2.3.4): 56 data bytes
--- 1.2.3.4 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = …Run Code Online (Sandbox Code Playgroud) 这是我关于堆栈溢出的上一个问题的后续问题。我会将脚本缩减为基本部分,但如果这样认为,了解脚本的作用可能会有所帮助,您可以研究其他问题。
#!/usr/bin/env bash
set -eu -o pipefail
declare -a framelist
#Print all results
function output_values() {
echo "Results!";
}
#parsing information from stdin
function parser () {
while read tc;
do
if [ -z "$tc" ]; then
continue
fi
#Evaluation and saving result to array
echo $result_value;
framelist+=($result_value);
if (( <<some abort condition>> )); then
exec 0>&-
echo "Last result: $result_value";
return 0
fi
done
}
some_command_writing_to_stdout | parser $2;
output_values;
Run Code Online (Sandbox Code Playgroud)
该脚本执行命令并将输出通过管道传输到我的本地函数,该函数最终按echo "Last result: $result_value";预期返回该行的结果。此后,它将终止提供在此函数中解析的数据的命令 - 这也有效。 …
shell ×10
bash ×7
linux ×2
awk ×1
debugging ×1
dev-null ×1
erlang ×1
execution ×1
expect ×1
httpserver ×1
if-statement ×1
localhost ×1
makefile ×1
php ×1
ping ×1
shell-exec ×1
shellcheck ×1
stdout ×1