Why is an error output on parsing a line within a FOR loop? [duplicate] - string

This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 4 years ago.
I'm currently trying to write a batch file to automatically detect if a VPN is logged in or not. However, the code is not working as expected.
set VAR=SUCCESS
for /f "delims=: tokens=1" %%i in ('"C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info') do (
set str=%%i
set rep=%str:logged out=%
echo %str%
echo %rep%
if not "%str%"=="%rep%" (
set VAR=FAIL
)
echo %VAR%
)
echo %VAR%
Running "C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info will give exactly the following (will have spaces before the start of each line):
Command arguments:
INFO result:
session: code: status:
xxxxxx 64 logged out
There is 1 active session(s)!
The status can be either logged out or session established. What I'm trying to determine is that I'd have run the VPN. If the user has successfully logged into the VPN, then I need to do something, otherwise, have to execute some other code.
VAR=FAIL means the VPN failed to log in successfully and would be in logged out state. The problem I'm facing is that the str and rep variables are not assigned any value. I can determine it by the echo statements.
Can somebody help me out in this?

There is a much more simple way to work around this. The way you have the for statement setup will cause it to perform echo's and set strings for each new line of the output string.
A very easy way is to use the | FIND /I after your command. This will filter and search for a statement or text your looking for without any loops.
"C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info | FIND /I "logged out">Nul && (Echo.logged out) || (Echo.session established)
To use the setup, simply put your command in the front (Before the pipe) and use the find /I "" to search for that within the output. Please keep in mind that >Nul will silence the command from being shown on console, which is great for this use.

Related

Retrieve underlying file of tee command

References
Fullcode of what will be discussed here:
https://github.com/djon2003/com.cyberinternauts.linux.backup
activateLogs question that solved how to log to file and screen: https://stackoverflow.com/a/70792273/214898
Limitation
Just a small reminder from the last question: this script is executed on limited environment, on a QNAP (NAS).
Background
I have a function that activate logging which now has three modes: SCREEN, DISK, BOTH. With some help (from the question of the link above), I achieve to make work the BOTH option. DISK & BOTH use a file descriptor numbered 3. The first is pointing to a file and the second to stdout.
On exit of my script (using trap), it detects if there were logged errors and send them via email.
Code
function sendErrorMailOnExit()
{
## If errors happened, then send email
local isFileDescriptor3Exist=$(command 2>/dev/null >&3 && echo "Y")
if [ "$isFileDescriptor3Exist" = "Y" ]; then
local logFile=$(readlink /proc/self/fd/3 | sed s/.log$/.err/)
local logFileSize=$(stat -c %s "$logFile")
if [ $logFileSize -gt 0 ]; then
addLog "N" "Sending error email"
local logFileName=$(basename "$logFile")
local logFileContent=$(cat "$logFile")
sendMail "Y" "QNAP - Backup error" "Error happened on backup. See log file $logFileName\n\nLog error file content:\n$logFileContent"
fi
fi
}
trap sendErrorMailOnExit EXIT
Problem
As you can see, this works well because the file descriptor #3 is using a file. But now, using the BOTH option, the file descriptor #3 is pointing to stdout and the file is written via tee. Hence my question, how could I get the location of the file of tee.
Why not only using a variable coming from my function activateLogs would you say? Because, this function relaunches the script to be able to get all the logs not caught before the function is called. Thus why using this method to retrieve the error file location.
Possible solutions, but not the best (I hope)
One way would be to pass the file location through a script
parameter, but I would prefer not do that if that can be avoid.
Another, would be to create a "fake" file descriptor #4 (probably my best solution up to now) that would always point to the file.
Does anyone have an idea?
I finally opted for the creation of a "fake" file descriptor #4 that does not nothing except pointing to the current log file.

Powershell script to parse a log file and then append to a file

