-3

Im Dono ! And i learn bash on linux. im a beginer and i need help for something pretty basic for someone already know bash linux.

I write a simple script and i got a problem.

I'm trying to set a variable that detects when I say "games" to tell me "Good games!" and if I say anything else he replies "Good day!"

However, when I try to set the ca variable doesn't work. I leave you screenshots of what I have as an answer.

Btw I don't know how to copy/paste on linux. (I want to specify that I am on windows with ssh access to a Linux Linode in Ubuntu 22.10 version)

Ps** The script is in French because I am French speaking. I do my best to translate for you, so be indulgent please☺.

The Script

The Result

#!/bin/sh

name=Dono
user=$(whoami)
date=$(date)
whereamit=$(pwd)



echo "Yio $name, comment tu va?"
sleep 2
echo "Tu es connecter avec l'utilisateur $user."
sleep 1 
echo "Nous sommes le $date."
sleep 2

echo "Que veux tu faire maintenant?"
read reponse
sleep 1

if ( reponse = jeux)
then
        echo "Bon jeux!"
    
else 
        echo "Bonne journée!"
fi
12
  • 2
    Please post your script and error as text, not as screenshots.
    – tink
    Commented Oct 22, 2022 at 17:07
  • 1
    As a general rule: it's always prudent to check one's scripts using ShellCheck ...
    – tink
    Commented Oct 22, 2022 at 17:09
  • thank you for your answer but i dont know how to copy from my linux session to my windows session to post it here , can you help me plz ? :P Commented Oct 22, 2022 at 17:12
  • 1
    thank u for help me, i used your tool to what was the problem and i solved it :P now how can i mark my post as solved ? Commented Oct 22, 2022 at 18:16
  • 1
    @DonoConfig - regarding mark as solved ... that's not really a thing here at Stack Overflow. If I were you I'd remove the question, it won't benefit others too much as it would be hard to find a descriptive search that addresses all of the issues you'd built into your original script. Stack Overflow isn't meant as a support forum for individuals, but rather as a Q&A site for the greater good of programmers.
    – tink
    Commented Oct 22, 2022 at 18:51

1 Answer 1

1

There are three errors in the script.

The if statement requires square brackets, not round.

To reference the value of the variable "reponse", you must reference that by using the form $reponse, or more strictly ${reponse}.

You also need to ensure that you leave space around elements of the expression and the square brackets. Otherwise, the string and the adjacent bracket are sometimes lumped together, causing a syntax error.

Beyond that, I suggest you get into the habit of wrapping string values in double-quotes, so that if your editor does syntax highlighting, it will perform its job more easily.

Also, the round-brackets "(" are best reserved if there is need to evaluate an expression (which could include shell commnds), whereas you should stick to braces "{" where you are only referencing the direct variable involved. A minor thing these days, given the power of computers these days, but it would improve overall performance.

Also, the reference provided as a comment, ShellCheck, is very good for helping you for errors that you cannot recognize yourself, but I suggest you get into the habit of trying to identify those by yourself first, examining your code with a critical eye as to what you are trying to do with each element of an expression, and only then use the helper tool. That way, you are likely going to shorten your learning curve and be able to catch errors before your first run.

Lastly, I suggest you create a test script for any new function/syntax with which you are not familiar. It allows you to focus on the issue and try different scenarios in a controlled fashion.

Not the answer you're looking for? Browse other questions tagged or ask your own question.