LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Converting a Linux script to a desktop launcher (https://www.linuxquestions.org/questions/linux-software-2/converting-a-linux-script-to-a-desktop-launcher-4175644761/)

julianvb 12-21-2018 06:37 PM

Converting a Linux script to a desktop launcher
 
2018-12-21

I wish to convert the following alias 'tel' or its associated script to a desktop launcher. When I run this alias from the terminal, it always works. I find that most terminal commands would work in a launcher file but not 'bash'.

If I write Command=bash ~/info1.sh and try to run this .desktop file, I get a runtime error 'There was an error launching this application.' I feel that I am not going about this matter the right way. Any help will be much appreciated.

Julianvb

1. File info1.txt consists of just the following lines.
Quote:

#! /bin/sh
echo "Pls enter name whose phone number you need."
read name
grep -i $name ~/fones.txt
exit
2. I define alias 'tel' in my .bash_aliases file as follows:
Quote:

alias tel='bash ~/info1.txt'
3. File fones.txt just consists of lines with names and associated phone numbers.

nodir 12-21-2018 07:09 PM

you probably ask for something like:
xterm -e script_name
if you want to add a desktop-launcher for a script.
Other terminals have a simimilar solution. do "man your_terminal" and search for -e.
I am not that sure if an alias will work with that, it usually is used for the shell, and doesn't do that much(functions might work instead, not sure either, but: ).
Just put the script in ~/bin and use that., make the desktop-launcher use "whichever-terminal -e scriptname" to launch it.

A few things, just saying them, nothing is written in stone:
info1.text is used for both, the scriptname and the file you grep. (?).
There is no need to add an extension to a shell script., like .txt. Call it grep_fones or such, without extension.
Quote the expansion of variables, so "$name", not $name.
No need for "exit".
#!/bin/sh is not bash, so if you want bash make the shebang use bash, probably #!/bin/bash in your case.
uhm. i think the terminal will exit as soon the script finishes. You will need a solution to not close before you have seen the output. Ask the user for further input via read before the script "exits", or such.

hydrurga 12-21-2018 07:20 PM

A couple of minor general points.

Note that you said that you used the command

Code:

Command=bash ~/info1.sh
but you then go on to tell us that the file is called info1.txt.

Also, it may be better to use full path names in scripts rather than using the ~ shortcut.

Finally, if the script is to be interpreted by bash then it should have

Code:

#!/bin/bash
at the top, not

Code:

#! /bin/sh

Mechanikx 12-21-2018 07:23 PM

Quote:

Originally Posted by nodir (Post 5940150)
#!/bin/sh is not bash, so if you want bash make the shebang use bash, probably #!/bin/bash in your case.

/bin/sh is a symlink to bash on many systems. But you are right that they should use #!/bin/bash if they'll be using features specific to bash.

nodir 12-21-2018 07:27 PM

Quote:

Originally Posted by Mechanikx (Post 5940154)
/bin/sh is a symlink to bash on many systems. But you are right that they should use #!/bin/bash if they'll be using features specific to bash.

yup, of course. I probably wanted to say "may not be bash ".
Thanks for making it more clear.

most of what i said after "not written in stone" is just a sidenote.
he will get away with it, as far i can see.

BW-userx 12-21-2018 07:28 PM

Code:

#!/bin/bash
read -p "Name please " name
grep -i $name list
echo;echo "q to quit"
read g
[[ "$g" = 'q' ]]  && exit

you will need to add that so it keeps your terminal open so you can reference whatever number you are looking up, else, it will just grep then exit, closing your terminal.

Code:

xterm -geometry 85x30 -bg black -fg orange -T "Phone Numbers" -e scriptName
that you can change to suit your needs.

Mechanikx 12-21-2018 07:50 PM

Quote:

Originally Posted by nodir (Post 5940156)
yup, of course. I probably wanted to say "may not be bash ".
Thanks for making it more clear.

most of what i said after "not written in stone" is just a sidenote.
he will get away with it, as far i can see.

No worries. It was a minor quibble on my part :)

julianvb 12-21-2018 08:10 PM

nodir, hydrurga, BW-userx and Mechanikx,

Honestly I wasn't aware of the fine points about Bash. I copied the old shebang from some place long ago when I first started writing scripts. Since they all seemed to work, I moved on ignorantly. My info1 file normally appears as info1.txt in my .bash_aliases file and in my 'tel' alias. I renamed it only tonight but it still works as far my alias 'tel' or the execution of the info1 script goes.

Thank you all very much for your suggestions and sharing your extensive knowledge with me. My Linux Mint system has both xterm and uxterm. When I run the following, the script quits prematurely after I enter an input to it.
Code:

xterm -e info1.sh
whereas
Code:

bash info1.sh
always responds with the proper output after I enter an input to it.

Julianvb

BW-userx 12-21-2018 08:17 PM

Quote:

Originally Posted by julianvb (Post 5940165)
nodir,

Thank you very much for sharing your extensive knowledge with me. My Linux Mint system has both xterm and uxterm. When I run the following, the script quits prematurely after I enter an input to it.
Code:

xterm -e info1.sh
whereas
Code:

bash info1.sh
always responds with the proper output after I enter an input to it.


Julianvb

did you miss reading post #6???

julianvb 12-21-2018 08:37 PM

Quote:

Quote:
Originally Posted by nodir View Post
yup, of course. I probably wanted to say "may not be bash ".
Thanks for making it more clear.

most of what i said after "not written in stone" is just a sidenote.
he will get away with it, as far i can see.
No worries. It was a minor quibble on my part
BW-userx,

I am kind of dense. I read Reply #6 and still fail to grasp its gist. Please help me. Clearly you folks are much younger and sharper than I'm. I regret to admit it; time has a way of pushing me toward 90.

