Menu item for shell script opens it as text file instead of running it (SOLVED, albeit in a mystifying way)

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Menu item for shell script opens it as text file instead of running it (SOLVED, albeit in a mystifying way)

Post by arclights »

For solution, see Mikeflan's comment. Neither he nor I can explain why the two slight differences between what I had been doing and what he prescribed make a difference. But the fact is, when I did it his way, it worked.

I am running Linux Mint 21.2 Cinnamon with Cinnamon version 5.8.4.

To solve a problem I keep having with my touchscreen, I have created a shell script (is that the correct term?). Its entire contents are as follows:
#!/bin/bash
xinput map-to-output 11 HDMI-0
xinput map-to-output 12 HDMI-0
This is my first time writing a script in Linux, I think.

I created a menu item for the script. I checked the box for "launch in Terminal." But when I click the menu item, the script does not launch, in Terminal or otherwise. Instead, it opens in Text Editor.

When I double-click on the file in Nemo, it opens a dialog box says it's an executable text file and asks me whether I want to run it or display its contents. If I select "Run," a Terminal window briefly flashes onscreen and, judging by the fact that my touchscreen started working properly the first time it did that, I infer that that's the script executing correctly. But when I click the menu item, I don't even get the dialog box; I just get Text Editor with the script open in it for editing.

How do I make it so that when I click the menu item, it runs the script? It would be nice if double-clicking the file also just ran the script, but I can live with the dialog box if I have to.

Thank you!
Last edited by arclights on Fri Mar 29, 2024 12:23 am, edited 2 times in total.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Menu item for shell script opens it as text file instead of running it

Post by xenopeek »

What is the command you put in the menu launcher?

Also "launch in terminal" doesn't do anything here. Your script is not taking user input so the terminal will exit after the script is done.
Image
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

I selected the script with the browse function. It produced this command:
/home/arc/AppImages/Fix\ touchscreen.sh
"Fix touchscreen.sh" is the filename of the script, and I put the script in the folder I originally created for AppImages. It is clear that the path and filename are correct, because the menu item causes the script to open in Text Editor.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: Menu item for shell script opens it as text file instead of running it

Post by xenopeek »

Try changing the command to bash '/home/arc/AppImages/Fix touchscreen.sh'
Image
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

Thanks, but that didn't work. Instead, it caused the menu item to show a text-file icon instead of a rocket icon and the filename of the script (including extension) instead of the name I specified for the menu item.
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Menu item for shell script opens it as text file instead of running it

Post by mikeflan »

that didn't work. Instead, it caused the menu item to show a text-file icon instead of a rocket icon and the filename of the script (including extension)
It worked perfectly for me. It still shows a rocket icon.

Click in the Command line - Ctrl-A to highlight the whole command - Ctrl-C - and paste to this forum.

It worked perfectly for me if I have either of these two in the Command line:
bash '/home/mike/.local/bin/resize.sh'
or
/home/mike/.local/bin/resize.sh

The only difference I see is that I have no space in my path and filename. But that should not matter.
billyswong
Level 8
Level 8
Posts: 2240
Joined: Wed Aug 14, 2019 1:02 am

Re: Menu item for shell script opens it as text file instead of running it

Post by billyswong »

In file manager, navigate to /home/arc/.local/share/applications. See if you can find the .desktop file created for your script. Drag the file icon into xed the text editor to show its content. Maybe we can study it and see what went wrong with it. You may also double click the .desktop file to try if it get launched properly.
vimes666
Level 6
Level 6
Posts: 1241
Joined: Tue Jan 19, 2016 6:08 pm

Re: Menu item for shell script opens it as text file instead of running it

Post by vimes666 »

arclights wrote: Tue Mar 26, 2024 12:02 pm
#!/bin/bash
xinput map-to-output 11 HDMI-0
xinput map-to-output 12 HDMI-0
Shouldn't it be
#!/bin/bash
xinput --map-to-output 11 HDMI-0
xinput --map-to-output 12 HDMI-0
?
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

mikeflan wrote: Tue Mar 26, 2024 10:13 pm Click in the Command line - Ctrl-A to highlight the whole command - Ctrl-C - and paste to this forum.

It worked perfectly for me if I have either of these two in the Command line:
bash '/home/mike/.local/bin/resize.sh'
or
/home/mike/.local/bin/resize.sh

The only difference I see is that I have no space in my path and filename. But that should not matter.
I got rid of the space.

With the command line
/home/arc/AppImages/FixTScrn.sh
it opened it in the text editor.

With the command line
bash '/home/arc/AppImages/FixTScrn.sh'
it still opened it in the text editor.

And yes, I used ctrl+A to select the whole line both times.
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

vimes666 wrote: Wed Mar 27, 2024 7:09 am
arclights wrote: Tue Mar 26, 2024 12:02 pm
#!/bin/bash
xinput map-to-output 11 HDMI-0
xinput map-to-output 12 HDMI-0
Shouldn't it be
#!/bin/bash
xinput --map-to-output 11 HDMI-0
xinput --map-to-output 12 HDMI-0
?
I don't need the leading hyphens when I input the commands directly in Terminal, and the script seems to be working when it actually runs. (It runs if I find it in the file manager, double-click it, and choose "run" in the resulting dialog box.) But thanks for the suggestion.
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