I am new to Shellscripting.I am working on a poc in which a script should read a log file and then append to a existing file for the purpose of alert.It should work as per below
There will be some predefined format according to which it will decide whether to append in file or not.For example:
WWXXX9999XS message
**XXX** - is a 3 letter acronym (application code) like for **tom** for tomcat application
9999 - is a 4 numeric digit in the range 1001-1999
**E or X** - For notification X ,If open/active alerts already existing for same error code and same message,new alerts will not be raised for existing one.Once you have closed existing alerts,it will raise alarm for new error.There is any change in message for same error code from existing one, it will raise a alarm even though open/active alerts present.
X option is only for drop duplicates on code and message otherwise all alert mechanisms are same.
**S** - is the severity level, I.e 2,3
**message** - is any text that will be displayed
The script will examine the log file, and look for error like cloud server is down,then it would append 'wwclo1002X2 cloud server is down'if its a new alert.
2.If the same alert is coming again,then it should append 'wwclo1002E2 cloud server is down
There are some very handy commands you can use to do this type of File manipulation. I've updated this in response to your comment to allow functionality that will check if the error has already been appended to the new file.
My suggestion would be that there is enough functionality here to warrant saving it in a bash script.
My approach would be to use a combination of less, grep and > to read and parse the file and then append to the new file. First save the following into a bash script (e.g. a file named script.sh)
#!/bin/bash
result=$(less $1 | grep $2)
exists=$(less $3 | grep $2)
if [[ "$exists" == "$result" ]]; then
echo "error, already present in file"
exit 1
else
echo $result >> $3
exit 0
fi
Then use this file in the command passing in the log file as the first argument, the string to search for as the second argument and the target results file as the third argument like this:
./script.sh <logFileName> "errorToSearchFor" <resultsTargetFileName>
Don't forget to run the file you will need to change the permissions - you can do this using:
chmod u+x script.sh
Just to clarify as you have mentioned you are new to scripting - the less command will output the entire file, the | command (an unnamed pipe) will pass this output to the grep command which will then search the file for the expression in quotes and return all lines from the file containing that expression. The output of the grep command is then appended to the new file with >>.
You may need to tailor the expression in quotes after grep to get exactly the output you want from the log file.
The filenames are just placeholders, be sure to update these with the correct file names. Hope this helps!
Note updated > to >> (single angle bracket overwrites, double angle bracket appends

Start Excel file from Windows batch script in safemode, use default file association

Question Summary:
Can I start Excel file Installer.xlsm from Windows batch script in safemode, without providing EXCEL.EXE installation path?
Details
I have a windows batch script which downloads the latest versions of a family of Excel Add-ins from a remote server, places them in a directory (C:\appname\AddIns) and calls the Excel file Installer.xlsm.
Upon loading, Installer.xlsm executes a VBA macro, which uninstalls older versions of the add-ins and installs their newer version.
Currently I start Installer.xlsm using the command:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm"
What's great about it is that it uses Windows' file association to open Excel, and I don't have to provide the EXCEL.EXE installation path (multiple users with different machine images and MS Office versions).
Now I'd like to load Installer.xlsm in safemode, to make sure that no add-ins are loaded and no other code is run while Installer.xlsm tries to work with the add-ins.
I know I can use "<PathToExcel>excel" /safemode "<PathToXls>Installer.xlsm" as described in this answer, but this method doesn't use Windows' file association and requires that I provide a path.
We have users with various machine images, using different versions of MS Office, so I do not want to get into hardcoding all possible Excel installation locations.
Can I do something of the following form:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm /safemode"
I tried different possible combinations without success. How would you do it?
First I suggest to read the Microsoft documentation page Application Registration. It explains how the installer of an application or an application suite like Microsoft Office should register the installed application(s) so that the executable(s) of the application(s) can be found by other applications.
Recommended is creating under registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
a subkey with name of the executable file like excel.exe with default string value being name of the executable with full path and optionally adding one more string value with name Path containing just the path to the executable. The Path string can but most not exist and it can but must not end with a backslash.
The command START uses also this key to find an application as explained in answer on Where is “START” searching for executables?
The installers of the various versions of Microsoft Office register excel.exe key under this key too.
So the easiest method on Windows Vista and later Windows versions to get installation location of Microsoft Excel is:
#echo off
for /F "skip=1 tokens=2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
But on Windows XP the output of reg.exe is different and requires for that reason this batch code:
#echo off
for /F "skip=3 tokens=3*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause
The different outputs are explained in answer on Read words separated by space & string value also contains space in a batch script in batch code written to get string value of a default string of a registry key containing spaces.
And it is good coding practice to add extra code which handles an error case like registry key does not exist at all because Microsoft Excel is not installed at all.
But is it possible with batch code to do what command START respectively the Windows shell function ShellExecuteEx does on using in a command prompt window the command line?
start "Launching installer file" "C:\Path to file\Installer.xlsm"
Yes, it is possible as the commented batch code below demonstrates.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem First query default string value of HKEY_CLASSES_ROOT\.xlsm from registry.
call :GetDefaultRegValue "HKCR\.xlsm"
rem Is there no key HKEY_CLASSES_ROOT\.xlsm or was the default string empty?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem Get the shell command line used for opening a *.xlsm file.
call :GetDefaultRegValue "HKCR\%RegValue%\shell\open\command"
rem Could the command line not read successfully from Windows registry?
if not defined RegValue goto GetFromAppPaths
SET RegValue
rem The command line contains as first string usually enclosed in double
rem quotes EXCEL.EXE with full path enclosed in double quotes. And there
rem can be even more arguments on the command line which are not needed
rem here. The command line below is used to get just first string of
rem the command line which should be EXCEL.EXE with full path.
for %%I in (%RegValue%) do set "RegValue=%%~I" & goto CheckExcelExistence
rem It is not good when both registry queries above fail. This means
rem either Microsoft Excel is not installed at all or a version of
rem Excel is installed which does not support *.xlsm files like Excel
rem of MS Office 2003, MS Office 2000 or MS Office 97.
rem However, perhaps just *.xlsm is not correct registered and therefore
rem get full path to excel.exe from application registration key.
:GetFromAppPaths
call :GetDefaultRegValue "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe"
if defined RegValue goto CheckExcelExistence
echo Failed to determine installation location of Microsoft Excel.
echo/
endlocal
pause
goto :EOF
:CheckExcelExistence
SET RegValue
rem Remove surrounding double quotes if the Excel executable file name
rem read from Windows registry is still enclosed in double quotes.
set "RegValue=%RegValue:"=%"
if exist "%RegValue%" goto :RunInstall
echo Registered "%RegValue%" does not exist.
echo/
endlocal
pause
goto :EOF
:RunInstall
SET RegValue
ECHO start "Launching installer file" /wait "%RegValue%" "%~dp0Installer.xlsm" /safemode
endlocal
goto :EOF
rem This subroutine queries from Windows registry the default string value of
rem the key passed to the subroutine as first and only parameter and assigns
rem this value to environment variable RegValue. Environment variable RegValue
rem is deleted and therefore is not defined after subroutine exits on failure
rem to get the registry value or when the default value is an empty string.
rem This subroutine works for Windows XP and all later versions of Windows.
:GetDefaultRegValue
set "TypeToken=2"
:Reg3Run
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if not "%%~B" == "" (
set "RegValue=%%B"
goto :EOF
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto Reg3Run
)
)
set "RegValue="
goto :EOF
This batch code is just a demonstration. It does not start Excel when really found. Instead it just outputs the command line which would start Excel because of ECHO left of start ... in block below label RunInstall.
Further this batch code contains 4 lines with just SET RegValue. Those 4 lines output just the string value queried successfully from Windows registry and stored in environment variable RegValue. Those 4 commands help to understand what happens on execution of the batch file. Those four command lines should be deleted finally from batch file and also the single ECHO written in upper case.
Note: It is quite easy to test what happens if an expected registry key does not exist or its default value is an empty string. Just insert a single character like # before last double quote on a line starting with call :GetDefaultRegValue and the modified registry key is not found anymore.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
setlocal /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg.exe command line with using a separate command process started in background.

