使用jupyter和ipython的bash与R(irkernel)的区别
我有一个jupyter/ipython笔记本,其中包含以下bash脚本,使用%%magic搜索目录并将命令应用于与“bam”文件扩展名匹配的文件使用jupyter和ipython的bash与R(irkernel)的区别,bash,ipython,jupyter-notebook,jupyter-irkernel,Bash,Ipython,Jupyter Notebook,Jupyter Irkernel,我有一个jupyter/ipython笔记本,其中包含以下bash脚本,使用%%magic搜索目录并将命令应用于与“bam”文件扩展名匹配的文件 %%bash cd ../data/raw/Alignment_Files/ echo "starting" for file in *; do echo "testing" $file if [[ $file = *bam ]]; then echo "Processing" $file # do so
%%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循环从不执行。我的猜测是一个角色需要转义,但我无法理解。多亏了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)
system
在大多数情况下可能使用sh
,而不是bash
。因此,在这两个示例中,您使用的是不同的shell——尽管您使用的是bash syntaxIs,但有一个原因,您不只是在python中使用os.walk
来实现这一点,而不是使用shell来实现它?