【问题标题】:Why am I getting an if: Expression syntax error in ~/.profile?为什么我在 ~/.profile 中出现 if: Expression 语法错误?
【发布时间】:2021-01-25 10:38:06
【问题描述】:

当我尝试source ~/.profile 时,我的.profile 中出现'if: Expression syntax' 错误。我究竟做错了什么? .profile file

【问题讨论】:

  • 请注意,这与 Go 无关,因此我已从问题中删除了该标签。此外,您应避免使用图片,尤其是图片链接,而应尽可能使用文本,包括代码。
  • 链接图像中的if 块看起来没问题,并且ShellCheck 确认if 块没有问题;你能确认你在bash shell 下运行吗? ~/.profile 的其他地方或从属脚本中是否还有另一个 if 块?如果您像这样修改脚本:echo "1"; tty -s; if ... fi; echo "2" 并获取脚本,您是否看到由1 和2 括起来的错误消息?
  • @markp-fuso 系统回显 1 但立即显示 if:Expression 语法。 .profile 中只有 16 行
  • 请在您的问题中添加~/.profile。没有链接。
  • 代码(来自链接)看起来不错,并且在我的 bash 环境中运行正常;假设您在bash 下运行,并且您的文件是链接中该代码的精确副本,我正在画一个空白

标签: linux bash shell syntax profile


【解决方案1】:

链接中的代码看起来“正常”,在我的bash 环境中运行“正常”,并以“正常”@ShellCheck 检出。

从 OP 的 cmets 中,不会产生语法错误:

$ bash
$ source ./.profile
$                         <<<=== no error message, just a command prompt

这确认了 OP 的 .profile 的内容对于 bash 环境是“好的”。

从其他 cmets 中得知:

$ echo $SHELL
/bin/tcsh

问题就出在这里...tcsh 与bash 不同,这也意味着在tcsh shell 下执行时,OP 的.profile 中的某些命令无效。

如果我们去Try It Online,我们可以确认这一点,向下滚动并选择tcsh,输入以下代码...

tty -s
if test $? = 0
then
    stty dec crt
fi

...当我们运行代码时,我们得到:

if: Expression Syntax.

要解决这个语法问题,有几个选项:

  • OP 可以将默认 shell 切换为 bash(请参阅 chsh 或让系统管理员更新 /etc/passwd 以将 tcsh shell 替换为 bash shell);一旦完成,所有新的登录/shell会话应该没有问题采购OP的.profile
  • 或...
  • OP 可以在命令行手动运行bash,然后手动获取.profile

【讨论】:

  • 或者,OP可以为tcsh使用正确的配置文件。
猜你喜欢
  • 2022-01-20
  • 2018-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 2022-08-14
  • 2012-07-11
  • 2017-11-03
  • 2019-07-07
相关资源
最近更新 更多