How to run batch script without using *.bat extension

Is there any method in Windows through which we can execute a batch script without *.bat extension?
This is an interesting topic to me! I want to do some observations about it.
The important point first: A Batch file is a file with .BAT or .CMD extension. Period. Batch files can achieve, besides the execution of usual DOS commands, certain specific Batch-file facilities, in particular:
Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
Execution of GOTO command.
Execution of CALL :NAME command (internal subroutine).
Execution of SETLOCAL/ENDLOCAL commands.
Now the funny part: Any file can be redirected as input for CMD.exe so the DOS commands contained in it are executed in a similar way of a Batch file, with some differences. The most important one is that previous Batch-file facilities will NOT work. Another differences are illustrated in the NOT-Batch file below (I called it BATCH.TXT):
#echo off
rem Echo off just suppress echoing of the prompt and each loop of FOR command
rem but it does NOT suppress the listing of these commands!
rem Pause command does NOT pause, because it takes the character that follows it
pause
X
rem This behavior allows to put data for a SET /P command after it
set /P var=Enter data:
This is the data for previous command!
echo Data read: "%var%"
rem Complex FOR/IF commands may be assembled and they execute in the usual way:
for /L %i in (1,1,5) do (
set /P line=
if "!line:~0,6!" equ "SHOW: " echo Line read: !line:~6!
)
NOSHOW: First line read
SHOW: Second line
NOSHOW: This is third line
SHOW: The line number 4
NOSHOW: Final line, number five
rem You may suppress the tracing of the execution redirecting CMD output to NUL
rem In this case, redirect output to STDERR to display messages in the screen
echo This is a message redirected to STDERR >&2
rem GOTO command doesn't work:
goto label
goto :EOF
rem but both EXIT and EXIT /B commands works:
exit /B
:label
echo Never reach this point...
To execute previous file, type: CMD /V:ON < BATCH.TXT
The /V switch is needed to enable delayed expansion.
More specialized differences are related to the fact that commands in the NOT-Batch file are executed in the command-line context, NOT the Batch-file context. Perhaps Dave or jeb could elaborate on this point.
EDIT: Additional observations (batch2.txt):
#echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.
rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2
echo Standard input/output operations> CON
echo/> CON
< CON set /P var=Enter value: > CON
echo/> CON
echo The value read is: "%var%"> CON
Execute previous file this way: CMD < BATCH2.TXT > NUL
EDIT: More additional observations (batch3.txt)
#echo off
rem Dynamic access to variables that usually requires DelayedExpansion via "call" trick
rem Read the next four lines; "next" means placed after the FOR command
rem (this may be used to simulate a Unix "here doc")
for /L %i in (1,1,4) do (
set /P line[%i]=
)
Line one of immediate data
This is second line
The third one
And the fourth and last one...
(
echo Show the elements of the array read:
echo/
for /L %i in (1,1,4) do call echo Line %i- %line[%i]%
) > CON
Execute this file in the usual way: CMD < BATCH3.TXT > NUL
Interesting! Isn't it?
EDIT: Now, GOTO and CALL commands may be simulated in the NotBatch.txt file!!! See this post.
Antonio
Just use:
type mybat.txt | cmd
Breaking it down...
type mybat.txt reads mybat.txt as a text file and prints the contents. The | says capture anything getting printed by the command on its left and pass it as an input to the command on its right. Then cmd (as you can probably guess) interprets any input it receives as commands and executes them.
In case you were wondering... you can replace cmd with bash to run on Linux.
in my case, to make windows run files without extension (only for *.cmd, *.exe) observed, i have missed pathext variable (in system varailbles) to include .cmd. Once added i have no more to run file.cmd than simply file.
environment variables --> add/edit system variable to include .cmd;.exe (ofcourse your file should be in path)
It could be possible yes, but probably nor in an easy way =) cause first of all.. security.
I try to do the same thing some year ago, and some month ago, but i found no solution about it.. you could try to do
execu.cmd
type toLaunch.txt >> bin.cmd
call bin.cmd
pause > nul
exit
then in toLaunch.txt put
#echo off
echo Hello!
pause > nul
exit
just as example, it will "compile" the code, then it will execute the "output" file, that is just "parse"
instead of parsed you could also just rename use and maybe put an auto rename inside the script using inside toLaunch.txt
ren %0 %0.txt
hope it helped!
It is possible at some degree. You'll need an admin permissions to run assoc and ftype commands. Also a 'caller' script that will use your code:
Lets say the extension you want is called .scr.
Then execute this script as admin:
#echo off
:: requires Admin permissions
:: allows a files with .scr (in this case ) extension to act like .bat/.cmd files.
:: Will create a 'caller.bat' associated with the extension
:: which will create a temp .bat file on each call (you can consider this as cheating)
:: and will call it.
:: Have on mind that the %0 argument will be lost.
rem :: "installing" a caller.
if not exist "c:\scrCaller.bat" (
echo #echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\scrCaller.bat
rem :: associating file extension
assoc .scr=scrfile
ftype scrfile=c:\scrCaller "%%1" %%*
You even will be able to use GOTO and CALL and the other tricks you know. The only limitation is that the the %0 argument will be lost ,tough it can be hardcoded while creating the temp file.
As a lot of languages compile an .exe file for example I think this a legit approach.
If you want variables to be exported to the calling batch file, you could use
for /F "tokens=*" %%g in (file.txt) do (%%g)
This metod has several limitations (don't use :: for comments), but its perfect for configuration files.
Example:
rem Filename: "foo.conf"
rem
set option1=true
set option2=false
set option3=true
#echo off
for /F "tokens=*" %%g in (foo.conf) do (%%g)
echo %option1%
echo %option2%
echo %option3%
pause

BAT script to search directory for folders that match an input name

not sure what's the best (or better) way to write a bat script to take name input and search a directory to see whether it exists.
do I need to output the directory list to a file first before running a comparison?
and if it makes a difference, the directory is on a repository so for my purposes I'll be using 'svn list', but I thought a nice general solution for everyone would be nice.
If you are testing for a specific name within a directory, then you would generally do something like:
if exist name (echo found it) else (echo not found)
Or if the name is incomplete
if exist *name* (echo found it) else (echo not found)
If you are testing for a name anywhere within a directory tree, then I would use
dir /s /a-d *name* >nul && (echo found it) || (echo not found)
If you are issuing a command that generates lines of output and you want to test if a name exists within any one line, then Windows pipes generally work fine, as long as the size of the output is not huge and the 2nd half can keep up with the 1st half.
yourCommand | find "name" >nul && (echo found it) || (echo not found)
But pipes become inefficient if the 2nd half is slow compared to the 1st half, and a lot of data must be buffered. In that case it is definitely better to use a temp file instead of a pipe. I incorporate a random number into the temp file name to guard against possible collision of multiple processes using the same temp directory.
set tempFile="%temp%\myTempFileBaseName%random%.txt"
yourCommand >%tempFile%
<%tempFile% find "name" >nul && (echo found it) || (echo not found)
del %tempFile%
I generally use pipes unless I know I have a performance issue.

Resources