[英]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.