为什么使用./shell.sh得到错误,但是。 shell.sh作品 [英] How come using ./shell.sh get error but . shell.sh works

查看:146
本文介绍了为什么使用./shell.sh得到错误,但是。 shell.sh作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个shell脚本:

VAR=(aa bb cc)

for i in "${VAR[@]}"
do
        echo $i;
done

在使用运行。 ar_test.sh ,它的工作原理。

zhangyf@zhangyf-desktop:~/test$ . ar_test.sh 
aa
bb
cc

但以这种方式出现故障时,

but fails in this way,

zhangyf@zhangyf-desktop:~/test$ ./ar_test.sh 
./ar_test.sh: 9: Syntax error: "(" unexpected

有文件中的其他行,因此9号线实际上是VAR =(AA BB CC)。我知道其中的差别是,后者叉一个新的shell进程而前者的运行在当前shell脚本,但为什么结果不同,这么多呢?

There are other lines in the file, so line 9 is actually VAR=(aa bb cc). I know the difference is that the latter forks a new shell process while the former ones run the script in the current shell, but why does the result differs so much?

推荐答案

的区别是不是一个岔路口,但不同的炮弹。 源在当前shell文件和 ./ ar_test.sh 运行可执行文件默认的shell(/ bin / sh的) ,其可能不支持阵列。使用家当作为脚本的第一行,指定适当的外壳:

The difference is not a fork, but different shells. . sources file in the current shell and ./ar_test.sh runs executable with default shell (/bin/sh), which may not support arrays. Use shebang as the first line of your script to specify proper shell:

#!/bin/bash
...other code goes here...

这篇关于为什么使用./shell.sh得到错误,但是。 shell.sh作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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