How to Make a Clickable Script

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

How to Make a Clickable Script

#1 Post by rcrsn51 »

You may have scripts that you use regularly, but must open a terminal window to run them. Here is a simple method for making a script clickable. You can then put a shortcut to the script on your desktop.

Just place the following three lines at the top of the script (without any blank lines):

Code: Select all

#!/bin/sh
tail -n +4 $0  > /tmp/script.txt
exec rxvt -e sh /tmp/script.txt
To leave the terminal window open when the script is done, add this line to the end:

Code: Select all

read
Here is an example. When I'm working with flash drives, I like to run the sync command regularly to ensure that data has been written out to the device. So I made a script called "mysync" and put it in /root/my-applications/bin. I then made a desktop shortcut by dragging the script onto the desktop. That way, I can see the sync command running and I know when it's done.

Code: Select all

#!/bin/sh
tail -n +4 $0 > /tmp/script.txt
exec rxvt -e sh /tmp/script.txt
echo Syncing...
sync
Last edited by rcrsn51 on Sat 02 Feb 2013, 14:48, edited 5 times in total.

User avatar
abushcrafter
Posts: 1418
Joined: Fri 30 Oct 2009, 16:57
Location: England
Contact:

#2 Post by abushcrafter »

Thanks.
[url=http://www.adobe.com/flashplatform/]adobe flash is rubbish![/url]
My Quote:"Humans are stupid, though some are clever but stupid." http://www.dependent.de/media/audio/mp3/System_Syn_Heres_to_You.zip http://www.systemsyn.com/

User avatar
abushcrafter
Posts: 1418
Joined: Fri 30 Oct 2009, 16:57
Location: England
Contact:

#3 Post by abushcrafter »

You need the 2 main commands to be on the same line with "&&" in-between. Other wise the exec command ends up in the tmp file. So it ends up in a loop where it keeps running more copy's of it self till it crashes your OS from using up all of your ram :(.

Code: Select all

cat $0 | tail -n +4 > "/tmp/download_magatune_song-of-the-today_tmp.sh" && exec rxvt -e sh "/tmp/download_magatune_song-of-the-today_tmp.sh"
I also added a rm command to delete the tmp file at the end of the script.
[url=http://www.adobe.com/flashplatform/]adobe flash is rubbish![/url]
My Quote:"Humans are stupid, though some are clever but stupid." http://www.dependent.de/media/audio/mp3/System_Syn_Heres_to_You.zip http://www.systemsyn.com/

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#4 Post by rcrsn51 »

Make sure that the first three lines of your script are as shown.

Then the "tail -n +4" copies everything starting at line 4 into the temp file.

User avatar
abushcrafter
Posts: 1418
Joined: Fri 30 Oct 2009, 16:57
Location: England
Contact:

#5 Post by abushcrafter »

rcrsn51 wrote:Make sure that the first three lines of your script are as shown.

Then the "tail -n +4" copies everything starting at line 4 into the temp file.
Arrr... I get it now. I had a new line after "#!/bin/sh".
[url=http://www.adobe.com/flashplatform/]adobe flash is rubbish![/url]
My Quote:"Humans are stupid, though some are clever but stupid." http://www.dependent.de/media/audio/mp3/System_Syn_Heres_to_You.zip http://www.systemsyn.com/

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#6 Post by rcrsn51 »

abushcrafter wrote: I had a new line after "#!/bin/sh".
That will do it.

Also, you don't need to delete the script afterward. The /tmp folder is automatically erased.

User avatar
abushcrafter
Posts: 1418
Joined: Fri 30 Oct 2009, 16:57
Location: England
Contact:

#7 Post by abushcrafter »

rcrsn51 wrote:
abushcrafter wrote: I had a new line after "#!/bin/sh".
That will do it.

Also, you don't need to delete the script afterward. The /tmp folder is automatically erased.
Oh.
[url=http://www.adobe.com/flashplatform/]adobe flash is rubbish![/url]
My Quote:"Humans are stupid, though some are clever but stupid." http://www.dependent.de/media/audio/mp3/System_Syn_Heres_to_You.zip http://www.systemsyn.com/

User avatar
steve_s
Posts: 1595
Joined: Mon 26 May 2008, 13:29
Location: Austin, TX, USA
Contact:

#8 Post by steve_s »

Thanks for the script tips...keep 'em coming! this is very useful stuff and very appreciated...

User avatar
piratesmack
Posts: 100
Joined: Wed 16 Sep 2009, 14:22

#9 Post by piratesmack »

Yes, thanks for the tip.

I modified slightly so the script doesn't have to start at exactly 4 lines

Code: Select all

#!/bin/sh

# figure out which line the script starts at
LINE=$(awk '/^START/ { print NR + 1 }' $0)
tail -n+$LINE $0 > /tmp/myscript.txt
exec rxvt -e sh /tmp/myscript.txt

START # script starts after this line
echo "Hello, world!"
read

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

Re: How to Make a Clickable Script

#10 Post by greengeek »

rcrsn51 wrote:Here is an example. When I'm working with flash drives, I like to run the sync command regularly to ensure that data has been written out to the device. So I made a script called "mysync" and put it in /root/my-applications/bin. I then made a desktop shortcut by dragging the script onto the desktop. That way, I can see the sync command running and I know when it's done.

Code: Select all

#!/bin/sh
tail -n +4 $0 > /tmp/script.txt
exec rxvt -e sh /tmp/script.txt
echo Syncing...
sync
Hi rcrsn51 - I just did some brief testing with this and the terminal disappeared immediately so I was unable to see anything that was happening. Is that normal, and merely an indication that there was no data to write out?
EDIT : Further testing with larger files and a slow usb stick suggets yes, the terminal disappears as soon as the sync is finished. Excellent.
To leave the terminal window open when the script is done, add this line to the end:

Code: Select all

read
I tried appending read as the last line but then the terminal just stayed open but giving no indication of sync having ended as shown here:
Attachments
sync_hang.jpg
(31.95 KiB) Downloaded 363 times

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#11 Post by greengeek »

EDIT : I tried adding an extra line to the script as follows:

Code: Select all

#!/bin/sh
tail -n +4 $0 > /tmp/script.txt
exec rxvt -e sh /tmp/script.txt
echo Syncing...
sync
echo Sync complete.
read
and the outcome seems to work for me. I know when the sync is still active, I know when the sync is complete, and the terminal stays open for me to notice the outcome if I've been looking the other way at the moment the sync ends (see pic).
Is this method ok?
cheers!
Attachments
SyncComplete.jpg
(4.67 KiB) Downloaded 352 times

User avatar
puppyluvr
Posts: 3470
Joined: Sun 06 Jan 2008, 23:14
Location: Chickasha Oklahoma
Contact:

#12 Post by puppyluvr »

:D Hello,
Thank you!
Close the Windows, and open your eyes, to a whole new world
I am Lead Dog of the
Puppy Linux Users Group on Facebook
Join us!

Puppy since 2.15CE...

Post Reply