Not getting expected result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not getting expected result
# 1  
Old 04-11-2017
Not getting expected result

Hi Experts,

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 ]].

Code:
 
 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

Code:
Execution 1: I have passed 5 parameters.
ksh p_values.ksh
param 5
missing_count condition executed
param condition executed

Code:
 
 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.

Thanks in advance.
# 2  
Old 04-11-2017
couple of things:
Code:
if [ $parm -eq 5 ]; then
and
cnt=`wc -l < list.txt`

# 3  
Old 04-11-2017
Hi,

Thanks a lot for your prompt response.
However it's giving expected output.

I have executed with 4 and 5 parameters. Both the cases I got the same output.

Code:
 
 ksh p_values.ksh
param 4
missing_count condition executed
final missing
 
ksh p_values.ksh
param 5
missing_count condition executed
final missing

this condition is not satisfying in both the cases.

Code:
if [[ $parm -eq 5 ]]; then
 echo "param condition executed"
        cnt=`wc -l < list.txt`

Thanks.
# 4  
Old 04-11-2017
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...
# 5  
Old 04-11-2017
Hi.

Given the indented code on file z6:
Code:
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:
Code:
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:
Code:
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

On a system like:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie)

And note advice from vgersh99 about set -x

Best wishes ... cheers, drl
# 6  
Old 04-12-2017
Hi All,

Thanks a lot for your suggestions.

Regards,
Nalu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep output to file result not as expected

Hi Gurus, I run command grep ABC file1 > file2 against below file. I got all ABC_xxx in one line in file2. I expect to get multiple lines in file2. If I print result in screen, the result is expected. thanks in advance My os is SunOS 5.10 Generic_150400-64 sun4v sparc sun4v ABC_123 XXXXX... (2 Replies)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Assigning variable to output gives error with expected result

Hello, I am trying to print out the first string matching query with grep and I need your help. My scenario: Database John F 4433 Street No 88 CA Elisabeth Taylor 7733 Street No 26 ON Jack Nicholson 0133 Green Park No 34 AR John F 2 9399 Southpark No 02D UT test.sh... (6 Replies)
Discussion started by: baris35
6 Replies

3. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

4. Shell Programming and Scripting

AWK Looping. How can I get expected result?

Can Anyone help with looping... awk 'FNR==1{i++} {for(k=1; k<=NF; k++) A=$k} # 3 Dimension Array END{ for(i=1;i<=217;i++) # For loop 2nd File 1st and 2nd column x=0;y=0 ... (18 Replies)
Discussion started by: Akshay Hegde
18 Replies

5. Shell Programming and Scripting

Result of Catching Return Value from Sub_script.sh to Main_script.sh is not as Expected

Main_script.sh #! /bin/sh ./Sub_script.sh rc=$? echo "Return code from Sub_script.sh : $rc" if ; then echo "$rc = 991" echo "" exit 1 elif ; then echo "$rc = 992" echo "" exit 1 elif ; then echo "$rc = 0" echo "" exit 1 fi (2 Replies)
Discussion started by: duddukuri
2 Replies

6. Shell Programming and Scripting

elif if not expected

Hi All, I write an script will have some functions... I am getting elif not expected error.. Even tried by using set -x for debug but no use.. Could you please help me out in this Variables .... .... .... TEST_SRC() { cat ${filesrc} >> ${filetgt} if ]; then echo... (12 Replies)
Discussion started by: kmsekhar
12 Replies

7. Programming

Test program not giving expected result

I have five classes. 2 composition classes,1 aggregation class and 1 dependency class.I have coded all the classes but one of my test program is not giving me the expected result.I have the following classes: TimeStamp Interval (composition of 2 TimeStamps) TimeSheet ( aggregation of many... (3 Replies)
Discussion started by: moraks007
3 Replies

8. UNIX for Dummies Questions & Answers

display the result of wc -l with words before and after the result

hello showrev -p | wc -l returns: 381 What to do in case I want to have this output: number of lines returned by showrev -p is: 381 thx (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

9. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies

10. UNIX for Advanced & Expert Users

executing script by cron doesnt give me expected result

Hi frnds... I m facing very irritating problem already waisted my 2 days.. I have a following script..( i am pasting only the main code) ftp -ivn 213.194.40.77 <<FTP user $user $password binary cd $FileDir/out lcd $localpath get $file rename $FileDir/out/$file $FileDir/tmp/$file... (1 Reply)
Discussion started by: clx
1 Replies
Login or Register to Ask a Question