gpt4 book ai didi

linux - 转义 linux shell 参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:58 24 4
gpt4 key购买 nike

我正在尝试去除 shell 参数之间有空格的“{ }”字符。例如:

./cod4_start.sh hello 28960 "带空格的主机名"

一旦我将“HOSTNAME WITH SPACES”作为参数传递,即使它是双引号 shell 也会将主机名视为 {HOSTNAME WITH SPACES}。

是否有任何类型的其他参数传递给脚本,以便我的变量中没有 {} 字符?

代码如下:

#!/usr/bin/expect -f
set pssword [lrange $argv 0 0]
set port [lrange $argv 1 1]
set mod [lrange $argv 2 2]
set map [lrange $argv 3 3]
set num [lrange $argv 4 4]
set hostname [lrange $argv 5 5]
set rcon [lrange $argv 6 6]
set password [lrange $argv 7 7]
set gtype [lrange $argv 8 8]
set slots [lrange $argv 9 9]
spawn su - kod -c cd cod4 -c "nohup ./cod4_lnxded +set s_num=$num +set net_port $port +set dedicated 2 +set fs_game mods/$mod +set logfile 0 +set sv_punkbuster 1 +set sv_hostname \'$hostname\' +exec fastdl.cfg +set rcon_password $rcon +set g_password '$password' +set promod_mode match_mr10 +set g_gametype $gtype +set sv_maxclients $slots +set ui_maxclients $slots +map $map" > /dev/null 2>&1 &
expect "Password:" { send "$pssword\r" }
expect "# " { send "q" }
exit

最佳答案

我猜你应该在脚本中使用 lindex 而不是 lrange:

set hostname [lindex $argv 5]

否则 lrange 将返回一个列表,其中一个元素由带空格的字符串组成。在 TCL 符号中,这类似于 {string with spaces}

附录:

正如 schlenk 所建议的,您还可以使用 lassign(取决于您的 TCL 版本)

lassign $argv pssword port mod map num hostname rcon password gtype slots

foreach 技巧:

foreach {pssword port mod map num hostname rcon password gtype slots} $argv break

关于linux - 转义 linux shell 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9363824/

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