问题
To begin, please excuse my lack of knowledge on windows batch scripting (even the basic).
I would like to ask for help on the equivalent of a working Unix script. This is the working script that I have.
#!/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
The "view tags" file contain:
pompei.s1272.hwdig_b12.default dincsori.arisumf.s2637b_dig.default tags2
The output of "cleartool lsview -l -prop -full $i | grep "Last accessed":
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".
The output of "cleartool lsview -l -prop -full $i | grep "Last accessed": | 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
Basically, it will execute the command, cleartool lsview -l -prop -full $i | grep "Last accessed" on each line of the file "view_tags".
If it finds a string "Last accessed" on the output, it will proceed to print the output but if not, it will say, "not found".
I really hope someone can help me on this. Thank you very much in advance.
回答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!
)
- The first
For /f
will iterate the input file - The 2nd one will parse the output of cleartool, getting the 3rd space delimited string of each line.
回答2:
The following batch script processing your posted "view_tags" input:
@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
Should give the following output (though I'm not able to test):
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
I can eliminate one of the FOR /F loops if I use JREPL.BAT - a regular expression text processing utility written as hybrid batch/JScript:
@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
来源:https://stackoverflow.com/questions/43675433/windows-equivalent-of-unix-shell-script