-1

There is something I do not understand regarding Bash variable assignment. I want to create a predicate (so, a variable whose value is either true or false) foo that I could use later in my script and that would be equal to the result of a certain Boolean operation. An ideal code would be like the following, which does not work:

var=1
foo=[[ $var = 2 ]] # wishing foo to be equal to the result of the test "$var = 2"
bash: 1 : command not found
foo=test $var = 2
bash: 1 : command not found

In other words, I wonder if there is a shorter way other than:

f(){
  if [[ $var = 2 ]]; then echo true
  else echo false
  fi
}
foo=$(f)

that could give the same final value to foo. What are your thoughts about this? Thank you for your answers.

Loutr_
  • 3
  • 3
  • Do you just need to know if it is running. – BobMonk Mar 31 '20 at 08:43
  • @BobMonk I use it multiple times after this so I would like to store it... The problem does not come from `systemctl`, afaik. – Loutr_ Mar 31 '20 at 08:46
  • You need to read/learn about bash syntax, It does not work like that. Try https://shellcheck.net to verify your script. – Jetchisel Mar 31 '20 at 09:19
  • 1
    Does this answer your question? [In bash, how to store a return value in a variable?](https://stackoverflow.com/questions/15013481/in-bash-how-to-store-a-return-value-in-a-variable) – oguz ismail Mar 31 '20 at 09:19
  • This question is changing everytime... – Jetchisel Mar 31 '20 at 09:21
  • Sorry for changing the question, I am trying to make as minimal as I can... – Loutr_ Mar 31 '20 at 09:24
  • The `[[` is a command which is like the `test` command but with more feature, so to put your assignment in a way you can understand is `foo=test $var = 1` – Jetchisel Mar 31 '20 at 09:28
  • The only difference is that the `[[` needs a closing `]]` , but before someone tells me that what `[[` really is check the output of `type [[` – Jetchisel Mar 31 '20 at 09:31
  • a=[] will create an empty array – BobMonk Mar 31 '20 at 10:30

2 Answers2

0

To store permanetly a variable you must use db or file, instead if you want to store it only for this session check it:"What are sessions? How do they work?". if you want to store it on a file check:it:Saving php output in a file"

0

Echo "is active" >> server log.txt

Or

Echo the result of systemctl

> This will always make a new file

>> Will append to a file or create if not there

You could do with maybe recording the time to the file also.

Additionally

== I believe needed to check if numeric is true -eq I believe if a string

echo "Run script"

    read -r approveAction

     approved=$(echo "$approveAction" | awk '{print tolower($1)}' | sed "s/[^[:alpha:]^[:digit:]]//g")

     if [ $approved == "y" ] ;

     then

      when=date
         echo "user agreement "$when>> userlog.log
       ./script.sh


     fi 

Here's an example from a script asking for users permission to run a script

FROM YOUR CHANGE OF QUESTION

var=1

if [[ $var == 1 ]]; then 
    echo "true" 
else 
    echo "false"
fi

This works

BobMonk
  • 178
  • 1
  • 10
  • 1
    I do not want to store it into a logfile or anything... And I believe the `==` operator is not the recommended syntax for an equality test (although also supported). `-eq` does not work either. – Loutr_ Mar 31 '20 at 09:03
  • If using single = then single [ are needed, -eq is for integer – BobMonk Mar 31 '20 at 09:12
  • I believe that equation doesn't actually result in anything. a equal array of 1 , = and 1 – BobMonk Mar 31 '20 at 09:46