0

I have one script name check.ksh which will check some functionality and having different functions for that:

!/bin/ksh
print "start"
val1=$1
val2=$2
func=$3

checkfunc() {
 if [[ $val1 != $val2 ]]
 then
         print "argument differs"
         exit 0
 else
         print "argument same"
         exit 1
 fi
 }
anotherfunc() {

print "blah blah"
}
 $func 2&1> testlog.log
 print "end"

and another wrapper script name wrapper.ksh which will use as a wrapper script of check.ksh:

#!/bin/ksh

wfunc() {
check.sh $1 $2 $3
[[ $? -eq 0 ]] && printf "different argument passed" || printf "same argument passed"
}

wfunc $1 $2 $3

when I run ./wrapper.sh test1 test1 checkfunc its should print the output as "same argument passed" as the called script check.sh exited with 1

but always I am getting $? as 0

I have tried to print some value or using return but same issue, can we return only the output from the function to the wrapper.ksh

note : I cannot remove the print and other things in check.sh I can only edit the content of checkfunc() in the check.sh

Sann
  • 41
  • 5
  • In your wrapper.sh, you're calling wfunc without any argument. The argument within the function are the ones with which you called the function, not the script. Basically you're calling check.sh without any arguments, it prints "end" then exit with 0. – Andre Gelinas Feb 13 '19 at 12:54
  • I have done that but still not working, its not the problem, anyway the script is running as I checked after setting `set -vx` – Sann Feb 13 '19 at 16:52
  • 1
    Actually I think you're trying to use && and || wrong. You're trying to translate something that should an if/then/else, but it won't work. The way it's written now is that if $? is 0 then execute the whole statement after &&. If it's not, don't bother and continue, which is exactly what your script is doing. – Andre Gelinas Feb 13 '19 at 17:27
  • I have tried with if else also, but no luck – Sann Feb 14 '19 at 05:20
  • In check.sh use "$func 2>testlog.log 1>&2", works in my test after that. – Andre Gelinas Feb 14 '19 at 14:04
  • yes its working, but m wondering is there any consequence if I change that because `check.ksh` is big script, and I dont want to change anything other than my function `checkfunc()`. and why its not working with `$func 2&1> testlog.log` – Sann Feb 15 '19 at 12:08
  • Well with your syntax, $func 2& will start the function $func with parameter 2 and in background, then fd 1 is redirected to testlog.log. And that's why check.sh is printing "end" with this syntax. – Andre Gelinas Feb 15 '19 at 13:02

0 Answers0