简体   繁体   English

无法在 for 循环中运行来自 bash 脚本的命令

[英]Can't run commands from bash script in for-loop

I'm trying to create a bash script that reads in subfolders within a folder and uses the subfolder as an input for a python program, but for some reason, it can't recognize some simple commands.我正在尝试创建一个 bash 脚本,该脚本读取文件夹中的子文件夹并将子文件夹用作 python 程序的输入,但由于某种原因,它无法识别一些简单的命令。

So just as a reference, the python program I have takes in two inputs: the subfolder it's taking in (folder must only have files in it) and an output file name.因此,作为参考,我的 python 程序接受两个输入:它所接受的子文件夹(文件夹中必须只有文件)和 output 文件名。

#!/bin/bash

PATH=/foo/bar/*
for folder in $PATH; do
        echo  "${folder##*/}"
        (cat "${folder##*/}" output.txt) | python3 program.py
done

I've ran the program by itself in the command line and it has worked numerous times, but for some reason this doesn't.我已经在命令行中单独运行了该程序,并且它已经运行了很多次,但由于某种原因,它没有。

This is the resulting output:这是生成的 output:

$ bash fileread.sh
> File1
> fileread.sh: line 6: cat: command not found
> fileread.sh: line 6: python3: command not found
> File2
> fileread.sh: line 6: cat: command not found
> fileread.sh: line 6: python3: command not found

Clearly, the echo command works, but somehow the other two stopped working显然, echo命令有效,但不知何故其他两个停止工作

PATH is a reserved variable. PATH是一个保留变量。 By using it for your own purpose, bash can't find the commands anymore.通过将其用于您自己的目的,bash 再也找不到命令了。 Just use a different name.只需使用不同的名称。

Also, if you want to store a list, it makes IMO more sense to use an array:此外,如果你想存储一个列表,IMO 使用数组更有意义:

p=(/foo/bar/*)
for folder in "${p[@]}"
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM