在批处理文件中的同一行中运行2个unix命令 [英] running 2 unix commands in the same line in a batch file

查看:230
本文介绍了在批处理文件中的同一行中运行2个unix命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何帮助,如果我的术语不正确,请原谅我。

Apreciate any help and excuse me if my terminology is incorrect.

我想要做的是写一个scrpit / .bat文件,

将1个目录(和子目录)从pointA复制到点B.

然后在pointB(和子目录)中解压缩将给出* .csv文件的文件
< br>然后在pointB(和子目录)中我要删除所有这些csv文件中的一些行

What I am trying to do is write a scrpit/.bat file that will do the following:
copy 1 directory(and subdirectories) from pointA, to point B.
Then in pointB(and subdirectories) unzip the files which will give *.csv files
Then in pointB(and subdirectories) I want to delete some rows from all these csv files

在cygwin上运行的unix命令将复制所有文件从/ cygdrive / v / pointA / *到当前目录。 (即点是当前工作目录)

This unix command, run on cygwin, will copy all the files from /cygdrive/v/pointA/* to the current directory . (i.e. the dot is the current working directory)

cp /cygdrive/v/pointA/* .

在cygwin上运行的unix命令将遍历目录和子目录中的所有文件.zip
并解压缩它们

This unix command, run on cygwin, will go through all the files in the directory and subdirectories that end with .zip and unzip them

find -iname *.zip -execdir unzip {} \;

在cygwin上运行的unix命令将遍历目录和子目录中的所有文件与.csv

对于每个文件,它删除前6行和最后一行,这是返回的文件。

This unix command, run on cygwin, will go through all the files in the directory and subdirectories that end with .csv
For each file it deletes the 1st 6 rows and the last row and that's the returned file.

find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';'



我希望在一个脚本/ bat文件中执行此操作,与第一个find命令
我在一行上的find和unzip命令有麻烦,我想知道如何和如果这可以做

I was looking to do this in one script/bat file but I am having trouble with the first find command I am having trouble with the find and unzip commands on the one line and am wondering how and if this can be done

chdir C:\pointA
C:\cygwin\bin\cp.exe /cygdrive/v/pointB/* .
::find -iname *.zip -execdir unzip {} \;
::find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';' 

我试过这样:

C:\cygwin\bin\find.exe -iname *.zip -execdir C:\cygwin\bin\unzip.exe {} \;  

但我得到以下结果:

/usr/bin/find: missing argument to `-execdir'



<

Can anyone advise if/how this can be done?

推荐答案

Cygwin工具使用他们的自己的路径,例如 /cygdrive/c/cygwin/bin/unzip.exe 虽然有时反斜杠的Windows路径工作,反斜杠倾向于混淆Cygwin工具。

The Cygwin tools use their own kind of paths, e.g. /cygdrive/c/cygwin/bin/unzip.exe though sometimes the Windows paths with backslashes work, the backslashes do tend to confuse the Cygwin tools.

我强烈建议您在Bash shell脚本中编写工具,而不是cmd.exe Windows批处理文件。在我的经验中(1)在bash脚本中比在批处理文件中更容易做流控制,(2)Cygwin环境更好地使用Bash。您可以打开bash shell并运行 bash yourscript.sh

I highly recommend you write your tool in Bash shell script instead of a cmd.exe Windows batch file. In my experience (1) it's much easier to do flow control in bash scripts than in batch files, and (2) the Cygwin environment works better from Bash. You can open a bash shell and run bash yourscript.sh.

您的Bash脚本可能如下所示: (未测试)

Your Bash script might look something like this: (untested)

#!/bin/bash
# This script would be run from a Cygwin Bash shell.
# You can use the Mintty program or run C:\cygwin\bin\bash --login
# to start a bash shell from Windows Command Prompt.

# Configure bash so the script will exit if a command fails.
set -e 

cd /cygdrive/c/pointA
cp /cygdrive/v/pointB/* .

# I did try something like this:

# 1. Make sure you quote wildcards so the shell doesn't expand them
#    before passing them to the 'find' program.
#
# 2. If you start bash with the --login option, the PATH will be
#    configured so that C:\cygwin\bin is in your PATH, and you can
#    just call 'find', 'cp' etc. without specifying full path to it.

# This will unzip all .zip files in all subdirectories under this one.
find -iname '*.zip' -execdir unzip {} \;

这篇关于在批处理文件中的同一行中运行2个unix命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