Page 73 of 76

Posted: Sun 18 Nov 2018, 19:00
by don570
echo command can have options . I would try echo -n

or maybe echo -e and then use \n for a new line.

http://linuxcommand.org/lc3_man_pages/echoh.html
_____________________________________________

Posted: Sun 18 Nov 2018, 21:43
by Argolance
Thanks.
In the script above, the temporary text file is built at startup using the echo command only as example, but in reality, this text file is a list of files (built with the find command) which can have spaces in their path and in their name (example: /root/.moonchild productions/pale moon/xxx). It is these files that are problematic and are processed in segments instead of in one piece.
What could be done to avoid this?

Posted: Sun 18 Nov 2018, 22:01
by SFR
Argolance wrote:What could be done to avoid this?
This, for example:

Code: Select all

while read -r LINE; do
LIST="${LIST}
   <checkbox>
      <variable>$LINE</variable>
      <default>true</default>
      <label>$LINE</label>
      <action>if false disable:$LINE</action>
      <action>sed -i 's/$LINE//' /tmp/checkbox_test</action>
      <action>refresh:TEXT</action>
   </checkbox>
"
done < /tmp/checkbox_test
Greetings!

Posted: Sun 18 Nov 2018, 22:04
by Argolance
Oh yes, thank you SFR! :D

Posted: Mon 19 Nov 2018, 00:23
by some1
Argolance,SFR:Nice :)
----
Argolance:
this text file is a list of files (built with the find command) which can have spaces in their path and in their name (example: /root/.moonchild productions/pale moon/xxx).
With your demo - try som slashes in the DATA:

Code: Select all

echo "My_tailor_is_rich
My/tailor/is/NOT/rich" > /tmp/checkbox_test 
 
and consider this:

Code: Select all

 <action>sed -i 's/$LINE//' /tmp/checkbox_test</action>
In the sed command -you can use | or some other char - instead of the default- /-s -but...?

The i-switch in sed may be convenient - but sed is in reality rewriting the whole file using a tempfile - which is moved to be the updated input-file.
Alternatively-we can emulate that -using awk,grep -v or a read-loop to update the input-file via a tempfile every time we choose a $LINE to be excluded.


----
Anyway -Nice demo.

Posted: Mon 19 Nov 2018, 09:46
by Argolance
Bonjour,
some1 wrote:In the sed command -you can use | or some other char - instead of the default- /-s -but...?
Yes indeed: my "real" script (allowing user to set the font of the UI of browsers like Seamonkey, Firefox, Pale Moon and some others, according to the current GTK theme or any else) uses %, because of the / that are in the found files paths (profiles.ini)...

Code: Select all

CSS_FILE="$(cat /tmp/PROF_PATHS)"

while read -r LINE; do
PROFILES_LIST="${PROFILES_LIST}
	<checkbox tooltip-text=" $(gettext "If unchecked, the 'userChrome.css' configuration file of this profile will not be modified") ">
		<variable>$LINE</variable>
		<default>true</default>
		<label>$LINE</label>
		<action>if false disable:$LINE</action>
		<action>sed -i 's%${LINE}%%' /tmp/PROF_PATHS</action>
	</checkbox>
"
done < /tmp/PROF_PATHS
Cordialement.

Posted: Mon 19 Nov 2018, 18:11
by don570
There is no need to use the read command. Your original script will work with a small change....

Last nite I read Advanced Bash manual
and the answer is to change the IFS

Here's an example with filenames rather than data in a file....


Filenames with embedded whitespace can cause globbing to choke. David Wheeler shows how to avoid many such pitfalls.

