关于语法错误,如果[" $ foo的" =="酒吧和QUOT]在bash [英] syntax error on if["$foo" == "bar"] in bash

查看:118
本文介绍了关于语法错误,如果[" $ foo的" =="酒吧和QUOT]在bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的bash,但根据我看到一些流量控制指南,这个脚本应该只是寻找,但我得到附近意外的标记,然后 4号线语法错误

 #!/斌/庆典
回声键入您的姓名
阅读PERSONNAME
如果[$ PERSONNAME==凯文];然后
    回声你的名字是凯文
    1号出口
科幻


解决方案

在外壳, [是一个命令和] 是一个参数的命令。参数必须是单独的词,用空格分隔。除此之外,我还建议改变你的 == = (后者是更广泛的支持)。此外,你可以结合你的回声 using语句读-p

 #!/斌/庆典
阅读-p $键入您的姓名\\ n'PERSONNAME
如果[$ PERSONNAME=凯文];然后
    回声你的名字是凯文
    1号出口
科幻

$字符串\\ n'语法允许你使用字符,如新行 \\ n 在提示字符串。如果不真正想要一个换行符,你可以简单地使用类似输入您的姓名:和提示会出现在同一行用户输入<。 / p>

I am new to bash, but according to some flow control guides I saw, this script should work just find, but I get line 4 syntax error near unexpected token then

#!/bin/bash
echo "Type your name"
read personname
if["$personname" == "kevin"]; then
    echo "Your name is kevin"
    exit 1
fi

解决方案

In the shell, [ is a command and ] is an argument to that command. Arguments must be separate words, separated by white space. Aside from that, I would also suggest changing your == to = (the latter is more widely supported). Also, you can combine your echo and read statements using read -p.

#!/bin/bash
read -p $'Type your name\n' personname
if [ "$personname" = "kevin" ]; then
    echo "Your name is kevin"
    exit 1
fi

The $'string\n' syntax allows you to use characters such as the newline \n in the prompt string. If you don't actually want a newline, you could simply use something like 'Type your name: ' and the prompt would appear on the same line as the user input.

这篇关于关于语法错误,如果[&QUOT; $ foo的&QUOT; ==&QUOT;酒吧和QUOT]在bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