1>&2在shell中是什么意思? [英] What does 1>&2 mean in shell?

查看:60
本文介绍了1>&2在shell中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相当菜鸟的问题,这个脚本中的 1>&2 有什么作用?

Pretty noob question, what does the 1>&2 do in this script?

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

推荐答案

将This script must be run as root"从标准输出 (STDOUT) 重定向到标准错误输出 (STDERR).

That redirects the line "This script must be run as root" from standard out (STDOUT) to standard error output (STDERR).

这是一种向 STDERR 打印错误消息的简单方法 - 如果您从另一个脚本(如 crontab)运行 bash 脚本,这很重要,如果您从命令行直接运行它,则无关紧要,因为您的终端将同时显示 STDOUT和 STDERR.

It's a easy way to print an error message to STDERR - this matters if you run the bash script from another script (like crontab), matters much less if you run it from the command line driectly since your terminal will show both STDOUT and STDERR.

另见输出到标准错误的回声

这篇关于1>&2在shell中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