1 IFS="$(printf '\n\t')" # Remove space.
2
3 # Correct glob use:
4 # Always use for-loop, prefix glob, check if exists file.
5 for file in ./* ; do # Use ./* ... NEVER bare *
6 if [ -e "$file" ] ; then # Check whether file exists.
7 COMMAND ... "$file" ...
8 fi
9 done
10
11 # This example taken from David Wheeler's site, with permission.
Here's script with two extra lines...

Code: Select all

#!/bin/sh

echo  -n "My_tailor_is_rich
My tailor is NOT rich" > /tmp/checkbox_test

FILE="`cat /tmp/checkbox_test`"
I=$IFS; IFS="$(printf '\n\t')" 

for LINE in $FILE; do
LIST="${LIST}
   <checkbox>
      <variable>$LINE</variable>
      <default>true</default>
      <label>$LINE</label>
      <action>if false disable:$LINE</action>
      <action>sed -i 's/$LINE//' /tmp/checkbox_test</action>
      <action>refresh:TEXT</action>
   </checkbox>
"
done
IFS=$I
export MAIN="
<window window_position=\"1\">
   <vbox>
      <hbox border-width=\"5\">
         <frame Text (/tmp/checkbox_test)>
            <edit>
            <input file>/tmp/checkbox_test</input>
            <variable>TEXT</variable>
            </edit>
         </frame>
         <frame Checkboxes>
            ${LIST}
         </frame>
      </hbox>
      <hbox>
      <button><input file stock=\"gtk-undo\"></input>
         <label>$(gettext 'Reset')</label>
         <action>EXIT:restart</action>
      </button>      
         <button cancel></button>
      </hbox>
   </vbox>
</window>
"
I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog --program=MAIN); do
   eval $STATEMENTS
done
IFS=$I

[ "$EXIT" = "restart" ] && $0

Posted: Tue 20 Nov 2018, 00:45
by Argolance
Hello,
Great! :)
These "tips" are both welcome and work perfectly! I think we're never done learning...
Thank you a lot.

Cordialement.

Posted: Tue 11 Dec 2018, 12:53
by smokey01
Is there some way to make this work?

Code: Select all

<button>
	<label>Test</label>
	<action>if [ -e /tmp/file ]; then state="true"; else state="false"; fi</action>
	<action>echo $state</action>
</button>
I want to check if a file exists, if it does make the variable true, else false.
The variable is always blank.

TIA

Posted: Tue 11 Dec 2018, 13:45
by MochiMoppel

Code: Select all

<button> 
    <label>Test</label> 
    <action>if [ -e /tmp/file ]; then state="true"; else state="false"; fi; echo $state</action> 
</button>
Every <action> runs in its own subshell and variables created in such subshell are usable only in this subshell.

Posted: Tue 11 Dec 2018, 19:29
by smokey01
MochiMoppel wrote:

Code: Select all

<button> 
    <label>Test</label> 
    <action>if [ -e /tmp/file ]; then state="true"; else state="false"; fi; echo $state</action> 
</button>
Every <action> runs in its own subshell and variables created in such subshell are usable only in this subshell.
Thanks MochiMoppel.

Is it possible to use this method to enable/disable buttons.
EG: If the file exists disable the button else enable it.

Code: Select all

<button>
	<label>Status</label>
	<variable>STATUS</variable>
	<action>if [ -e /tmp/file ]; then disable:STATUS; else enable:STATUS; fi;</action>
</button>
I get this error:
sh: disable:STATUS: command not found
Thanks

Posted: Tue 11 Dec 2018, 23:37
by zigbert
Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>

Posted: Wed 12 Dec 2018, 10:27
by smokey01
zigbert wrote:Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>
At first I couldn't get it to work.

The penny has just dropped. It's reading the contents of a file to see if it's true or false. I thought it was checking to see if a file existed.

This will do nicely.

Thanks again.

Posted: Wed 12 Dec 2018, 10:41
by MochiMoppel
Doesn't work for me either. I'm not even sure what file_is_false means. Not existent, not readable?

What should always work is the command_is_true condition:

Code: Select all

<action condition="command_is_true( if [ -e /tmp/file ]; then  echo true ;fi)">disable:STATUS</action>
or shorter:

Code: Select all

<action condition="command_is_true( [ -e /tmp/file ] && echo true)">disable:STATUS</action>

Posted: Wed 12 Dec 2018, 12:58
by smokey01
MochiMoppel wrote:Doesn't work for me either. I'm not even sure what file_is_false means. Not existent, not readable?

What should always work is the command_is_true condition:

Code: Select all

<action condition="command_is_true( if [ -e /tmp/file ]; then  echo true ;fi)">disable:STATUS</action>
or shorter:

Code: Select all

<action condition="command_is_true( [ -e /tmp/file ] && echo true)">disable:STATUS</action>
Ah this one is good too as it checks to see if a file exists rather than reading the contents of the file.

Posted: Wed 12 Dec 2018, 13:16
by MochiMoppel
smokey01 wrote:Ah this one is good too as it checks to see if a file exists rather than reading the contents of the file.
What do you mean by "good too"? It's what you tried to achieve with your example. Reading the contents is not what you asked for.

Posted: Wed 12 Dec 2018, 21:12
by smokey01
MochiMoppel wrote:
smokey01 wrote:Ah this one is good too as it checks to see if a file exists rather than reading the contents of the file.
What do you mean by "good too"? It's what you tried to achieve with your example. Reading the contents is not what you asked for.
You are quite correct and thanks.

I'm trying to automate the process a little more. If I explain the full requirements it might make more sense.

Attached is a logic map for the buttons. Red means disabled and Green enabled. There is actually a stop button which should stop the server and client and enable client and server buttons but not affect Teacher and Student buttons. The Teacher and Student buttons need to be enabled after their apps are closed. It's actually the same app but with different configuration. Not sure how to do this. Currently I have these buttons being enabled with the stop button.

I'm sure the code could be a lot smarter and tidier but it mostly works.

Thanks

Code: Select all

#!/bin/sh

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

MAIN_DIALOG='
<window>
	<vbox>
		<hbox>
			<button>
				<label>Server</label>
				<variable>SERVER</variable>
				<action>touch /tmp/server</action>
				<action>touch /tmp/client</action>
				<action>touch /tmp/teacher</action>
				<action>disable:CLIENT</action>
				<action>disable:TEACHER</action>
				<action>disable:SERVER</action>
			</button>
			
			<button>
				<label>Client</label>
				<variable>CLIENT</variable>
				<action>touch /tmp/client</action>
				<action>touch /tmp/server</action>
				<action>touch /tmp/student</action>
				<action>disable:SERVER</action>
				<action>disable:STUDENT</action>
				<action>disable:CLIENT</action>
			</button>

			<button>
				<label>Teacher</label>
				<variable>TEACHER</variable>
				<action>touch /tmp/teacher</action>
				<action>touch /tmp/student</action>
				<action>touch /tmp/server</action>
				<action>disable:STUDENT</action>
				<action>disable:SERVER</action>
				<action>disable:TEACHER</action>
			</button>
			
			<button>
				<label>Student</label>
				<variable>STUDENT</variable>
				<action>touch /tmp/student</action>
				<action>touch /tmp/teacher</action>
				<action>touch /tmp/client</action>
				<action>disable:TEACHER</action>
				<action>disable:CLIENT</action>
				<action>disable:STUDENT</action>
			</button>
			
			<button tooltip-text="Reset all the buttons">
				<label>Stop</label>
				<variable>STOP</variable>
				<action condition="command_is_true( [ -e /tmp/server ]&& echo true && rm /tmp/server)">enable:SERVER</action>
				<action condition="command_is_true( [ -e /tmp/client ]&& echo true && rm /tmp/client)">enable:CLIENT</action>
				<action condition="command_is_true( [ -e /tmp/teacher ]&& echo true && rm /tmp/teacher)">enable:TEACHER</action>
				<action condition="command_is_true( [ -e /tmp/student ]&& echo true && rm /tmp/student)">enable:STUDENT</action>
			</button>
			
			<button ok></button>
		</hbox>
		
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --program=MAIN_DIALOG ;;
esac

Posted: Thu 13 Dec 2018, 14:52
by MochiMoppel
zigbert wrote:Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>
@zigbert: As smokey01 and I couldn't get this to work I would be keen to learn how this condition is supposed to work. The description in the manual makes no sense to me and I haven't found a single script or example where this condition has been used.
I suspected that "file" would relate to a monitored input file, but tests were negative.
Do you know more?

[Edit] OK, figured it out. file_is_false tests if a given file contains "false", "no" or zero. In smokey01's example the tmp files contain nothing, hence the condition is not met. At least I now understand why nobody uses this condition.

Posted: Thu 13 Dec 2018, 21:03
by zigbert
MochiMoppel wrote:At least I now understand why nobody uses this condition.
I do :oops: :lol:

Posted: Thu 13 Dec 2018, 21:06
by smokey01
MochiMoppel wrote:
zigbert wrote:Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>
@zigbert: As smokey01 and I couldn't get this to work I would be keen to learn how this condition is supposed to work. The description in the manual makes no sense to me and I haven't found a single script or example where this condition has been used.
I suspected that "file" would relate to a monitored input file, but tests were negative.
Do you know more?

[Edit] OK, figured it out. file_is_false tests if a given file contains "false", "no" or zero. In smokey01's example the tmp files contain nothing, hence the condition is not met. At least I now understand why nobody uses this condition.
Once I created a file containing the word false it work as you've also discovered.

I'm still trying to find a way to enable/disable a button depending if a file exists or not in the following situation:

If I use a function that runs psip like this:

run_psip() {
touch /tmp/psip-file
psip
rm /tmp/psip-file
}
export -f run_psip

and call it from a button, the function only runs when the button is clicked.
When psip is terminated the "rm /tmp/psip-file" needs to be run after psip is closed, then a test to see if the file exists. The button in the GUI should be enabled when there is no /tmp/psip-file and disabled when there is.

I thought the <input file> option might work but apparently it only seems to display graphic files on the button widget, if they exist. I also tried it with tag attributes file-monitor and auto-refresh, no joy.

Is there are way to achieve this?

Thanks