用数组中的sed替换字符串并存储为变量
Replace string with sed in array and store as variable
如果我对 csgo 路径进行硬编码,我的代码就可以工作,但是如果我使用搜索功能并替换我使用 sed 搜索的目录,我的代码就会失败。
#Find directorties of CSGO instances to update
updatepaths=`find /home/tcagame/ -type f -name "update_csgo.txt"`
#Splits diretories on space to be read from the array
updates=($updatepaths)
#Path to CSGO instances to update
#csgo="/home/tcagame/user/33/csgo/steam.inf"
#Creating automated path
csgo= echo "${updates[0]}" | sed 's,update_csgo.txt,csgo/steam.inf,'
#Check for updates
python $updatecheck $csgo > ~/autoupdate/status/updatestatus.txt
当我 echo "$csgo"
它创建一个新行时,我认为这就是它不起作用的原因。
/home/tcagame/user/33/csgo/steam.inf
[New Line]
这就是我试图以自动化方式实现的目标:
python srcupdatecheck /home/tcagame/iceman/206/csgo/steam.inf
使用 mapfile
将 find
输出的行读入数组比依赖分词更安全:唯一的麻烦是文件名包含换行符。
mapfile -t updates < <(find /home/tcagame/ -type f -name "update_csgo.txt")
这里只需要参数展开,不需要sed:
csgo="${updates[0]%update_csgo.txt}csgo/steam.inf"
或者,让 find 为您完成更多繁重的工作:
mapfile -t update_dirs < <(
find /home/tcagame/ -type f -name "update_csgo.txt" -exec dirname '{}' \;
)
csgo="${update_dirs[0]}/csgo/steam.inf"
如果我对 csgo 路径进行硬编码,我的代码就可以工作,但是如果我使用搜索功能并替换我使用 sed 搜索的目录,我的代码就会失败。
#Find directorties of CSGO instances to update
updatepaths=`find /home/tcagame/ -type f -name "update_csgo.txt"`
#Splits diretories on space to be read from the array
updates=($updatepaths)
#Path to CSGO instances to update
#csgo="/home/tcagame/user/33/csgo/steam.inf"
#Creating automated path
csgo= echo "${updates[0]}" | sed 's,update_csgo.txt,csgo/steam.inf,'
#Check for updates
python $updatecheck $csgo > ~/autoupdate/status/updatestatus.txt
当我 echo "$csgo"
它创建一个新行时,我认为这就是它不起作用的原因。
/home/tcagame/user/33/csgo/steam.inf
[New Line]
这就是我试图以自动化方式实现的目标:
python srcupdatecheck /home/tcagame/iceman/206/csgo/steam.inf
使用 mapfile
将 find
输出的行读入数组比依赖分词更安全:唯一的麻烦是文件名包含换行符。
mapfile -t updates < <(find /home/tcagame/ -type f -name "update_csgo.txt")
这里只需要参数展开,不需要sed:
csgo="${updates[0]%update_csgo.txt}csgo/steam.inf"
或者,让 find 为您完成更多繁重的工作:
mapfile -t update_dirs < <(
find /home/tcagame/ -type f -name "update_csgo.txt" -exec dirname '{}' \;
)
csgo="${update_dirs[0]}/csgo/steam.inf"