0

我有一个带有以下 bash 脚本的 jupyter/ipython 笔记本,它使用 %% 魔法来搜索目录并将命令应用于匹配“bam”文件扩展名的文件

%%bash
cd ../data/raw/Alignment_Files/
echo "starting"
for file in *; do
    echo "testing" $file
    if [[ $file = *bam ]]; then
        echo "Processing" $file
        # do something interesting...
    fi
done

我正在尝试使用 R 内核(irkernel)将它移植到另一个笔记本并想出了这个

system('cd ../data/raw/Alignment_Files/
echo "starting"
for file in *; do
    echo "testing" $file
    if [[ $file = *bam ]]; then
        echo "Processing" $file
        # do something interesting...
    fi
done',
intern=TRUE)

但是 if/then 循环不起作用,我尝试了很多变体但没有成功。R 版本提供“开始”和“测试”调试消息,但 if 循环从不执行。我的猜测是需要转义一个角色,但我无法弄清楚。

4

1 回答 1

0

感谢 Eric Renouf 的评论,我通过使用 shell 工具(grep)而不是任何特定于 bash 的东西找到了解决方案。由于 irkernel 使用系统 shell,而不是 bash。

system('cd ../data/raw/Alignment_Files/
echo "starting"
for file in *; do
    echo "testing" $file
    if echo "$file" | grep "bam$"; then
        echo "Processing" $file
        # do something interesting...
    fi
done',
intern=TRUE)
于 2018-06-10T23:56:33.267 回答