spl*_*nux -1 unix macos bash zsh vector
首先(mac 使用它%来作为我~$在终端中的广告输入,因此我将使用 mac 约定来指示已启动的命令:
~% echo $SHELL
/bin/zsh
~% zsh --version
zsh 5.9 (x86_64-apple-darwin22.0)
Run Code Online (Sandbox Code Playgroud)
test.sh(vim 样式的行号以方便阅读):
1 #!/bin/bash
2 #
3 declare -A myvector
4 echo "wut?"
Run Code Online (Sandbox Code Playgroud)
我假设 zsh (我不知道如何使用)运行 bash (我生来就有它:_),但是
~% chmod u+x ./test.sh
~% ./test.sh
./test.sh: line 3: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
wut?
Run Code Online (Sandbox Code Playgroud)
然后执行:
~% . ./test.sh
wut?
Run Code Online (Sandbox Code Playgroud)
我假设 . zsh 是否启动脚本但没有 bash。
我在 unix.stackoverflow.com 和 Ask Different.stackoverflow.com 的 stackoverflow 中检查了有关如何在zsh和bash中使用声明、排版的各种问题
问题是man bash并man zshbuiltins给出所有不一致和无效的方式来使用关联数组。
为什么我不能声明向量并使用 bash 方式(并且正如 man zshbuiltins 所说我可以)。
以下内容绝对不起作用如何 在 Bash 中迭代关联数组
所以将其更改为
...
3 declare -a myvector
...
Run Code Online (Sandbox Code Playgroud)
所以初始化向量也不起作用
myvector=(a 0 b 0 c 0)
Run Code Online (Sandbox Code Playgroud)
也不
myvector=("a" 0 "b" 0 "c" 0)
Run Code Online (Sandbox Code Playgroud)
我真的很茫然……我应该再退出CS 5 年吗xD?
我假设 zsh (我不知道如何使用)运行 bash
不,zsh它们bash是 2 个完全不同的 shell,每个都有自己的语法、内置命令等。
mac 使用这个 % 来代替我以前的 ~$
不完全是,这只是您的PS1变量在您的.profile(或.bash_profile或.zsh_profile,无论您使用哪个)或系统中.profile默认设置的任何字符串。PS1='fluffy bunny '在命令行中键入并按 Enter 键即可看到它的更改。
我不知道zsh,但试试这个:
$ cat tst.sh
#!/usr/bin/env bash
declare -A assoc_arr=( ['foo']='bar', ['this']='that other thing' )
declare -p assoc_arr
echo ""
declare -a index_arr=( 'twas the' 'best of times' )
declare -p index_arr
Run Code Online (Sandbox Code Playgroud)
$ ./tst.sh
declare -A assoc_arr=([foo]="bar," [this]="that other thing" )
declare -a index_arr=([0]="twas the" [1]="best of times")
Run Code Online (Sandbox Code Playgroud)
bash这将使用在您的脚本中找到的第一个版本来运行您的脚本,PATH无论它是从zsh或bash或任何其他 shell 调用的。如果上述内容对您不起作用,那么bash您declare -A的PATH.
MacOS附带zsh作为默认登录 shell 和一个非常旧的版本,bash以避免一些许可问题,因此要么学习zsh,要么,如果您要使用bash,您应该安装较新的版本(谷歌“在 Mac 上使用 bash”或类似版本)。
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |