【问题标题】:Why am I getting if: Expression Syntax when I try to run this script?为什么我在尝试运行此脚本时会收到 if: Expression Syntax?
【发布时间】:2016-04-04 00:34:00
【问题描述】:

我目前正在尝试在终端中运行一个 Unix 可执行文件(我的 shell 是 TCSH),我在线下载并不断收到以下错误:

if: Expression Syntax

这是我要运行的脚本:

if [ -f .1 ] 
then
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
chmod 700 xrdcalc
./xrdcalc
else
platform=`uname`
echo
echo
echo "You are using \"xrdcalc\" for the first time on $platform , read the \"Readme.txt\" file and then proceed"
echo
echo 
echo "Press enter...."
read char;
echo `date` > .1
mkdir .source
mv xrdcalc-1.1.c .source
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
./xrdcalc
fi

我几乎没有运行脚本的经验,我相信这很容易解决。

【问题讨论】:

  • 您可以尝试在if 声明中使用圆括号'( )',我认为是TCSH 吗?并且可能endif 关闭它?
  • 另见,tcsh script if statement,在 tcsh 中,您需要用括号代替方括号

标签: shell unix scripting terminal


【解决方案1】:

此代码存在其他问题,表明它是为传统的shbash 编码的。

只需将#!/bin/bash 作为第一行(使用您系统的bash 副本的正确路径),它应该可以在没有其他修改的情况下工作。

当然,

 chmod 755 scriptName

还需要将文件“标记”为可执行文件,如果文件保存到不在路径中的目录,您需要 cd 到正确的目录,或调用为

/full/path/to/scriptName

如果您使用的是未安装 bash 的精简版 Linux,并且由于某种原因无法安装,请寻找其他“Bourne Shell”派生的 shell 处理器,例如 ash, dash, ksh, sh

IHTH

【讨论】:

    猜你喜欢
    • 2016-06-30
    • 2022-01-15
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2021-07-18
    • 2022-06-13
    相关资源
    最近更新 更多