【问题标题】:Windows Equivalent of Unix Shell ScriptUnix Shell 脚本的 Windows 等效项
【发布时间】:2017-04-29 12:28:53
【问题描述】:

首先,请原谅我对 Windows 批处理脚本(即使是基础知识)缺乏了解。

我想就一个可运行的 Unix 脚本寻求帮助。这是我的工作脚本。

#!/bin/bash
list=`cat view_tags`
for i in $list; do
    cleartool lsview -l -prop -full $i | grep "Last accessed" >& /dev/null
    if [ $? -eq 0 ]; then
         echo -n $i
         echo " " `cleartool lsview -reg ccase_win -l -prop -full $i | grep "Last accessed" | awk '{print $3}'`
    else
         echo $i cannot be found
fi
done
  1. “查看标签”文件包含:

    pompei.s1272.hwdig_b12.default
    dincsori.arisumf.s2637b_dig.default
    tags2
    
  2. "cleartool lsview -l -prop -full $i | grep "Last访问"的输出:

    Last accessed 2017-11-05T11:32:13+01:00 by UNIX:UID-111234.s1272@server1
    Last accessed 2013-11-20T16:16:50+01:00 by UNIX:UID-124312.exrt@177.32.5.1
    cleartool: Error: No matching entries found for view tag "tags2".
    
  3. "cleartool lsview -l -prop -full $i | grep "Last访问"的输出:| awk '{print $3}'

    2017-11-05T11:32:13+01:00
    2013-11-20T16:16:50+01:00
    cleartool: Error: No matching entries found for view tag "tags2".
    tags2 cannot be found
    

基本上,它会执行命令, cleartool lsview -l -prop -full $i |在文件“view_tags”的每一行上 grep “上次访问”。

如果在输出中找到字符串“上次访问”,它将继续打印输出,但如果没有,它会说“未找到”。

我真的希望有人可以帮助我。非常感谢您。

【问题讨论】:

  • windows系统上可以使用cleartool lsview吗?
  • view_tags 包含哪些样本?如果不是所有东西都可用,就很难尝试和复制一些东西。请编辑问题并添加诸如 view_tags 之类的示例数据以及在最后访问的部分使用 echo 时的输出是什么样的
  • 您好,是的,它是可用的。我们正在运行 Clearcase 版本 8.0.1.11:)
  • 您是坚持使用 Windows 批处理等效项(这需要使用附加工具,等效于 bash 脚本中的二进制命令),还是可以接受 PowerShell 解决方案(实际上是考虑到您似乎希望通过 bash 脚本完成的工作,要简单得多)? (在我看来,您想从文件中获取文件列表,并打印每个文件的文件名和上次访问时间,对吗?)

标签: batch-file unix windows-scripting


【解决方案1】:
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
For /f "delims=" %%A in ('Type view_tags') do (
  Set "Out=%%A can nnot be found"
  For /f "tokens=3" %%B in (
    'cleartool lsview -reg ccase_win -l -prop -full %%A ^| Find "Last accessed" 2^>Nul '
  ) Do Set "Out=%%B"
  Echo !Out!
)
  • 第一个For /f 将迭代输入文件
  • 第二个会解析 cleartool 的输出,得到每行的第三个空格分隔的字符串。

【讨论】:

    【解决方案2】:

    以下批处理脚本处理您发布的“view_tags”输入:

    @echo off
    for /f "delims=" %%A in (view_tags) do (
      for /f "tokens=3" %%B in (
        'cleartool lsview -reg ccase_win -l -prop -full "%%A" 2^>nul ^| find "Last accessed"'
      ) do echo %%A: %%B
    ) || echo %%A: Not Found
    

    应该给出以下输出(虽然我无法测试):

    pompei.s1272.hwdig_b12.default: 2017-11-05T11:32:13+01:00
    dincsori.arisumf.s2637b_dig.default: 2013-11-20T16:16:50+01:00
    tags2: Not Found
    

    如果我使用JREPL.BAT - a regular expression text processing utility written as hybrid batch/JScript,我可以消除其中一个 FOR /F 循环:

    @echo off
    for /f "usebackq delims=" %%A in ("view_tags") do ^
      cleartool lsview -l -prop -full "%%A" 2>nul | ^
      jrepl "Last accessed (\S+)" "$txt='%%A: '+$1" /jmatchq || echo %%A: Not Found
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多