Batch file to check the minimum required Java version - sh

I'm new to batch script coding and a little stuck here. I've got a .sh file that checks if version of java installed is at least 1.8.0:
# Minimal version
MINIMAL_VERSION=1.8.0
# Check if Java is present and the minimal version requirement
_java=`type java | awk '{ print $ NF }'`
CURRENT_VERSION=`"$_java" -version 2>&1 | awk -F'"' '/version/ {print $2}'` #says 1.8.0_65
minimal_version=`echo $MINIMAL_VERSION | awk -F'.' '{ print $2 }'` #says 8
current_version=`echo $CURRENT_VERSION | awk -F'.' '{ print $2 }'` #says 8
if [ $current_version ]; then
if [ $current_version -lt $minimal_version ]; then
echo "Error: Java version is too low. At least Java >= ${MINIMAL_VERSION} needed.";
exit 1;
fi
else
echo "Not able to find Java executable or version. Please check your Java installation.";
exit 1;
fi
It is pretty clear. I need to write a .bat file for Windows exactly same logic. So here's where I'm stuck, because I do not know windows analogs for awk.

Here you can see how you can get java version like integer which can numerically compared:
#echo off
:: uncomment the line bellow if the java.exe is not in the %PATH%
::PATH %PATH%;%JAVA_HOME%\bin\
java -version 1>nul 2>nul || (
echo no java installed
exit /b 2
)
for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do set "jver=%%j%%k%%l%%m"
if %jver% LSS 18000 (
echo java version is too low
echo at least 1.8 is needed
exit /b 1
)

Related

SSMS agent job using scp command to transfer files to sftp is going to loop and not completing

Have tried different approaches to make the SSMs job working with SCP command with perl script. but the job is going into loop with out having a result.
PS : The script is working fine with running from command prompt directly.
command used in the perl script:-
$Command = "scp -i D:\File1\RS2\DataFeed\Code\PrivateKey.ppk -s $InternalFile admin#sftp.world.com:$VendorName/$DestFileName";
system command used in perl
system($command);
While running the command directly from windows cmd it is correctly placing file to the SFTP. but while running this perl script from ssms agent job it seems not working and the job is keep running without any results.
Any possible leads to the actual errors will be much appreciated
Detailed Steps :
Job in SSMS :
Step :
DataFeed.cmd
%_Debug% echo off
cd /d %0\..
pushd .
setlocal
rem -----------------------------------------------------------------
rem Localize environment
rem -----------------------------------------------------------------
if exist DataFeed_Environment.cmd (
call DataFeed_Environment.cmd
) else (
echo DataFeed_Environment.cmd not found!!!
echo
goto CmdUsage
)
rem -----------------------------------------------------------------
rem Run perl package
rem -----------------------------------------------------------------
C:\Perl\bin\perl.exe DataFeedProd1.pl
if %ERRORLEVEL% NEQ 0 goto ErrorExit
goto Exit
rem -----------------------------------------------------
rem Command Usage
rem -----------------------------------------------------
:CmdUsage
Echo ---------------------------------------------------------------------
echo.
echo DataFeed.cmd
echo Wraps the call to DataFeed.pl,
echo mails log upon errors.
echo.
echo Usage:
echo DataFeed.cmd
echo.
echo ----------------------------------
rem endlocal
rem popd
rem exit 1
rem -----------------------------------------------------------------
rem Error exit
rem -----------------------------------------------------------------
:ErrorExit
echo DataFeedProd1.pl failed !!!
echo
rem endlocal
rem popd
rem exit 1
rem -----------------------------------------------------------------
rem Exit
rem -----------------------------------------------------------------
rem endlocal
rem popd
:Exit
rem exit 0
sub CopyDataFeedFileToSftp{
my ($DataFeedFileInternal, $DataFeedVendorName,$DataFeedFileName) = #_;
my($DestFileName)=$DataFeedFileName.".zip";
my($Command);
my($RetValue) = 1;
$Command = "C:\\Users\\hprasu\\Downloads\\OpenSSH-Win64\\scp.exe -i D:\\File1\\RS2\\DataFeed\\Code\\PrivateKey.ppk -s $DataFeedFileInternal a_Tne\#nasftp\.egencia.com:$DataFeedVendorName/$DestFileName";
$RetVal = &CallSystem($Command);
if ($RetVal == 0) {
&AppendFileToLog($TempFile);
&ErrorExit("Unable to copy data feed file using SCP command:\n".$Command);
}
}
The above perl method is executing the System command
You should:
have full path to perl.exe and your perl script in job's command
escape all special characters in interpolated strings for Perl and
use full path for scp command since
operating system don't know where scp.exe is located (until it in the $PATH):
check filesystem permissions for all files in the command and perl script. Job should has access those files.
So command would be
$Command = "full_path\\scp.exe -i D:\\File1\\RS2\\DataFeed\\Code\\PrivateKey.ppk -s $InternalFile admin\#sftp.world.com:$VendorName/$DestFileName";
Read this:
https://www.geeksforgeeks.org/perl-quoted-interpolated-and-escaped-strings/

Get fish shell to work with gcloud command line tools?

Has anyone had any luck getting fish shell to work with google's gcloud command line tools? I'm not an expert in Fish script but these are the two files gcloud needs to run (which work fine use Fish's bash mode). Fish doesn't allow you to source bash files from what I understand so these would need to be converted to Fish script?
path.bash
script_link="$( readlink "$BASH_SOURCE" )" || script_link="$BASH_SOURCE"
apparent_sdk_dir="${script_link%/*}"
if [ "$apparent_sdk_dir" == "$script_link" ]; then
apparent_sdk_dir=.
fi
sdk_dir="$( command cd -P "$apparent_sdk_dir" && pwd -P )"
bin_path="$sdk_dir/bin"
export PATH=$bin_path:$PATH
path.completion
_python_argcomplete() {
local IFS=''
COMPREPLY=( $(IFS="$IFS" COMP_LINE="$COMP_LINE" COMP_POINT="$COMP_POINT" _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" _ARGCOMPLETE=1 "$1" 8>&1 9>&2 1>/dev/null 2>/dev/null) )
if [[ $? != 0 ]]; then
unset COMPREPLY
fi
}
complete -o default -F _python_argcomplete "gcloud"
_completer() {
command=$1
name=$2
eval '[[ "$'"${name}"'_COMMANDS" ]] || '"${name}"'_COMMANDS="$('"${command}"')"'
set -- $COMP_LINE
shift
while [[ $1 == -* ]]; do
shift
done
[[ $2 ]] && return
grep -q "${name}\s*$" <<< $COMP_LINE &&
eval 'COMPREPLY=($'"${name}"'_COMMANDS)' &&
return
[[ "$COMP_LINE" == *" " ]] && return
[[ $1 ]] &&
eval 'COMPREPLY=($(echo "$'"${name}"'_COMMANDS" | grep ^'"$1"'))'
}
unset bq_COMMANDS
_bq_completer() {
_completer "CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK=1 bq help | grep '^[^ ][^ ]* ' | sed 's/ .*//'" bq
}
unset gsutil_COMMANDS
_gsutil_completer() {
_completer "CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK=1 gsutil help | sed /Additional/q | grep '^ ' | sed -e 's/^ //' -e 's/ .*//'" gsutil
}
unset gcutil_COMMANDS
_gcutil_completer() {
_completer "CLOUDSDK_COMPONENT_MANAGER_DISABLE_UPDATE_CHECK=1 gcutil help | grep -v '^information' | grep '^[a-z]' | sed -e 's/ .*//' -e '/^$/d'" gcutil
}
complete -o default -F _bq_completer bq
complete -o default -F _gsutil_completer gsutil
complete -o default -F _gcutil_completer gcutil
What worked for me was just using bass. Check it out:
https://github.com/edc/bass
Just take the lines that gcloud adds to your bash_profile, and prepend bass to them in your .config/fish/config.fish file, as follows:
# The next line updates PATH for the Google Cloud SDK.
bass source '/Users/hunter/bin/google-cloud-sdk/path.bash.inc'
# The next line enables shell command completion for gcloud.
bass source '/Users/hunter/bin/google-cloud-sdk/completion.bash.inc'
As of today, I was able just to do
brew install --cask google-cloud-sdk
Added source /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.fish.inc to my ~/.config/fish/config.fish
Clone https://github.com/aliz-ai/google-cloud-sdk-fish-completion then run install.sh.
For path.bash, all it does is add the Cloud SDK bin directory to your PATH. We put some weird stuff in there because we wanted it to work from inside the Cloud SDK directory even when behind, eg, a symlink. For your own system, just do the fsh equivalent of "export PATH=$PATH:/path/to/google-cloud-sdk/bin".
For the tab completion, I don't know how fsh's tab completion works, so I've got nothing.
Fish support is now included out of the box with gcloud, however I ran into a pretty annoying issue. The code included in google-cloud-sdk/path.fish.inc (and #nafg's answer) leaves the directory changed, resulting in each new shell session starting in the google-cloud-sdk directory.
The modification I made was fairly simple, adding two extra lines to get the current working directory and restore it afterwards. This seems to have resolved the issue for me, so hopefully will help anyone else googling for "fish gcloud" problems.
set restore_dir (pwd -P)
set sdk_dir (builtin cd "$apparent_sdk_dir" > /dev/null; and pwd -P)
set bin_path "$sdk_dir/bin"
cd "$restore_dir"
I was able to set up completion by executing this:
# fisher v3
fisher add aliz-ai/google-cloud-sdk-fish-completion
# fisher v4
fisher install aliz-ai/google-cloud-sdk-fish-completion
Fisher can be found here: https://github.com/jorgebucaran/fisher
using fisher:
fisher install lgathy/google-cloud-sdk-fish-completion
and you are good to go
There's an interesting approach here: http://michelpm.com/blog/2013/07/26/switching-from-zsh-to-fish/
Basically it will run a bash script in bash, but it will diff how it changes the environment and apply that in fish.
However it won't work for completions and for your path.bash it's overkill. More like:
Change var=value to set var value
Change [ ... ] to test ...
Change $( ... ) to ( ... )
if doesn't need then and ends with end
Change || to ; or and && to ; and
Change export to set -x
So without testing here's what I would try:
set script_link ( readlink "$BASH_SOURCE" ); or set script_link $BASH_SOURCE
set apparent_sdk_dir ${script_link%/*}
if test "$apparent_sdk_dir" == "$script_link" ;
set apparent_sdk_dir .
end
set sdk_dir ( command cd -P "$apparent_sdk_dir"; and pwd -P )
set bin_path $sdk_dir/bin
set -x PATH $bin_path:$PATH

Need to copy the lines just before a particular string using batch file

I have a file like this:
Executing resource: D:\waste2\SPC\depks_rtl_teller_custom.spc
Executing resource: D:\waste2\SPC\ifpks_msg_incoming_cluster.spc
Failed to execute:
Executing resource: D:\waste2\SQL\casapks_batch.sql
Failed to execute:
Executing resource: D:\waste2\SQL\depks_decbatop_kernel.sql
Executing resource: D:\waste2\SQL\depks_services.sql
Failed to execute:
I need a batch file or perl script or ANT script to pick all the lines just in front of the string "Failed to execute:" and copy to a new file. Simply the failed file list I need in a new file. Please assist.
Surprise! The native Windows FINDSTR command can handle this problem quite nicely :-) There is no need for perl, or any other non-native utility.
#echo off
setlocal
::Define LF variable containing a linefeed (0x0A)
set LF=^
::Above 2 blank lines are critical - do not remove
::Define CR variable containing a carriage return (0x0D)
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
setlocal enableDelayedExpansion
::regex "!CR!*!LF!" will match both Unix and Windows style End-Of-Line
findstr /rc:"!CR!*!LF!Failed to execute:" "test.txt" >"failed.txt"
type failed.txt
See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.
With perl, you could do something like:
while(<>) {
print $prev if /^Failed to execute:/;
$prev = $_;
}
To execute directly from your shell, you can use the following command
perl -ne 'print $prev if /^Failed to execute:/; $prev=$_' path/to/your/file
Using tac and sed:
tac file | sed -n '/Failed to execute/{n;p;}' | tac
You could also use two grep invocations, although this is more of a hack (assuming you only have lines starting with either "failed" or "executing"):
grep -B1 '^Failed to execute' your/file | grep '^Executing'
Or
grep -B1 '^Failed to execute' your/file | grep -v '^--' | grep -v '^Failed to execute'
with PowerShell:
PS II> Select-String "Failed to execute:" c:\file.txt -co 1 | % { $_.context.Precontext }
or simply:
for /f "delims=" %%a in (c:\test.txt) do (
echo(%%a| find /i "Failed to execute:" >Nul && (
setlocal enableDelayedExpansion
echo !msg!
endlocal
)
set "msg=%%a"
)

SymbolicateCrash is not creating proper de-symbol file

I have the dSYM file for build created on client's machine. Client got a crash in build and now I am trying to de-symbol by use of the symbolicatecrash by the simple following command in terminal:
symbolicatecrash myapp_iPod-Touch.crash myapp.app.dSYM > test.txt
but it's not creating any meaningful de-symboled file. and it's giving the follwoing error in terminal:
Can't understand the output from otool
then I found a solution in following link:
iPhone SDK 3.0 and symbolicatecrash not getting along?
but still it's not de-symbolicating the exact memory location to exact code line responsible for crash.
Then I tried some other options too:
Following is the other option but didn't work:
symbolicatecrash.sh -A -v [crashlog-filename] MyApp.dSYM
For reference: http://apptech.next-munich.com/2010/01/symbolicatecrash.html
The best option that helped me is atos command to get the exact code line number of the crash but I want the systematic symbolicatecrash to create the dump.
NOTE: When I create build in my machine and desymbolicate (the my machine created) build's crash log in my machine it creates perfectly good dump file (show's exact memory location VS code line responsible for crash).
If you have the DSYM file for the crash then you can use this one:
#!/bin/bash
if [[ $# < 2 ]]
then
echo "Usage: $0 [-arch <arch> (defaults to whatever is specified in the crashlog- file] <dSYM-file> <crashlog-file>"
exit 1
fi
#Get the architecture either from the params or from the crashlog itself
ARCH_PARAMS=''
if [[ "${1}" == '-arch' ]]
then
ARCH_PARAMS="-arch ${2}"
shift 2
else
ARCHITECTURE=$(cat "${2}" | grep -A1 "Binary Images:" | grep 0x | sed -E -n 's/.*(armv[6-9]).*/\1/p')
if [ -n "${ARCHITECTURE}" ]
then
ARCH_PARAMS="-arch ${ARCHITECTURE}"
else
echo "Couldn't determine architecture based on the crashlog. Please specify it by calling $0 -arch <arch> <dSYM-file> <crashlog-file>"
exit
fi
fi
echo "Assuming architecture:" ${ARCHITECTURE}
#Store the other params
SYMBOL_FILE="${1}"
CRASHLOG="${2}"
#Get the identifier out of the crashlog
IDENTIFIER=$(cat "${CRASHLOG}" | egrep -o "^Identifier:[[:space:]]*.*$" | sed 's/^Identifier:[[:space:]]*\(.*\)$/\1/')
echo "Identifier:" $IDENTIFIER
echo
echo
#Iterate through the crashlog files, find the ones that belong to the $IDENTIFIER, sed the address out of those files, symbolicate them with atos and finally replace them back into those line again. Print all other lines untouched.
while read line
do
SYMBOL=$(echo $line | sed -E -n "s/.*(${IDENTIFIER}[[:space:]]*)(0x[[:alnum:]]*).*/\2/p" | atos -o "${SYMBOL_FILE}/Contents/Resources/DWARF/${IDENTIFIER}" ${ARCH_PARAMS})
if [ -n "$SYMBOL" ]
then
echo $line | sed -E "s/(${IDENTIFIER}[[:space:]]*)(0x[[:alnum:]]*)(.*)/\1\2 ${SYMBOL}/"
else
echo $line
fi
done < "${CRASHLOG}"

tail and grep log and mail (linux)

i want to tail log file with grep and sent it via mail
like:
tail -f /var/log/foo.log | grep error | mail -s subject name#example.com
how can i do this?
You want to send an email when emailing errors occur? That might fail ;)
You can however try something like this:
tail -f $log |
grep --line-buffered error |
while read line
do
echo "$line" | mail -s subject "$email"
done
Which for every line in the grep output sends an email.
Run above shell script with
nohup ./monitor.sh &
so it will keep running in the background.
I'll have a go at this. Perhaps I'll learn something if my icky bash code gets scrutinised. There is a chance there are already a gazillion solutions to do this, but I am not going to find out, as I am sure you have trawled the depths and widths of the cyberocean. It sounds like what you want can be separated into two bits: 1) at regular intervals obtain the 'latest tail' of the file, 2) if the latest tail actually exists, send it by e-mail. For the regular intervals in 1), use cron. For obtaining the latest tail in 2), you'll have to keep track of the file size. The bash script below does that - it's a solution to 2) that can be invoked by cron. It uses the cached file size to compute the chunk of the file it needs to mail. Note that for a file myfile another file .offset.myfile is created. Also, the script does not allow path components in the file name. Rewrite, or fix it in the invocation [e.g. (cd /foo/bar && segtail.sh zut), assuming it is called segtail.sh ].
#!/usr/local/bin/bash
file=$1
size=0
offset=0
if [[ $file =~ / ]]; then
echo "$0 does not accept path components in the file name" 2>&1
exit 1
fi
if [[ -e .offset.$file ]]; then
offset=$(<".offset.$file")
fi
if [[ -e $file ]]; then
size=$(stat -c "%s" "$file") # this assumes GNU stat, possibly present as gstat. CHECK!
# (gstat can also be Ganglias Status tool - careful).
fi
if (( $size < $offset )); then # file might have been reduced in size
echo "reset offset to zero" 2>&1
offset=0
fi
echo $size > ".offset.$file"
if [[ -e $file && $size -gt $offset ]]; then
tail -c +$(($offset+1)) "$file" | head -c $(($size - $offset)) | mail -s "tail $file" foo#bar
fi
How about:
mail -s "catalina.out errors" blah#myaddress.com < grep ERROR catalina.out