使用 nohup 将命令在后台运行时,某些内容会出现在终端中。
cp: error reading ‘/mnt/tt/file.txt’: Input/output error
cp: failed to extend ‘/mnt/tt/file.txt’: Input/output error
Run Code Online (Sandbox Code Playgroud)
我想将该内容保存到文件中。
有两种形式将标准输出和标准错误重定向到标准输出。但哪个更好?为什么&>被认为是完美的?
我找不到有什么区别,所以很多教程甚至 bash 手册都说 &>更好!
所以我为什么要使用&>而不是2>&1
主要使用bashshell
编辑:感谢评论者
只有 >& 适用于 csh 或 tcsh
在 ksh 中只有 2>&1 有效。
破折号仅使用 >file 2>&1 重定向
然后使用哪一个来确保我的脚本与其他系统兼容,无论使用的 shell 是什么!
我一直在自学 bash 脚本,但遇到了一个问题。我编写了一个脚本来使用“读取”命令从用户那里获取输入,并使该输入成为稍后在脚本中使用的变量。该脚本有效,但是......
我希望能够使用“对话框”进行设置。我发现
'dialog --inputbox' 会将输出定向到 'stderr',为了将该输入作为变量,您必须将其定向到一个文件,然后检索它。我发现解释这一点的代码是:
#!/bin/bash
dialog --inputbox \
"What is your username?" 0 0 2> /tmp/inputbox.tmp.$$
retval=$?
input=`cat /tmp/inputbox.tmp.$$`
rm -f /tmp/inputbox.tmp.$$
case $retval in
0)
echo "Your username is '$input'";;
1)
echo "Cancel pressed.";;
esac
Run Code Online (Sandbox Code Playgroud)
我看到它使用 2> 将 sdterr 发送到 /tmp/inputbox.tmp.$$,但输出文件看起来像“inputbox.tmp.21661”。当我尝试 cat 文件时,它给了我一个错误。所以我仍然无法从 --inputbox 获取用户输入作为变量。
示例脚本:
echo " What app would you like to remove? "
read dead_app
sudo apt-get remove --purge $dead_app
Run Code Online (Sandbox Code Playgroud)
所以你可以看到它是一个基本的脚本。甚至有可能将变量作为一个词从dialog --inputbox?
我有一个启动脚本行:
pyprogramm >> /dev/null 2>&1 &
Run Code Online (Sandbox Code Playgroud)
含义:
>> /dev/null - redirect stdout to null device
2>&1 - redirect stderr to stdout (that is redirected to null device)
Run Code Online (Sandbox Code Playgroud)
但最后一个&是什么意思?
我正在做作业,我有一个关于一行的问题。我不明白这>&2是什么意思:
if test -d $2
then echo "$2: Is directory" >&2 ; exit 21
fi
Run Code Online (Sandbox Code Playgroud) 在许多 Linux 书籍中都写到“在命令之前执行重定向”。让我们考虑一下:
$ cat bar
hello
$ ecno "foo" > bar
bash: ecno: command not found
$cat bar
Run Code Online (Sandbox Code Playgroud)
但是它并没有将任何输出复制到 bar 中(因为没有输出),所以应该说'重定向是在命令之前部分执行的',因为'>'的一部分在这里不起作用,即复制输出命令变成一个文件吧。这样对吗?
我见过很多这样写的 crontab 命令:
*/5 * * * * ~/script.sh >/dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)
有人能解释一下 的确切含义是什么吗2>&1?