微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在意外标记附近的语法错误'然后'

我input的代码与“Linux命令行:完整简介” (第369页)相同,但提示错误

line 7 `if[ -e "$FILE" ]; then`

代码是这样的:

#!/bin/bash #test file exists FILE="1" if[ -e "$FILE" ]; then if[ -f "$FILE" ]; then echo :"$FILE is a regular file" fi if[ -d "$FILE" ]; then echo "$FILE is a directory" fi else echo "$FILE does not exit" exit 1 fi exit

我想知道是什么引入了错误? 我如何修改代码? 我的系统是Ubuntu。

厨师的apt-get update和apt-get升级

如何在Centos 7上禁用mongodb的THP

如何枚举USB设备*和*读/写给他们?

如何在Shell中的variables内扩展variables

LD_PRELOAD堆栈和数据段的内存分配

哪些方法可以用来检测在Linux内核中被覆盖的内存?

在bash脚本中逐行阅读

如何减less生成的二进制文件的大小?

部署Java“并排”和工具的Linux标准?

检查文件夹大小是否大于特定大小的最快方法是什么?

if和[之间必须有一个空格,像这样:

#!/bin/bash #test file exists FILE="1" if [ -e "$FILE" ]; then if [ -f "$FILE" ]; then echo :"$FILE is a regular file" fi ...

这些(和他们的组合)也都是不正确的 :

if [-e "$FILE" ]; then if [ -e"$FILE" ]; then if [ -e "$FILE"]; then

另一方面这些都可以:

if [ -e "$FILE" ];then # no spaces around ; if [ -e "$FILE" ] ; then # 1 or more spaces are ok

顺便说一句,这些是相当的

if [ -e "$FILE" ]; then if test -e "$FILE"; then

这些也是等同的:

if [ -e "$FILE" ]; then echo exists; fi [ -e "$FILE" ] && echo exists test -e "$FILE" && echo exists

而且,你的脚本的中间部分对于这样的elif会更好:

if [ -f "$FILE" ]; then echo $FILE is a regular file elif [ -d "$FILE" ]; then echo $FILE is a directory fi

(我也在echo删除了引号,因为在这个例子中它们是不必要的)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