billyswong wrote: Wed Mar 27, 2024 5:52 am In file manager, navigate to /home/arc/.local/share/applications. See if you can find the .desktop file created for your script. Drag the file icon into xed the text editor to show its content. Maybe we can study it and see what went wrong with it. You may also double click the .desktop file to try if it get launched properly.
There is no file there whose filename is the filename of the script plus ".desktop."

There is a file there called "Fix Touchscreen," but that shows a command line from a menu item that was an attempt to run the two xinput commands in the script directly from the menu item by joining them with a semicolon.
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Menu item for shell script opens it as text file instead of running it

Post by mikeflan »

Type this in a terminal:

Code: Select all

echo $PATH | tr : \\n | sort
If /home/arc/AppImages/ is not listed I suggest you move the sh file to one of the PATH folders. /home/arc/.local/bin/ might be one of the better places for it if that is listed in your PATH.

Then you have to rebuild the menu item so it has the rocket icon and the new location.
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

mikeflan wrote: Wed Mar 27, 2024 7:17 pm Type this in a terminal:

Code: Select all

echo $PATH | tr : \\n | sort
If /home/arc/AppImages/ is not listed I suggest you move the sh file to one of the PATH folders. /home/arc/.local/bin/ might be one of the better places for it if that is listed in your PATH.

Then you have to rebuild the menu item so it has the rocket icon and the new location.
This sounded like it might work, but my PATH did not list any folders with my username in their names. So I renamed AppImages to exe and added that to the PATH using .bashrc. Then I created a new menu item for the script in its new location.

I'm not sure what happened then. The menu does not show an item with the name I gave it in the menu editor. It does show an item whose name is the filename of the script, including the .sh extension, which I didn't include in the name field of the menu item. So I suspect this menu item is actually generated by the function that adds recent documents to the menu. In any event, this menu item opens the script in the text editor; it does not run the script.
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Menu item for shell script opens it as text file instead of running it

Post by mikeflan »

I'm not sure what happened then.
Me either.

Are you starting Menu Editor? And going into a subdirectory like TestMenu-MJF? And clicking New Item?
If you create a new item this way and it does not show as an item, I don't know what to do next.
.
menu.png
billyswong
Level 8
Level 8
Posts: 2240
Joined: Wed Aug 14, 2019 1:02 am

Re: Menu item for shell script opens it as text file instead of running it

Post by billyswong »

arclights wrote: Wed Mar 27, 2024 7:16 pm There is no file there whose filename is the filename of the script plus ".desktop."

There is a file there called "Fix Touchscreen," but that shows a command line from a menu item that was an attempt to run the two xinput commands in the script directly from the menu item by joining them with a semicolon.
Okay. How about this. In terminal, run

Code: Select all

ls -l ~/.local/share/applications
It shall show the true .desktop file name of your "Fix Touchscreen". Hopefully this time we can take a look of the actual content of that file. Not a paraphrase.
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

mikeflan wrote: Wed Mar 27, 2024 8:25 pm
I'm not sure what happened then.
Me either.

Are you starting Menu Editor? And going into a subdirectory like TestMenu-MJF? And clicking New Item?
If you create a new item this way and it does not show as an item, I don't know what to do next.
.
menu.png
I have been going into the Menu Editor, but I've been accessing it by right-clicking the menu button and selecting "Edit menu," which skips the first step of your illustration. And I haven't been going into a subdirectory. But I have been clicking "New Item."

I just tried it your way. This time, the new menu item appeared, and when I used it, the script did not open in the text editor. Nothing visible happened at all -- which may mean the script ran successfully. Thank you very much!

Do you have any idea why either using that additional step to launch the menu editor or putting the menu item in a subdirectory would have made a difference?
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Menu item for shell script opens it as text file instead of running it

Post by mikeflan »

Do you have any idea why either using that additional step to launch the menu editor or putting the menu item in a subdirectory would have made a difference?
No, I don't. The menu is a bit klunky if you try to get creative with it. It tends to work pretty good for me since I just stick to the basics. I don't use that subdirectory I showed except for experimentation.
arclights
Level 1
Level 1
Posts: 13
Joined: Tue Mar 26, 2024 11:47 am

Re: Menu item for shell script opens it as text file instead of running it

Post by arclights »

mikeflan wrote: Thu Mar 28, 2024 8:28 pm
Do you have any idea why either using that additional step to launch the menu editor or putting the menu item in a subdirectory would have made a difference?
No, I don't. The menu is a bit klunky if you try to get creative with it. It tends to work pretty good for me since I just stick to the basics. I don't use that subdirectory I showed except for experimentation.
It doesn't make sense to me that these changes would make a difference. But the fact is, they did. The shortcut I created in a subdirectory of the menu, after entering the menu editor by the slightly longer route, does indeed launch the script, I can now confirm.

Thank you, and thanks everyone!
billyswong
Level 8
Level 8
Posts: 2240
Joined: Wed Aug 14, 2019 1:02 am

Re: Menu item for shell script opens it as text file instead of running it (SOLVED, albeit in a mystifying way)

Post by billyswong »

I suspect there are some bugs in the menu editor. But we need someone take the patience and create a reproducible bug report. Thus why I asked for reading the .desktop file content directly in the earlier reply, in order to bypass any potential bug.
Post Reply

Return to “Scripts & Bash”