Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何在终端中通过管道将数据从python输出输出?_Shell_Tcsh - Fatal编程技术网

Shell 如何在终端中通过管道将数据从python输出输出?

Shell 如何在终端中通过管道将数据从python输出输出?,shell,tcsh,Shell,Tcsh,我使用os.listdir获取文件夹列表 python -c "import os; print os.listdir(os.getcwd())" 我希望通过管道将输出传递到shell中的for循环,并在tcsh shell中迭代,以运行不同的命令,在迭代中可以使用每个文件夹 publishContent -dir "each dir name" 其中,每个目录名都是上面python的输出 我以前也这样试过 for dirName in `python

我使用
os.listdir
获取文件夹列表

python -c "import os; print os.listdir(os.getcwd())"
我希望通过管道将输出传递到shell中的for循环,并在tcsh shell中迭代,以运行不同的命令,在迭代中可以使用每个文件夹

publishContent -dir "each dir name"
其中,每个目录名都是上面python的输出

我以前也这样试过

for dirName in `python -c "import os; print os.listdir(os.getcwd())"` do publishConent -dir dirName  End
但它似乎不起作用…

(t)csh没有
for
循环,但它确实有
foreach

   foreach name (wordlist)
   ...
   end     Successively sets the variable name to each member of wordlist
           and executes the sequence of commands between this command and
           the matching end.  (Both foreach and end must appear alone on
           separate lines.)  The builtin command continue may be used to
           continue the loop prematurely and the builtin command break to
           terminate it prematurely.  When this command is read from the
           terminal, the loop is read once prompting with `foreach? ' (or
           prompt2) before any statements in the loop are executed.  If
           you make a mistake typing in a loop at the terminal you can rub
           it out.
所以你想要的是:

% foreach dirName ( `python -c "import os; print(os.listdir(os.getcwd()))"` )
foreach? echo "$dirName"
foreach? end
这将提供:

['file',
'file
space',
'file2']
据我所知,没有任何方法可以把它放在一行上。上面引用的文档提到“foreach和end必须单独出现在单独的行中”

但我不确定您为什么在这里使用Python:

% foreach dirName ( ./* )
foreach? echo "$dirName"
foreach? end
./file
./file space
./file2
它没有Python语法,可以正确处理带有空格的文件名

两者都将列出所有文件和目录的方式;使用
if(-d“$dirName”)
测试它是否是一个目录

最好也使用
find

% find . -maxdepth 1 -a -type d -exec publishContent -dir {} \;

谢谢martin,查找解决方案看起来很正确,但是它返回的是完整路径,而不是当前工作目录中的文件夹名!或者,如果我们将cd刻录到cwd,然后运行它。/file2,而我希望它只是file2。我们怎么能像python中的一样在这里使用sed呢我试着用一个例子来摆脱
/
,但是没有用:如果
setenv var./file2
那么
echo$var:s\./|
它输出
file2
,但是类似的东西在find:
find中不起作用-maxdepth 1-a-type d-exec publishContent-dir{}:s\./|
您可以使用
basename
仅获取路径的最后一部分(
basename/home/martin/test
→ <代码>测试)@ChangZhao;我想这就是你想要的?您可以将它放在
find
命令的反勾中:
find[..]-exec publishContent-dir`basename{}`。它可能还有一个
find
标志效果很好