gpt4 book ai didi

scr.sh: 3: Syntax error: word unexpected (expecting ")") [duplicate](Scr.sh:3:语法错误:单词意外(应为“)”)[重复])

转载 作者:bug小助手 更新时间:2023-10-25 21:47:27 26 4
gpt4 key购买 nike




I'm doing a task, but I got an error: scr.sh: 3: Syntax error: word unexpected (expecting ")"). When I try to run my schell script on my local computer server, it works, but on the helios ssh server it return an error. Here's my test code:

我正在执行一项任务,但收到一个错误:scr.sh:3:语法错误:Word意外(预期为“)”)。当我尝试在我的本地计算机服务器上运行我的shell脚本时,它可以工作,但在Helios ssh服务器上它返回一个错误。以下是我的测试代码:


#!/bin/bash

arr=(kitten dog parrot)
for i in ${arr[@]}
do
echo $i
done

I tried to declare array like this:
arr=("kitten" "dog" "parrot"), it was the same error.

我尝试这样声明数组:arr=(“小猫”“狗”“鹦鹉”),这是相同的错误。


Using declare -a arr didn't work

使用DECLARE-A ARR不起作用


I run it like this:

我这样运行它:


sh scr.sh

更多回答

I'm not familiar with Helios, but is it possible that your script is being run in a non-Bash shell that doesn't support arrays, or in a very old version of Bash that doesn't support arrays?

我不熟悉Helios,但您的脚本是否可能运行在不支持数组的非Bash外壳中,或者运行在不支持数组的非常旧版本的Bash中?

I use GitBash 2.42.0.2. Should I try another shell?

我使用的是GitBash 2.42.0.2。我应该再试一次贝壳吗?

You need to run it as bash. sh does not have arrays. Hence execute it as bash scr.sh, or make it executable (chmod +x scr.sh) and then run it as ./scr.sh, in which case your #! line would make sure that bash is invoked.

你需要把它当做bash来运行。Sh没有数组。因此,将其作为bash scr.sh执行,或者将其设置为可执行文件(chmod+x scr.sh),然后作为./scr.sh运行,在这种情况下,您的#!行将确保调用bash。

优秀答案推荐

The problem is how you run the script. You tell it to use sh instead of bash. Instead do

问题在于您如何运行脚本。您告诉它使用sh而不是bash。取而代之的是


bash scr.sh

or make the file executable

或将文件设置为可执行文件


chmod +x scr.sh

and then you can run it without manually specifying the scripting language every time:

然后您可以运行它,而无需每次手动指定脚本语言:


./scr.sh

更多回答

@Camilla_T You're welcome! I'm glad it helped!

@Camilla_T,不客气!我很高兴它起到了作用!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com