0

I'm trying to write a fairly simple bash script to retrieve third party software from the cloud and call this with the CMake execute_process(). I did not expect to spend this long on this, but I'm having difficulty finding examples and I've never been good with bash syntax

The structure of the script is simple, three functions:

  • verifyObject()
  • getFile()
  • clean()

the verify file is structured like this

verifyObject() {
  if [ -e "${TP_LIB_PATH}/${TP_LIB_SO}" ]; then
    return 1
  else
    return 0
}

If tested this with debug messages and it seems to work, but I cannot get the syntax correct to do what I want to do with the other two functions. I want to use the result of the verify function in other if conditions such as:

c/c++ psudocode

if(!verifyObject()) {
  // do something
else
  // do something else
fi
}

I've hunted all over and haven't found examples of this. I've found a couple of syntax's that run but end up falling through the logic to the else regardless. Here is my latest attempt:

clean() {
  if [ verifyObject -eq 1 ]; then
    // This never is ends up happening even when the return
    // value is 1
  else
    // This always happens
  fi
}

What is the correct syntax to accomplish this?

note:

I am only doing it this way because the CMake doc's recommend bash scripts for anything more than simple scripting.

mreff555
  • 1,049
  • 1
  • 11
  • 21
  • 2
    Do not use parentheses around your `if` conditions and in your function calls, bash is not C. And do not use curly braces to group the `if` and `else` branches. Use `then`, `else`, `fi`. Try `if ! verifyObject; then blah; blah; else; blah; blah; fi` (and forget about your last attempt; it tests if string `verifyObject` is integer `1`). Note if you do not know anything about bash you should probably read a quick starter before trying to code anything. – Renaud Pacalet Jul 28 '22 at 10:47
  • 1
    verifyObject; if [ $? -eq 1 ]; then echo 1; else echo Happens; fi – Šerg Jul 28 '22 at 14:04

0 Answers0