mkautostart infinite loop

interpretive language scripts


Moderator: Forum moderators

Post Reply
User avatar
AntonioPt
Posts: 228
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 103 times
Been thanked: 38 times

mkautostart infinite loop

Post 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

Attachments
mkautostart.mp4.7z
(204.71 KiB) Downloaded 11 times

Why astronauts use Linux
Because you can't open windows in space

chris_r
Posts: 11
Joined: Sun Dec 27, 2020 2:32 pm
Been thanked: 2 times

Re: mkautostart infinite loop

Post 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.)

User avatar
MochiMoppel
Posts: 1257
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 455 times

Re: mkautostart infinite loop

Post 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.

mistfire
Posts: 739
Joined: Thu Jul 16, 2020 2:16 am
Location: CALABARZON, PH
Has thanked: 3 times
Been thanked: 182 times

Re: mkautostart infinite loop

Post 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

User avatar
AntonioPt
Posts: 228
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 103 times
Been thanked: 38 times

Re: mkautostart infinite loop

Post 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.

Why astronauts use Linux
Because you can't open windows in space

User avatar
MochiMoppel
Posts: 1257
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 455 times

Re: mkautostart infinite loop

Post 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.

User avatar
AntonioPt
Posts: 228
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 103 times
Been thanked: 38 times

Re: mkautostart infinite loop

Post 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.

Why astronauts use Linux
Because you can't open windows in space

d-pupp
Posts: 361
Joined: Tue Nov 22, 2022 9:11 pm
Location: Canada
Has thanked: 204 times
Been thanked: 58 times

Re: mkautostart infinite loop

Post 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?

User avatar
MochiMoppel
Posts: 1257
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 455 times

Re: mkautostart infinite loop

Post 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
Post Reply

Return to “Scripts”