printf 从 bash 中的目录数组打印了太多信息
printf prints too much information from array of directories in bash
下面的代码搜索包含文件 update_csgo.txt 的目录,将其写入数组并打印数组:
#!/bin/bash
mapfile -t updates < <(find /home/tcagame/ -type f -name "update_csgo.txt")
printf "'%s' \n"${updates[@]}""
这是我得到的输出:
'/home/tcagame/8frag/6/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/3/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/5/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/4/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/7/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/8/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/2/update_csgo.txt'
/home/tcagame/update_csgo.txt
这个/home/tcagame/update_csgo.txt从哪里来的?删除它的最佳方法是什么?
您有多余的引号:
printf "'%s' \n"${updates[@]}""
^^
删除它们使其工作(可选,您始终可以引用变量本身以正确处理带空格的文件名):
printf "'%s' \n" "${updates[@]}"
下面的代码搜索包含文件 update_csgo.txt 的目录,将其写入数组并打印数组:
#!/bin/bash
mapfile -t updates < <(find /home/tcagame/ -type f -name "update_csgo.txt")
printf "'%s' \n"${updates[@]}""
这是我得到的输出:
'/home/tcagame/8frag/6/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/3/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/5/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/4/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/7/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/8/update_csgo.txt'
/home/tcagame/update_csgo.txt'/home/tcagame/user/2/update_csgo.txt'
/home/tcagame/update_csgo.txt
这个/home/tcagame/update_csgo.txt从哪里来的?删除它的最佳方法是什么?
您有多余的引号:
printf "'%s' \n"${updates[@]}""
^^
删除它们使其工作(可选,您始终可以引用变量本身以正确处理带空格的文件名):
printf "'%s' \n" "${updates[@]}"