我正在尝试编写一个bash脚本,当在文件列表中找到特定脚本时,该脚本会打开文件。最终目标是获取(search-list.txt)中的每个订单项,搜索所有文件并在TextWrangler中打开它(如果它与术语匹配)。
文件列表(search-list.txt)
3.1.3.2
3.1.4.2
4.1.3.1
4.1.4.1
4.1.5.1
4.2.1.1
4.2.2.1
5.1.6.1
脚本
#!/bin/bash
cat ./search-list.txt | while read output
do
for f in $(find ./content/ -type f -name '*.md'); do
if [ ! -z "$(grep " $output " $f)" ]; then
open -a /Applications/TextWrangler.app $f
fi
done
done
答案 0 :(得分:1)
这对我有用:
#!/bin/bash
cat ./search-list.txt | while read output
do
for f in $(find ./content/ -type f -name '*.md'); do
if [ ! -z "$(grep -e "title:[[:space:]]$output[[:space:]]" $f)" ]; then
open -a /Applications/TextWrangler.app $f
fi
done
done
答案 1 :(得分:0)
这对我有用:
script.sh
:
#!/bin/bash
cat ./search-list.txt | while read output
do
for f in $(find ./content/ -type f -name '*.md'); do
if echo $f | grep -q $output; then
echo "opening matched file" $f
open -a /Applications/TextWrangler.app $f
fi
done
done
script.sh
的输出:
$ cat search-list.txt
3.1.3.2
3.1.4.2
4.1.3.1
4.1.4.1
4.1.5.1
4.2.1.1
4.2.2.1
5.1.6.1
$ touch test1_3.1.3.2_file.md
$ touch test2_3.1.4.2_file.md
$ touch test3_4.1.3.1.md
$./script.sh
opening matched file ./test1_3.1.3.2_file.md
opening matched file ./test2_3.1.4.2_file.md
opening matched file ./test3_4.1.3.1.md