Page 1 of 1

mkautostart infinite loop

Posted: Tue Dec 17, 2024 3:32 am
by AntonioPt

Hello everyone,
I have a doubt since this script if I click 2x in gui mode it will make me an infinite loop I would like to know how it runs or how it is called to see how I can remove this bug
inside function interactive i could add the option if i was running via terminal or not to avoid infinite loop but don't think its the best hack but ...
exe:

Code: Select all

GI="$(ps aux | grep -e " X \|Xorg\|xinit\|xwin\|wayland" | grep -v grep)"
if [ -t 0 -o -z "${GI}" ] ; then
	code that runs well via terminal even tho doesn't make sense to me
fi

thxx in advance


Re: mkautostart infinite loop

Posted: Wed Dec 18, 2024 11:29 pm
by chris_r

I don't know if it the the cause of your problem, but the un-escaped quotation marks in the first line look questionable (no pun intended.)


Re: mkautostart infinite loop

Posted: Thu Dec 19, 2024 2:46 am
by MochiMoppel
chris_r wrote: Wed Dec 18, 2024 11:29 pm

the un-escaped quotation marks in the first line look questionable

See what happens when you escape them ;) They must not be escaped and are fine as they are. However the whole line is questionable because I can't see a reason to create $GI

@AntonioPt mkautostart MUST be called from a terminal. It is not designed to be run from a GUI. What you probably experience when you try to start it from a GUI or click it in ROX is not an infinite loop but rather the script "hangs" when it waits for user input ("Type the name of executable that you want to autostart") and can't get the necessary user input in order to proceed.


Re: mkautostart infinite loop

Posted: Thu Dec 19, 2024 8:14 am
by mistfire
AntonioPt wrote: Tue Dec 17, 2024 3:32 am

Hello everyone,
I have a doubt since this script if I click 2x in gui mode it will make me an infinite loop I would like to know how it runs or how it is called to see how I can remove this bug
inside function interactive i could add the option if i was running via terminal or not to avoid infinite loop but don't think its the best hack but ...
exe:

Code: Select all

GI="$(ps aux | grep -e " X \|Xorg\|xinit\|xwin\|wayland" | grep -v grep)"
if [ -t 0 -o -z "${GI}" ] ; then
	code that runs well via terminal even tho doesn't make sense to me
fi

thxx in advance

Dealing with Wayland? Wayland is just a protocol. instead of grepping wayland just add these into your grep if you are dealing with wayland

Hyprland
sway
river
labwc
mutter
kwin
wayfire


Re: mkautostart infinite loop

Posted: Thu Dec 19, 2024 9:15 am
by AntonioPt

Hello all,

First of all thxx for everyone replay
@chris_r like i said it i need it help so any feedback is more then welcome :) 2 i just share an idea to avoid the script from hanging if user click 2x like you see on the video i share :D

@MochiMoppel you're right it's not an infinite loop but a pause until the user enters something and I already knew that if I created a condition to check if I'm running via terminal or not it would solve the problem if I clicked 2x by mistake but I didn't know if it was the best option so I asked for help. :)

Hi @mistfire this piece of code is from @KuLuSz my bash Guru from Discord and I stole it from him "but please don't tell him anything lol" I added Wayland because I also use MINT and I read somewhere that to check if I was running the script via terminal or not this was the solution if it's the right one I can't say because I'm still in the learning process.


Re: mkautostart infinite loop

Posted: Thu Dec 19, 2024 11:57 am
by MochiMoppel
AntonioPt wrote: Thu Dec 19, 2024 9:15 am

I already knew that if I created a condition to check if I'm running via terminal or not it would solve the problem if I clicked 2x by mistake but I didn't know if it was the best option so I asked for help. :)

The condition you created should solve your problem though the grepping stuff is redundant and may not even work properly. If you want to make it simpler you could add a condition at the start of your script:

Code: Select all

#! /bin/bash
[ -t 0 ] || exit
<your interactive terminal code follows>

This will prevent the execution of any code that follows if the file descriptor 0 (=stdin) is not open, i.e. if the code is not run in a terminal.


Re: mkautostart infinite loop

Posted: Thu Dec 19, 2024 4:08 pm
by AntonioPt

Thankyou

MochiMoppel wrote: Thu Dec 19, 2024 11:57 am
AntonioPt wrote: Thu Dec 19, 2024 9:15 am

I already knew that if I created a condition to check if I'm running via terminal or not it would solve the problem if I clicked 2x by mistake but I didn't know if it was the best option so I asked for help. :)

The condition you created should solve your problem though the grepping stuff is redundant and may not even work properly. If you want to make it simpler you could add a condition at the start of your script:

Code: Select all

#! /bin/bash
[ -t 0 ] || exit
<your interactive terminal code follows>

This will prevent the execution of any code that follows if the file descriptor 0 (=stdin) is not open, i.e. if the code is not run in a terminal.


Re: mkautostart infinite loop

Posted: Thu Dec 19, 2024 4:18 pm
by d-pupp

@MochiMoppel
I was looking for this exact code a few days ago to stop scripts from running if I wasn't in a terminal.
However the code I found used file descripter 1 (standard out).
Is there any advantage for using one or the other?


Re: mkautostart infinite loop

Posted: Fri Dec 20, 2024 4:49 am
by MochiMoppel
d-pupp wrote: Thu Dec 19, 2024 4:18 pm

However the code I found used file descripter 1 (standard out).
Is there any advantage for using one or the other?

No advantage. They both should work fine.

However I find that just exiting the script when it is accidentally clicked in ROX might be a bit rude. Some kind of explanation should be given to the user.
Even better: If terminal isn't already open, let the script open a terminal and load itself into the newly opened terminal.

Example:

Code: Select all

#! /bin/bash
[ -t 0 ] || exec defaultterminal -e "$0"
read -p "Please enter your name: " name
echo "Hello $name!"
sleep 2