Bash 根据行中的位置在shell中解析
我对脚本shell有一个小问题 让我们假设:Bash 根据行中的位置在shell中解析,bash,shell,parsing,Bash,Shell,Parsing,我对脚本shell有一个小问题 让我们假设: pos=80:1 我希望在以下语法中使用此变量: while read line word=${line:pos} done < file.txt 读取行时 word=${line:pos} 完成
pos=80:1
我希望在以下语法中使用此变量:
while read line
word=${line:pos}
done < file.txt
读取行时
word=${line:pos}
完成
但那不行
准确地说,
word=$(行:80:1}
工作正常。将其拆分并独立传递项目
pos=80:1
IFS=: read -r pos_start pos_len <<<"$pos"
word=${line:$pos_start:$pos_len}
pos=80:1
IFS=:read-r pos\u start pos\u len这是bash
?zsh
?ksh
?它是在bash中编程的。请注意,如果bash
以不同的方式解析事物,那么您就接近了:${line:$pos如果在尝试解析:
运算符之前扩展bash
可以使用操作。
但是,唉,它没有:)我会使用IFS=:read pos_start pos_len@chepner,很好的呼叫;这在简洁性和可读性上都是一个胜利。已适当编辑。感谢您的帮助:)