How do I write some (bash) shell script to convert all matching filenames in directory to command-line options?

梦想的初衷 提交于 2019-12-04 20:11:18
nosetests --with-coverage $(for f in *.py; do echo --cover-package="${f%.*}"; done)

The trick is here is using parameter substitution to remove the file extension.

${f%.*}

And if you care to do it correct (which means, don't allow wordsplitting to cut your filenames apart or unexpected globbing to expand to random filenames), use an array:

files=(*.py)
packages=("${files[@]/%.py/}")
nosetests --with-coverage "${packages[@]/#/--coverage-package=}"
chaos
nosetests --with-coverage `ls *.py|sed -e 's/^/--cover-package=' -e 's/\.py$//'`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!