Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在zsh脚本中传播第n个参数之后的所有参数_Bash_Zsh - Fatal编程技术网

Bash 在zsh脚本中传播第n个参数之后的所有参数

Bash 在zsh脚本中传播第n个参数之后的所有参数,bash,zsh,Bash,Zsh,如果我有这样的剧本 curl --socks5-hostname "127.0.0.1:${2-1080}" $1 我可以做到: purl bing.com 1081 意思是: curl --socks5-hostname "127.0.0.1:1081" bing.com 现在,我想动态添加新参数,如: purl bing.com 1081 --connect-timeout 1 我怎样才能做到呢 如果我使用 curl --socks5-hostname "127.0.0.1:${2

如果我有这样的剧本

curl --socks5-hostname "127.0.0.1:${2-1080}" $1 
我可以做到:

purl bing.com 1081
意思是:

curl --socks5-hostname "127.0.0.1:1081" bing.com
现在,我想动态添加新参数,如:

purl bing.com 1081 --connect-timeout 1
我怎样才能做到呢

如果我使用

curl --socks5-hostname "127.0.0.1:${2-1080}" $1 "$@"
然后它将结束为:

curl --socks5-hostname "127.0.0.1:1081" bing.com bing.com 1081 --connect-timeout 1
这不是理想的结果

我想:

curl --socks5-hostname "127.0.0.1:1081" bing.com --connect-timeout 1
使用


您可以将脚本更改为:

p=("$@")
curl --socks5-hostname "127.0.0.1:${p[1]-1080}" ${p[0]} "${p[@]:2}"
可以使用shift使用主机和可选端口,以便将其余参数传递给curl


这将是一个相对简单的使用shift 2的方法,在使用之前从$@删除主机和端口,除了$2是可选的。我是否应该假设如果有两个或更多参数,则始终指定端口?@chepner yes。但好的一点是,我们可以检查$2是否是一个数字,如果不是,那么我们假设没有指定端口,并使用移位1而不是移位2吗?我们可以。我将写下这一点和我的另一个想法。如果您有阵列支持,几乎可以肯定您能够在不需要阵列的情况下分割$@本身。这将在purl bing.com-connect timeout 1;我要求OP澄清这是否是一个可能的用例。如果只有一个参数,shift也无法转换任何内容,因此$host将被复制。
host=$1
port=${2:-1080}
shift 2
curl --socks5-hostname "127.0.0.1:$port" $host "$@"
p=("$@")
curl --socks5-hostname "127.0.0.1:${p[1]-1080}" ${p[0]} "${p[@]:2}"
host=${1:-Missing host}  # Exits if *no* arguments available.
shift

if [[ $1 =~ ^[0-9]+ ]]; then
    port=$1
    shift
else
    port=1080
fi

curl --sock5-hostname "127.0.0.1:$port" "$host" "$@"