https://wiki.playonlinux.com/api.php?action=feedcontributions&user=Walkman&feedformat=atomPlayOnLinux - User contributions [en]2024-03-28T13:07:21ZUser contributionsMediaWiki 1.30.0https://wiki.playonlinux.com/index.php?title=Scripting_-_Chapter_4:_Conditions&diff=877Scripting - Chapter 4: Conditions2016-07-08T21:58:44Z<p>Walkman: Fixed example code</p>
<hr />
<div>== Conditions? Can one eat it? ==<br />
<br />
You'd have to be very hungry, but why not? More seriously, a condition will allow you to order your script to do different things according to '''a test'''. For example, the value of a variable.<br />
<br />
== Some examples will make it easier to understand ==<br />
<br />
'''A test, with no action taken in case of failure:'''<br />
<br />
<pre class="playonlinux code"><br />
POL_SetupWindow_menu "What's for dinner?" "Tonight's menu" "Carrots Potatoes French-fries" " "<br />
if [ "$APP_ANSWER" = "Carrots" ]<br />
then<br />
POL_SetupWindow_message "Let's eat" "Tonight's menu"<br />
fi<br />
</pre><br />
<br />
The message "let's eat" will only appear if the user chooses Carrots.<br />
<br />
'''A test, with some action in case of failure:'''<br />
<br />
<pre class="playonlinux code"><br />
POL_SetupWindow_menu "What's for dinner?" "Tonight's menu" "Carrots Potatoes French-fries" " "<br />
if [ "$APP_ANSWER" = "Carrots" ]<br />
then<br />
POL_SetupWindow_message "I'm on a hunger strike" "Tonight's menu"<br />
else<br />
POL_SetupWindow_message "Can I have a second helping?" "Tonight's menu"<br />
fi<br />
</pre><br />
<br />
The message "I'm on a hunger strike" will only be displayed if the user chooses Carrots. Otherwise the message "Can I have a second helping?" will be displayed.<br />
<br />
'''Threefold test, with no action in case of failure:'''<br />
<br />
<pre class="playonlinux code"><br />
POL_SetupWindow_menu "What do you want to eat tonight?" "Tonight's menu" "Carrots Potatoes French-fries" " "<br />
if [ "$APP_ANSWER" = "French-fries" ]<br />
then<br />
POL_SetupWindow_message "I love french fries" "Tonight's menu"<br />
elif [ "$APP_ANSWER" = "Potatoes" ]<br />
then<br />
POL_SetupWindow_message "I agree to eat potatoes tonight" "Tonight's menu"<br />
elif [ "$APP_ANSWER" = "Carottes" ]<br />
then<br />
POL_SetupWindow_message "I do not like carrots" "Tonight's menu"<br />
fi<br />
</pre><br />
<br />
What this code does should be clear enough after seeing the previous examples.<br />
<br />
{{Info|In Bash, you can use either single-quotes ('), aka '''apostrophe''', or double-quotes ("), aka '''quote'''. We highly recommend, as a standard, to use double-quotes ("), aka '''quote''', so that you can use apostrophe in your string, if needed. Though, it is also preferred not to use contractions (which require an apostrophe, or '''single-quote''' (')) at all to avoid this situation altogether. The choice is yours; choose whichever is the cleanest, easiest-to-read, most-concise code.}}<br />
<br />
'''Previous:''' [[Scripting - Chapter 3: Variables|Chapter 3: Variables]]<br />
<br />
'''Next:''' [[Scripting - Chapter 5: Wine|Chapter 5: Wine]]<br />
<br />
[[Category:Scripting]]</div>Walkman