如何将输出重定向到 BASH 中的不同目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4342037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect output to different directory in BASH?
提问by Tony
I have 3 directories named: /home/user/control4 , /home/user/control8 ,/home/user/control16
我有 3 个目录: /home/user/control4 , /home/user/control8 ,/home/user/control16
I have written a script file which has two loops , the first one is running a simulation, producing 3 files named cc1.txt cc2.txt, and cc3.txt and a second loop is for the names of directory
我写了一个脚本文件,它有两个循环,第一个是运行模拟,生成 3 个文件名为 cc1.txt cc2.txt 和 cc3.txt,第二个循环用于目录名称
I like to direct cc1.txt, cc2.txt, cc3.txt to /home/user/control4,/home/user/control8 ,/home/user/control16, respectively. What is the exact syntax for doing this in BASH?
我喜欢将cc1.txt、cc2.txt、cc3.txt分别指向/home/user/control4、/home/user/control8、/home/user/control16。在 BASH 中执行此操作的确切语法是什么?
Thanks for your help.
谢谢你的帮助。
My script file look likes this
我的脚本文件看起来像这样
#!/user/bin/bash
for j in $(4 8 16 ) # loop for directories
do
for i in $(seq 1 3) # loop for simulations
do
.... produce cc1.txt cc2.txt cc3.txt
done
How to output the three files to the respective directories? something like /home/user/control$j/cc$i.txt?
如何将三个文件输出到各自的目录?像 /home/user/control$j/cc$i.txt 之类的东西?
done
完毕
采纳答案by Paused until further notice.
for j in /home/user/control{4,8,16}
do
for i in cc{1,2,3}.txt
do
produce "$j/$i"
done
done
That will produce nine files. If you want one file per directory for a total of three files, some math might do the trick.
这将产生九个文件。如果您希望每个目录一个文件,总共三个文件,一些数学可能会起作用。
for i in {1..3}
do
produce "/home/user/control$((2*2**i))/cc$i.txt"
done
回答by Andreas Wong
cat cc$i.txt > /home/user/control$j/cc$i.txt
回答by David Yaw
If I'm reading your script right, you'll end up with 9 files: cc1 through 3 each in directories control4, 8, and 16. However, your text description implies that you want only 3 files total: control4/cc1.txt, control8/cc2.txt, and control16/cc3.txt.
如果我没看错你的脚本,你最终会得到 9 个文件:cc1 到 3 每个在目录 control4、8 和 16 中。但是,你的文字描述暗示你总共只需要 3 个文件:control4/cc1.txt 、 control8/cc2.txt 和 control16/cc3.txt。
If you literally have a command named producethat takes output files as parameters, and you only want 3 files total, I'd just specify the three output directories & files manually, and not worry about looping.
如果您确实有一个produce将输出文件作为参数命名的命令,而您总共只需要 3 个文件,那么我只需手动指定三个输出目录和文件,而不必担心循环。
If you literally have a command named producethat takes output files as parameters, and you want 9 files total, then yes, control$j/cc$i.txt will do that.
如果您确实有一个produce将输出文件作为参数命名的命令,并且您想要总共 9 个文件,那么是的, control$j/cc$i.txt 会这样做。
If you don't have a command named producethat takes output files as parameters (i.e., that line in your code sample was psuedo-code), and the command that produces data just dumps those files out without you specifying anything, then I think you'll have to mv cc1.txt control4 ; mv cc2.txt control8 ; mv cc3.txt control16after it is done.
如果您没有名为produce将输出文件作为参数的命令(即,您的代码示例中的那一行是伪代码),并且生成数据的命令只是将这些文件转储出来,而无需您指定任何内容,那么我认为您“将不得不mv cc1.txt control4 ; mv cc2.txt control8 ; mv cc3.txt control16它完成之后。

