相关疑难解决方法(0)

使用()和$()执行一系列命令的区别

我目前正在尝试制作一个脚本来创建字节,这些字节将作为输入传输到 netcat。

这是脚本的想法:

(perl -e "print \"$BYTES\x00\";

cat file;

perl -e "print \"More bytes\"x16 . \"\r\n\"";) | netcat ip port
Run Code Online (Sandbox Code Playgroud)

我尝试使用 subshel​​l 和命令替换(例如使用 $())来执行命令。但是我不明白为什么使用命令替换时脚本的输出是错误的。我怀疑在执行多个命令时,命令替换会错误地管道输出。有人可以向我解释为什么会这样吗?

编辑

这是使用命令替换的变体:

$(perl -e "print \"$BYTES\x00\";

cat file;

perl -e "print \"More bytes\"x16 . \"\r\n\"";) | netcat ip port
Run Code Online (Sandbox Code Playgroud)

shell pipe shell-script command-substitution

13
推荐指数
2
解决办法
3264
查看次数

在命令行上有条件地设置变量

如果我的 Ubuntu 系统上的条件为真,我想设置一个变量。

这证明我的 if 语句是正确的:

$ (if [ 1 == 1 ]; then echo "hi there"; fi);
hi there
Run Code Online (Sandbox Code Playgroud)

这证明我可以设置变量:

$ a=1
$ echo $a
1
Run Code Online (Sandbox Code Playgroud)

这表明在 if 语句中设置变量不起作用:

$ (if [ 1 == 1 ]; then a=2; fi);
$ echo $a
1
Run Code Online (Sandbox Code Playgroud)

任何想法为什么?我所有的谷歌研究表明它应该像这样工作......

command-line shell

3
推荐指数
2
解决办法
3659
查看次数

什么时候需要多个选项的分组括号?

我正在使用find和grep命令。

当多个选项由“或”与-o标志和分组括号的使用连接时,以及何时不使用分组括号时,感到非常困惑。

使用时find,分组括号似乎是必要的

find $fdir ( -name *.texi -o -name *.org )
Run Code Online (Sandbox Code Playgroud)

使用时grep,不使用分组括号

grep --include "*.texi" --exclude "*.org"
Run Code Online (Sandbox Code Playgroud)

bash find options

3
推荐指数
3
解决办法
682
查看次数

在脚本末尾删除所有变量是一个好习惯吗?

在脚本末尾删除 shell 脚本中使用的变量是否好。

rm -rf $abc
rm -rf $def
Run Code Online (Sandbox Code Playgroud)

或者

unset $abc
unset $def
Run Code Online (Sandbox Code Playgroud)

等等。

这实际上是一个好习惯吗?

shell bash shell-script rm variable

1
推荐指数
2
解决办法
6798
查看次数