필터 지우기
필터 지우기

Testing Bash variable to decide whether to use MATLAB or Runtime

조회 수: 4 (최근 30일)
FM
FM 2023년 4월 27일
댓글: FM 2023년 4월 27일
I am using a Bash script to test a Java package created from an m-file function using Compiler SDK. I want the script to use either Matlab, if it is installed, or the Matlab Runtime if the former is not installed.
Based on the Stack Overflow answer https://stackoverflow.com/a/17157187, I tried testing the flow control at the Bash command line:
$ UseMLorRT=MATLAB # Next, test if this choice is properly recognized
$ if [ "$UseMLorRT"="MATLAB" ]; then echo UseMatlab; else echo UseRuntime; fi
UseMatlab
$ if [ "$UseMLorRT"="Runtime" ]; then echo UseMatlab; else echo UseRuntime; fi
UseMatlab
The setting `UseMLorRT=MATLAB` is not being recognized. In the second `if-else` statement above, `echo UseRuntime` should be executed, but it is not. I get the same behaviour by comparing using "==" instead of "=" and if I use double square brackets instead of single square brackets.
Can anyone see what I am doing wrong?
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 4월 27일
https://linuxize.com/post/bash-case-statement/ you could use a case statement
FM
FM 2023년 4월 27일
That might be cleaner than assuming that assuming that if $UseMLorRT is set to anything other than MATLAB, then it was set to Runtime (for example, what if it was set to the uncapitalized "matlab" instead?).
I'll possibly look into that in the future. For now, my situation doesn't really allow for that. I've already spent a good portion of the day figuring out the nuances of logical testing in Bash, how to compare variables against string literals, and also trying to learn whether there is a difference between a variable that has not been set versus one that was set to an empty string.
I think it depends on one's role. If I was a developer, I definitely should allot my time accordingly. Since I am not, doing so is very problematic, much as it would help. I appreciate the suggestion nevertheless.

댓글을 달려면 로그인하십시오.

채택된 답변

FM
FM 2023년 4월 27일
편집: FM 2023년 4월 27일
The answer is that I need spaces not just around the square brackets, but also around the equality test:
#!/bin/bash
# The 2nd statement overrides the 1st
UseMLorRT=Runtime
UseMLorRT=MATLAB
echo UseMLorRT = "$UseMLorRT"
#if [[ "$UseMLorRT"=="Runtime" ]]
if [ "$UseMLorRT" = "MATLAB" ]
then
echo UseMATLAB
else
echo UseRuntime
fi
If UseMLorRT=Runtime comes 2nd above, then the script prints UseRuntime. If `UseMLorRT=MATLAB` comes 2nd, then the script prints UseMATLAB.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB Compiler SDK에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by