Julianvb

BW-userx 12-21-2018 08:49 PM

Quote:

Originally Posted by julianvb (Post 5940172)
BW-userx,

I am kind of dense. I read Reply #6 and still fail to grasp its gist. Please help me. Clearly you folks are much younger and sharper than I'm. I regret to admit it; time has a way of pushing me toward 90.

Julianvb

if you use the methoid of calling a terminal to execute your script, as soon as it is completed, as you've seen it exit the terminal, thus you cannot read the ph number, the script above it prevents the terminal from exiting until the user enters the retuired q for quit.

'read' holds the terminal open until something is entered. Everything after xterm is just telling xterm what size, font color -fg and background color -bg , T is for title, -e is command to execute.


this is just another methoid of doing what you're asking to do.

julianvb 12-21-2018 09:36 PM

Quote:

#!/bin/bash
read -p "Name please " name
grep -i $name list
echo;echo "q to quit"
read g
[[ "$g" = 'q' ]] && exit
BW-userx,

Thank you so much for your great help. I've just implemented your much better version of my info1 script and it works just like my original one. Of course, your is more elegant. You may regard my primitive script a quick and dirty one even though it always works OK.

My basic question is 'How does one write a desktop launcher version of a Bash script step by step ?" I hope you'll bear with my slow wit.

Julianvb

julianvb 12-21-2018 11:31 PM

Quote:

1. File info1.txt consists of just the following lines.
Quote:
#! /bin/sh
echo "Pls enter name whose phone number you need."
read name
grep -i $name ~/fones.txt
read
2. I define alias 'tel' in my .bash_aliases file as follows:
Quote:
alias tel='bash ~/info1.txt'
3. File fones.txt just consists of lines with names and associated phone numbers.
Hi, Everyone,

In order to make my script info1.sh work with the Desktop instead of the terminal only, I simply replaced 'exit' with 'read' as the final command in the file to keep the screen open. Linux Mint does not care whether $name is double-quoted or not and accepts the shebanq #! /bin/sh and #! /bin/bash for bash scripts according to my experience. You may call me dumb-lucky or a lucky dummy.

The following is my working desktop launcher file:
Quote:

[Desktop Entry]
Comment=
Terminal=true
Name=tell
Exec=xterm -e /home/el/info1.sh
Type=Application
Finally I beg you all to forgive me for being so sloppy and inelegant as I cannot afford the luxury to strive for purity. I am a retired electrical engineer, software and hardware. Pragmatism is my life-long religion. However, I bear no ill will toward purists. The world is vast enough to embrace both purists and pragmatists.

Julianvb

BW-userx 12-22-2018 08:05 AM

Quote:

Originally Posted by julianvb (Post 5940205)
Hi, Everyone,

In order to make my script info1.sh work with the Desktop instead of the terminal only, I simply replaced 'exit' with 'read' as the final command in the file to keep the screen open. Linux Mint does not care whether $name is double-quoted or not and accepts the shebanq #! /bin/sh and #! /bin/bash for bash scripts according to my experience. You may call me dumb-lucky or a lucky dummy.

The following is my working desktop launcher file:

Finally I beg you all to forgive me for being so sloppy and inelegant as I cannot afford the luxury to strive for purity. I am a retired electrical engineer, software and hardware. Pragmatism is my life-long religion. However, I bear no ill will toward purists. The world is vast enough to embrace both purists and pragmatists.

Julianvb

so its working?

if you are just issuing commands in a electronic piece of paper, or e-paper as I call it sometimes. You don't need a shebang because it is not bash it is just a file made executable to issue commands in what is called a wrapper.

I've got one that inside the file it is just simply
Code:

/media/data1/scripts/resamplemp3s
the name of that file is called resamplemp3,

and it is placed in my /usr/local/bin. So all I have to do is open a terminal and type, resamplemp3. That cuts down on typing. Removing the need to type in the absolute path and script name, or changing in to the dir then typing ./resamplemp3s
just remember to make it executable

Code:

chmod +x filename
now all I have to do is type resamplemp3 and it starts the script.

all of this works wihtout a shebang

Code:



echo "bob"
mkdir deleteme
well="go back before its too late"
echo $well
#chop it off
what=${well#*before}
echo $what

read -p "give me a value " value

if [[ $value = "no" ]] ; then
        echo "why not?"
else
        echo $value
fi


nodir 12-22-2018 09:11 AM

Quote:

Originally Posted by julianvb (Post 5940205)
Finally I beg you all to forgive me for being so sloppy and inelegant as I cannot afford the luxury to strive for purity.

I am not at a scripting level where i could evaluate others, not even close, but to me:
you neither have been outstanding sloppy nor inelegant, and sure not outstanding slow.
I hardly add scripts as desktop-launchers, but when i did, it sure confused the heck out of me and took me longer to get it sorted (as said: you might say i am slow and got no talent ... :-) ).

to give one example: many people don't quote variable expansion, some as they don't know any better, some as it is not always strictly needed.
Perhaps i didn't say it clear enough, but my main info was "termina-emulator -e", and the rest was just "into the bargain" (as i see now: perhaps confusing, superfluous).
I hesitated quite a bit,
as you didn't ask for it, none of it was standing in your way to solve the question you have stated very clearly in your first post.

Short version: to me you are doing fine. Solving the stuff as you run in them.

To add something useful too: For the future you might want to sure
https://shellcheck.net
(assuming you will run in problems which might relate to those fine details as the used shebang. They might not relate to it, but at least you can exclude it with shellcheck). I guess i always prefer asking in a forum or irc channel, as i get better explanations and extra-info ... but it is a good tool.


All times are GMT -5. The time now is 02:56 AM.