GVKun编程网logo

linux – bash:设置数组env变量并从任何shell脚本中取消引用它失败(linux shell 数组操作)

2

对于想了解linux–bash:设置数组env变量并从任何shell脚本中取消引用它失败的读者,本文将是一篇不可错过的文章,我们将详细介绍linuxshell数组操作,并且为您提供关于-bash:./

对于想了解linux – bash:设置数组env变量并从任何shell脚本中取消引用它失败的读者,本文将是一篇不可错过的文章,我们将详细介绍linux shell 数组操作,并且为您提供关于-bash: ./full_build.sh: /bin/bash^M: bad interpret、-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory、-bash: ./xxx.sh: /bin/bash^M: bad interpreter: No such file or directory、-bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory的有价值信息。

本文目录一览:

linux – bash:设置数组env变量并从任何shell脚本中取消引用它失败(linux shell 数组操作)

linux – bash:设置数组env变量并从任何shell脚本中取消引用它失败(linux shell 数组操作)

我以这种方式将数组设置为环境变量
例如.脚本test.sh

在test.sh中

#!/bin/bash
export STRING=( "str1" "str2" )

source test.sh

现在在脚本test-1.sh中

#!/bin/bash
echo ${STRING[0]}

反应没什么,只是一条空白,
然而,如果我尝试在test.sh中设置STRING =“str1”
并在test-1.sh中回显$STRING,这是有效的.

测试仅从root用户执行,
现在如何将数组设置为env变量,以便我可以根据需要调用数组元素?早些时候,我试图甚至修改/ etc / bashrc,这也没有产生任何积极的结果.

我需要将数组设置为env变量,因为我可能需要编写许多脚本,这些脚本将使用这些变量设置.

任何人都可以向我提出建议,以纠正我在哪里做错了吗?

解决方法

阅读精细手册,“错误”部分.

Array variables may not (yet) be exported.

虽然,我不知道很多人认为这是一个真正的错误.其他支持ksh样式数组的shell也不允许导出它们.

您可以通过参数或变量或环境轻松传递数组定义.但它通常不是很有用.

function f {
    unset -v "$2"
    typeset "$2"
    eval "${!1}"
    typeset -p "$2"
}

typeset -a a=(a b c)
myArr=$(typeset -p a) f myArr a

-bash: ./full_build.sh: /bin/bash^M: bad interpret

-bash: ./full_build.sh: /bin/bash^M: bad interpret

报错信息:

    执行一个脚本 full_build.sh 时,一直是提示我:

    -bash: ./full_build.sh: /bin/bash^M: bad interpreter: No such file or directory

解决方案:

    出现上面错误的原因之一是脚本文件是 DOS 格式的,即每一行的行尾以 \r\n 来标识,使用 vim 编辑器打开脚本,运行:

    :set ff?

    可以看到 DOS 或 UNIX 的字样。使用 set ff=unix 把它强制为 unix 格式的,然后存盘退出,即可.


                                                                                                                                    Name:Xr

                                                                                                                                    Date:2014-05-31 22:55


-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory

-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory

问题:

       偶然使用 windows 进行编写脚本。使用 wsl (windows subsystem for linux) 进行运行的时候,什么事情没有。但是当把脚本移植到远程服务器进行运行的时候,发现脚本执行出现了错误:-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory 。一时间解决不了。理论上都使用wsl 执行过了,应该是没问题了。但是通过查找资料发现,确实是有问题:

思考:

       应该是就是windows 中 CRLF 与 linux 或者Unix类系统中的 LF 之间的区别。这段时间听说,window 即将更正CRLF 这个换行。想必到时应该就没有上述的问题了。同时我们也可以证明发现。在windows subsystem for linux 中,其是兼容windows的 CRLF 的这种格式的。

解决方法:

      1. 使用 vim/vi 中的 命令

:set ff=unix

  2. 使用 bash 中的dos2unix 命令,来进行文件格式转换。这个操作可以在服务器上,也可以使用windows subsystem linux 完成。 如果系统中没有 dos2unix 工具,那么你可能需要使用 yum / apt / dnf / pacman 之类的 包管理软件,进行安装一下了。

DESKTOP-05DDFQ6# dos2unix runmake.sh
dos2unix: converting file runmake.sh to Unix format ...

 转载请注明出处. 2018年5月13日

 

-bash: ./xxx.sh: /bin/bash^M: bad interpreter: No such file or directory

-bash: ./xxx.sh: /bin/bash^M: bad interpreter: No such file or directory

一些人喜欢用vim来写linux shell script, 但是, 有的人喜欢在Windows下用一些方便的编辑器(比如鼎鼎大名的Notepad++)写好, 然后拷贝文件到linux下, 结果呢, 在执行脚本a.sh的时候, 会出现如下问题:

      什么原因呢, 我们有理由怀疑是文件格式问题? 我们用vim a.sh进入a.sh这个文件, 然后在底部模式下, 执行:set ff查看一下, 结果发现fileformat=dos, 看看, 果然是文件格式问题, 那怎么解决呢?

 

 

     方法一:vim a.sh进入a.sh后, 在底部模式下, 执行:set fileformat=unix后执行:x或者:wq保存修改。 然后就可以执行./a.sh运行脚本了。(我亲自试过, 是ok的)

     方法二:直接执行sed -i "s/\r//" a.sh来转化, 然后就可以执行./a.sh运行脚本了。(我亲自试过, 是ok的)

     方法三:直接执行dos2unix a.sh来转化, 然后就可以执行./a.sh运行脚本了。(我的linux上执行dos2unix ./a.sh失败, 但是不要放弃啊, 加个busybox就可以了), 如下:

 

-bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory

-bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory

-bash: /etc/init.d/nginx: /bin/bash^M:bad interpreter: No such file or directory


这个使为了弄 nginx 自启的,然后在官网找了个 shell 脚本发现不行啊。。。。。。

找啊找。。。。

解决

vi /etc/init.d/nginx






保持退出就行。。。

因为使复制的别人的脚本。。。。。。

所以在 Linux 中运行所以使 dos 格式的


ok,然后解决了

关于linux – bash:设置数组env变量并从任何shell脚本中取消引用它失败linux shell 数组操作的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于-bash: ./full_build.sh: /bin/bash^M: bad interpret、-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory、-bash: ./xxx.sh: /bin/bash^M: bad interpreter: No such file or directory、-bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory等相关知识的信息别忘了在本站进行查找喔。

本文标签: