AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 459664
Accepted
RobotJohnny
RobotJohnny
Asked: 2018-08-01 09:26:55 +0800 CST2018-08-01 09:26:55 +0800 CST 2018-08-01 09:26:55 +0800 CST

数组的索引范围不允许您在 bash 中迭代新行

  • 772

我正在研究一个简单的 bash 脚本,该脚本修复了一个重复的命名问题。

该脚本本身会抓取在 gameslist.xml 文件中多次提及的任何名称,然后将这些名称存储在一个数组中以供以后使用。

我最终在索引中循环这个数组,如下所示:

pi@retropie:~ $ for game in ${game_array[@]:0:10} ; do echo $game; done

它将第一个元素拉到第 10 个元素(即${game_array[9]}),但是输出连接到一行:

pi@retropie:~ $ for game in ${game_array[@]:0:10} ; do echo $game; done
R.B.I. Baseball '94 World Series Baseball '95 Mega Games 1 Bill Walsh College Football T2: The Arcade Game Sonic & Knuckles + Sonic the Hedgehog Sega Top Five Pyramid Magic Tecmo Super Baseball Super Chinese Tycoon

但是,如果我遍历整个数组,它会按预期在新行上输出:

pi@retropie:~ $ for game in ${game_array[@]}; do echo $game; done | head -10
R.B.I. Baseball '94
World Series Baseball '95
Mega Games 1
Bill Walsh College Football
T2: The Arcade Game
Sonic & Knuckles + Sonic the Hedgehog
Sega Top Five
Pyramid Magic
Tecmo Super Baseball
Super Chinese Tycoon

字段分隔符已设置为新行,IFS='$\n'这就是第二个有效的原因,但我一生都无法弄清楚为什么它不适用于第一个?

这是上下文的完整测试脚本:

#!/bin/bash

user_input=$1
while [ -z "$user_input" ]; do
        echo "please enter the name of the system you want to fix the game list for"
        echo "(as it is labelled in /home/pi/RetroPie/roms)"
        read -r user_input
done

ls "/home/pi/RetroPie/roms/$user_input" >/dev/null 2>&1

if  [ "$?" -ne 0 ]; then
        echo "this doesn't appear to be a system installed here. exiting."
        exit 1
fi

games_to_fix()
{
        IFS=$'\n'
        console=$1
        filepath="/opt/retropie/configs/all/emulationstation/gamelists/$console/gamelist.xml"
        game_array=($(fgrep "<name>" "$filepath" | sort | uniq -c | sort -rn | awk  '$1 > 1 {print $0}'| cut -d ">" -f 2 | cut -d "<" -f 1))
        number_to_fix=($(fgrep "<name>" "$filepath" | sort | uniq -c | sort -rn | awk  '$1 > 1 {print $1}'))
}

get_new_name()
{
        mYpath=$1
        new_name=$(echo $mYpath | cut -d ">" -f 2 | cut -d "<" -f 1 | sed -e 's/\.\///g' | sed -e 's/\.7z//g')
}

games_to_fix $user_input

IFS=$'\n'
index=0
for i in ${number_to_fix[@]}; do
        loop=1
        for game in ${game_array[@]:$index:$i}; do
        #       for ind in $(eval echo {1..$i}); do
                line_number=$(fgrep -n "<name>$game</name>"  $filepath | awk '{print $1}' | cut -d : -f 1 | sed -e "${loop}q;d")
                path_line_number=$(expr $line_number - 1 )
                path=$(sed "${path_line_number}q;d" $filepath | cut -d : -f 2)
                get_new_name "$path"
                sed -i "${line_number}s/$game/$new_name/g" $filepath
                ((loop++))
        done
        index=$(expr index + $i);
done
bash shell
  • 2 2 个回答
  • 644 Views

2 个回答

  • Voted
  1. Best Answer
    ilkkachu
    2018-08-01T11:01:43+08:002018-08-01T11:01:43+08:00

    简而言之:除非您明确需要字段/单词拆分,否则您应该在这样的数组扩展周围使用引号。"$@"将每个位置参数扩展为一个单独的单词,"${a[@]}". 通过扩展,这应该以相同的方式为"${a[@]:0:2}".


    也就是说,在 Bash 中似乎仍然存在不一致,并且您使用的内容应该适用于您的情况(因为值中没有 glob 字符,并且通过IFS正确设置来处理字段拆分)。

    采取完整的阵列工作:

    $ IFS=$'\n'
    $ a=("foo bar" "asdf ghjk")
    $ printf "%s\n" ${a[@]}
    foo bar
    asdf ghjk
    

    切片不适用于数组,但适用于$@:

    $ printf "%s\n" ${a[@]:0:2}
    foo bar asdf ghjk
    
    $ set -- "aa bb" "cc dd"
    $ printf "%s\n" ${@:1:2}
    aa bb
    cc dd
    

    它在 ksh 和 zsh 中确实有效,这强调了 Bash 在这里不一致(zsh 当然有它自己的等效语法):

    $ ifs=$'\n' ksh -c 'IFS="$ifs"; a=("foo bar" "asdf ghjk"); printf "%s\n" ${a[@]:0:2}'
    foo bar
    asdf ghjk
    $ ifs=$'\n' zsh -yc 'IFS="$ifs"; a=("foo bar" "asdf ghjk"); printf "%s\n" ${a[@]:0:2}'
    foo bar
    asdf ghjk
    

    引用的版本也可以在 Bash 中使用,并且当您只需要原样的值时会更好,因为您不需要依赖IFS. IFS即使数组元素有空格,默认值在这里也可以正常工作:

    $ unset IFS                         # uses default of $' \t\n'
    $ printf "%s\n" "${a[@]:0:2}"
    foo bar
    asdf ghjk
    

    看起来好像不带引号${a[@]:0:2}的元素用空格连接起来,有点像 Bash 在不发生分词的上下文中发生的事情(例如str=${a[@]})。IFS然后它像往常一样尝试用 拆分结果。例如在这里,它在第二个数组元素中间的换行符处拆分:

    $ IFS=$'\n'
    $ a=("foo bar" $'new\nline' "asdf ghjk");
    $ printf ":%s\n" ${a[@]:0:3}
    :foo bar new
    :line asdf ghjk
    

    如上所述,在大多数情况下,您确实应该在数组扩展周围使用引号,但仍然会假设${a[@]:n:m}会导致多个单词,就像这样${a[@]}做一样。

    这里的行为似乎存在于 Bash4.4.12(1)-release和5.0.0(1)-alpha. 我发布了一个关于它的错误报告。

    • 3
  2. user232326
    2018-08-02T23:56:29+08:002018-08-02T23:56:29+08:00

    报价
    报价

    引用

    引用

    引用!!

    这有效:

    $ game_array=("foo bar" "baz" "bam foo" "bar")
    
    $ for game in "${game_array[@]:0:10}" ; do echo "$game"; done
    foo bar
    baz
    bam foo
    bar
    
    • 2

相关问题

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • 如何将带有〜的路径保存到变量中?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve