简体   繁体   English

在.bashrc中使用`uname`时,为什么会出现错误?

[英]Why am I getting an error when I use `uname` in my .bashrc?

In my .bashrc I have the following code 在我的.bashrc中,我有以下代码

if [`uname` == "Linux"]; then
    echo "It worked"
else
    echo "It didn't work"
fi

But when I source my .bashrc I get the following results 但是当我获取我的.bashrc时,得到以下结果

[Linux: command not found [Linux:找不到命令

It didn't work 没用

Strangly, the [ is not a typo, it is part of the error. 遗憾的是, [不是错别字,它是错误的一部分。 If I comment out the if-statement, then the error goes away, so I am pretty sure that it is the source of the error. 如果我注释掉if语句,那么错误就会消失,因此我很确定它是错误的根源。 Plus, if I change the Linux to linux , then the error changes to lowercase also. 另外,如果我将Linux更改为linux ,那么错误也会更改为小写。

And if I echo uname I get Linux. 如果我回应uname我将获得Linux。

To source my .bashrc I have used source .bashrc and also have started a new bash session by typing bash on the command line terminal. 要获取我的.bashrc,我使用了source .bashrc并且还通过在命令行终端上键入bash来启动了一个新的bash会话。

I didn't think it was that hard to check for the OS type, but I can't seem to figure out the correct syntax for the .bashrc. 我不认为检查OS类型不是那么难,但是我似乎无法找出.bashrc的正确语法。

I don't see what I am doing wrong, can anyone help? 我看不到我在做什么错,有人可以帮忙吗?

You forgot a space after the square brackets. 您忘记了方括号后的空格。 The first line has to look like this: 第一行必须看起来像这样:

if [ `uname` == "Linux" ]; then

In your version, without the spaces, the [ and the output of uname is concatenated into one executable named [Linux , which does not exist in the PATH. 在您的版本中,没有空格, [uname的输出被串联到一个名为[Linux可执行文件中,该文件在PATH中不存在。

Bash in finicky about spacing. 对间距的挑剔。 You need spaces in your conditional 您的条件中需要空格

if [ `uname` == "Linux" ]; then
    echo "It worked"
else
    echo "It didn't work"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在我的 UDP 服务器上尝试使用 sendto() 时会收到错误地址错误? - Why am I getting a bad address error when trying to use sendto() on my UDP server? 为什么在编写 linux 服务时出现 Exec 格式错误? - why am I getting Exec format error when I am writing my linux service? 为什么在 JVM 下运行时,我的 Ada 共享库会出现“存储错误” - Why am I getting “storage error” on my Ada shared library when running under a JVM 为什么我的bash shell scipt中出现此错误 - Why am I getting this error in my bash shell scipt 尝试“捕获” char设备驱动程序时,为什么会收到错误消息? - Why am I getting an error message when trying to “cat” my char device driver? 咨询:为什么我的OSX终端上出现了clang错误? - Inquiry: Why am I getting a clang error on my OSX terminal? 为什么在尝试使用sed命令删除字符串时出现此错误? - Why am I getting this error when trying to use sed command to delete string? 为什么我按照预期会出现图像大小的opencv错误? - Why am I getting an opencv error with size of images when they are as expected? 当我尝试 gcc make linux-headers-`uname -r` 时,我遇到了错误 - While I'm trying gcc make linux-headers-`uname -r` I'm getting error 我收到SegFault错误,但是为什么呢? - I am getting a SegFault error, but why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM