I have written the below script but its not working as per expectation.
My requirement is if this condition [[ $missing_count -gt 0 ]] is satisfied
then only check for this condition [[ $parm==5 ]] if this also satisfied check for the condition [[ $val -eq $cnt ]].
vi p_values.ksh
path="/db/ora/files"
mode=1
b_days=10
mins=120
freq=15
ksh p_test.ksh $path $mode $b_days $mins $freq
vi p_test.ksh
path=$1
mode=$2
b_days=$3
mins=$4
freq=$5
missing_count=9
param=$#
echo "param $param"
if [[ $missing_count -gt 0 ]]; then
echo "missing_count condition executed"
printf "some missing file\n" >> list.txt
if [[ $parm==5 ]]; then
echo "param condition executed"
cnt=`cat list.txt | wc -l`
val=`expr $mins / $freq`
if [[ $val -eq $cnt ]]; then
echo "frequency check executed"
fi
else
echo "final missing"
exit 0
fi
fi
Execution 1: I have passed 5 parameters.
ksh p_values.ksh
param 5
missing_count condition executed
param condition executed
Execution 2: I have passed 4 parameters.
ksh p_values.ksh
param 4
missing_count condition executed
param condition executed
expr: syntax error
Why if [[ $parm==5 ]]; then this condition is still satisfying even I passed 4 parameters .
Could you pleas help me.
without going too much into the code.... shouldn't if [ $parm -eq 5 ]; then
be if [ $param -eq 5 ]; then ?
run the script with tracing enabled: set -x and see what happens...
path=$1
mode=$2
b_days=$3
mins=$4
freq=$5
missing_count=9
param=$#
echo "param $param"
if [[ $missing_count -gt 0 ]]; then
echo "missing_count condition executed"
printf "some missing file\n" >> list.txt
if [[ $parm==5 ]]; then
echo "param condition executed"
cnt=`cat list.txt | wc -l`
val=`expr $mins / $freq`
if [[ $val -eq $cnt ]]; then
echo "frequency check executed"
fi
else
echo "final missing"
exit 0
fi
fi
the command shellcheck z6 produces:
In z6 line 2:
mode=$2
^-- SC2034: mode appears unused. Verify it or export it.
In z6 line 3:
b_days=$3
^-- SC2034: b_days appears unused. Verify it or export it.
In z6 line 12:
if [[ $parm==5 ]]; then
^-- SC2077: You need spaces around the comparison operator.
In z6 line 14:
cnt=`cat list.txt | wc -l`
^-- SC2006: Use $(..) instead of deprecated `..`
^-- SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.
In z6 line 15:
val=`expr $mins / $freq`
^-- SC2006: Use $(..) instead of deprecated `..`
^-- SC2003: expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].
^-- SC2086: Double quote to prevent globbing and word splitting.
^-- SC2086: Double quote to prevent globbing and word splitting.
Some details for utility shellcheck:
shellcheck analyse shell scripts (man)
Path : /usr/bin/shellcheck
Version : ShellCheck - shell script analysis tool
Type : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help : probably available with -h
Repo : Debian 8.7 (jessie)
Home : http://hackage.haskell.org/package/ShellCheck