简体   繁体   English

Bash-目录相关的脚本

[英]Bash - Directory dependent script

I am trying to run a python script in a directory and using bash apply this script to each of its subdirectories. 我试图在目录中运行python脚本,然后使用bash将此脚本应用于其每个子目录。

I found a script on unix stack exchange that does it for 1 set of subdirectories here . 我发现,做它1组子目录UNIX堆叠交换的脚本在这里 But I want it to recursively work for all sub-directories . 但我希望它对所有子目录都递归起作用

The problem is I have a single wav.py in the parent directory but none in the sub-directories. 问题是我在父目录中只有一个wav.py ,但在子目录中却没有。

for d in ./*/ ; do (cd "$d" && python3 $1 SA1.wav); done

As you can see $1 (wav.py) is the path to my python file set when I call the bash script. 如您所见,$ 1(wav.py)是我调用bash脚本时python文件集的路径。 I would also like the path to be relative to how many levels of the subdirectory tree I have traversed. 我还希望该路径与我遍历了多少级子目录树有关。 I know I can use an absolute path. 我知道我可以使用绝对路径。 But it will cause issues later on, so I'd like to avoid it. 但这会在以后引起问题,因此我想避免这种情况。

Eg. 例如。 for 1 level 1级

for d in ./*/ ; do (cd "$d" && python3 "../$1" SA1.wav); done

for 2 levels 2个级别

for d in ./*/ ; do (cd "$d" && python3 "../../$1" SA1.wav); done

Sorry if this seems trivial. 抱歉,这似乎无关紧要。 I'm still new to bash. 我还是bash的新手。

Additional Info: 附加信息:

This is my full directory path: 这是我的完整目录路径:

root@Chiku-Y700:/mnt/e/Code/Python - WorkSpace/timit/TIMIT/TEST/DR1# bash recursive.sh wav.py suit rag

the full command I'm trying to run is: 我要运行的完整命令是:

python3 $1 SA1.wav $2 SA2.wav $3

$2 and $3 are unrelated to any directory info. $ 2和$ 3与任何目录信息都不相关。

I get: 我得到:

python3: can't open file '/mnt/e/Code/Python': [Errno 2] No such file or directory python3:无法打开文件'/ mnt / e / Code / Python':[Errno 2]没有这样的文件或目录

This error came 12 times for 11 subdirectories. 11个子目录出现12次此错误。

Let's look at your command, with wav.py being $1: 让我们看一下您的命令,其中wav.py为$ 1:

for d in ./*/ ; do (cd "$d" && python3 $1 SA1.wav); done

Can we reduce the complexity by making wav.py executable and giving it a shebang, so that you can call it directly? 我们是否可以通过使wav.py可执行文件并赋予它shebang来降低复杂性,以便您可以直接调用它? Then you may move it to your PATH or temporarily putting the location, where it sits, into the path. 然后,您可以将其移至PATH或将其所在的位置临时放入路径中。 It's generally best habit, that your script does not depend upon the place, from where it is invoked, especially that it isn't needed to be in the same directory from where it is called. 通常最好的习惯是,脚本不依赖于调用位置,特别是不需要与调用位置位于同一目录中。

PATH=$PWD:$PATH
for d in ./*/ ; do (cd "$d" && wav.py SA1.wav); done

The input data should not depend on the same directory restriction, so that you can call it from every dir and with the data being in an arbitrary dir, too: 输入数据不应依赖于相同的目录限制,因此您可以从每个目录中调用它,并且数据也可以位于任意目录中:

for d in ./*/ ; do wav.py $d/SA1.wav; done

Probably, you produce an output file, which is written to the current directory, then you either should extract the output dir from the input dir, if this is what you always want to achieve, or let the user specify an output dir. 可能是,您生成了一个输出文件,该文件被写入当前目录,然后,如果这是您一直想要的结果,则应该从输入目录中提取输出目录,或者让用户指定一个输出目录。 A default outputdir might still be the inputdir or the current dir. 默认的outputdir可能仍然是inputdir或当前dir。 Or you write to STDOUT, and pipe the output to a file, located to your choice. 或者,您写入STDOUT,然后将输出通过管道传输到您选择的文件中。

But your full command is: 但是您的完整命令是:

python3 $1 SA1.wav $2 SA2.wav $3

That's fine for simple commands, but maybe you can name these parameters in a meaningful way: 对于简单的命令来说很好,但是也许您可以以有意义的方式命名这些参数:

pyprog="$1"
samplerate="$2"
log="$3"

python3 $pyprog SA1.wav $samplerate SA2.wav $log

or, as done before 或者,像以前一样

$pyprog SA1.wav "$samplerate" SA2.wav "$log"

Then, John1024s solution might work: 然后,John1024s解决方案可能会起作用:

find . -type d -execdir $pyprog SA1.wav "$samplerate" SA2.wav "$log" ";"

If changing pyprog is not an option, there is a second approach to solve the problem: 如果不能更改pyprog,则有第二种方法可以解决该问题:

Write a wrapper script, which takes the directory to work in as a parameter and test it with different depths of directories. 编写一个包装器脚本,该脚本将要使用的目录作为参数,并使用不同目录深度对其进行测试。

Then call that wrapper by find: 然后通过查找来调用该包装器:

find . -type d -exec ./wrapper.sh {} ";"

The wrapper.sh should start with: wrapper.sh应该以以下内容开头:

#/bin/bash
#
#
directory="$1" 

and use it where needed. 并在需要的地方使用它。

Btw.: I would rename the Python - WorkSpace to Python-WorkSpace (even better: python-workSpace ) too, because blanks in file and path names always cause trouble. 顺便说一句:我也将Python - WorkSpace重命名为Python-WorkSpace (甚至更好: python-workSpace ),因为文件名和路径名中的空格总是会引起麻烦。

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

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