Is this possible with a shell script? SOLVED

Forum rules
Before you post please read this

Is this possible with a shell script? SOLVED

Postby viking777 on Wed Dec 11, 2013 7:09 am

I am not a coder, or even a scripter, I have written single line scripts, but never anything complicated, and what I am trying to do falls into the category of complicated - for me anyway.

Years ago I had a distribution which had the ability to let you reboot into a distro of your choice rather than have to sit and wait for the menu to come up and then choose the right one. I found this an incredible time saver, as I have many distros installed and reboot often. The ability to do this disappeared for some reason, but has been resurrected recently by Ubuntu with its 'reboot in' funciton. Unfortunately this function requires Unity quicklists which in turn requires Unity, and the 'scopes' in 13.10 are a step too far afaiac and I won't be using Unity again (I am using Lubuntu now btw). So I am trying to get the same sort of functionality into Mint/Lubuntu via a script.

The difficulty I am having is not the basic function - it would be this:

Code: Select all
#! /bin/bash
sudo grub-reboot X && sudo reboot


The difficulty is in defining 'X'. Where X is a number between 1 and 5 in my case (the number of entries in my grub menu).

What I want is a script that, when executed, would offer a dialog box to enter the required number before executing it.

Maybe that is easy, maybe it is impossible, I don't know, so I though I would ask.
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

Linux Mint is funded by ads and donations.
 

Re: Is this possible with a shell script?

Postby Pilosopong Tasyo on Wed Dec 11, 2013 7:24 am

viking777 wrote:What I want is a script that, when executed, would offer a dialog box to enter the required number before executing it.


Have a look at Zenity or YAD (Yet Another Dialog).

For example, with YAD, this snippet:

Code: Select all
CHOICE=$(yad --form --field=Distribution:CB '1 Ubuntu!2 Mint!3 Debian!4 OpenSuse!5 PCLinuxOS' | cut -c1) ; echo $CHOICE

produces a dropdown dialog box:

Image

and returns the first character (the number) of the chosen entry:

Code: Select all
administrator@dg31pr:~$ CHOICE=$(yad --form --field=Distribution:CB '1 Ubuntu!2 Mint!3 Debian!4 OpenSuse!5 PCLinuxOS' | cut -c1) ; echo $CHOICE
2
administrator@dg31pr:~$ sudo grub-reboot $CHOICE

N.B. Parts of the (GUI-fied) script requires elevated privileges. If you plan on running it as a GUI applet, you may have to create a launcher and use gksudo /path/to/your/reboot-script as command so the 'sudo' portion doesn't have to prompt you for a password (or does it?). I'm not sure if sudo triggers a graphical prompt if a script is not run inside a terminal session. But I'm guessing the sudo command will fail since it expects to be run in a terminal window.
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 5
Level 5
 
Posts: 996
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Is this possible with a shell script?

Postby viking777 on Wed Dec 11, 2013 1:35 pm

Wow! Deep respect for that answer man, it is just what I needed. It hasn't actually solved the problem for me, but it has given me enough to work on to be able to solve it myself (I think!).
Thanks again.
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

Re: Is this possible with a shell script?

Postby Pilosopong Tasyo on Wed Dec 11, 2013 9:27 pm

You're quite welcome! You can always post back to this thread if you need some additional info/clarification. :D
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 5
Level 5
 
Posts: 996
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Is this possible with a shell script?

Postby viking777 on Thu Dec 12, 2013 7:07 am

You are going to have a laugh about this just like I did (not!).

I beat my brains out for hours trying to make this command work properly for me, I rewrote it with pipes, tee commands, read commands, all in varying orders and still I couldn't get it to work so I went to bed. In the morning I suddenly realised that good old grub 2, although it has been rewritten to numerate disk partitions from the number 1 it still counts grub menu entries from zero :evil:

Anyway after having realised that it was all plain sailing, the script you provided needed no alteration whatsoever, (except to set the suid bit to get round it constantly asking for my password) then I wrote a .desktop file to launch it and dragged it onto my docky bar - job done!

Brilliant work, thanks very much :D

Here is a screenshot of it in action:

desktop 1_002.png
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

Re: Is this possible with a shell script? SOLVED Partially

Postby viking777 on Thu Dec 12, 2013 12:30 pm

Damn it!

I was really pleased with my work on this until I realised it has one fundamental flaw - it only works on one distro. Try to apply the same technique to multiple distros and it fails.

grub-reboot works by writing to /boot/grub/grubenv a value for the variable "saved_entry=" which grub then reads and actions on reboot. Trouble is that if your default grub is on Lubuntu as mine is, then in order for the technique to work, then the variable has to be written to /boot/grub/grubenv for Lubuntu. If I am working in MInt, for example, then the variable is written to /boot/grub/grubenv for Mint, not Lubuntu, so the subsequent reboot boots into the default entry 0 not the entry saved in MInt's /boot/grub/grubenv.

Therefore in order for this to work over multiple distros (and multiple versions of grub) I would have to find a way to write to (in my case) /mnt/buntu1310/boot/grub/grubenv from Mint or any other distro.

I am not sure how to achieve that, maybe the 'tee' command might be a way of achieving it, but I haven't found a way to make that work yet.

Perhaps this is why the 'reboot to' command that I used to have years ago died out??
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

Re: Is this possible with a shell script? SOLVED

Postby grizzler on Thu Dec 12, 2013 1:48 pm

Symlink the lot? Make the grubenv in Lubuntu the 'master' and all the others in other distributions symlinks to that one. Just a thought. Haven't actually tried it myself, so no idea whether that would break anything.
grizzler
Level 4
Level 4
 
Posts: 399
Joined: Wed Jun 15, 2011 5:19 pm
Location: The Hague, NL

Re: Is this possible with a shell script? SOLVED

Postby viking777 on Thu Dec 12, 2013 3:02 pm

grizzler wrote:Symlink the lot? Make the grubenv in Lubuntu the 'master' and all the others in other distributions symlinks to that one. Just a thought. Haven't actually tried it myself, so no idea whether that would break anything.


grizzler, that was such a brilliant idea I tried it and what do you know - it worked - at least it worked on two out of three distros. The reason it didn't work on the third is because of a grub version mismatch and the latest grub version sends a different code to grubenv ("next_entry" instead of "saved_entry" and the old grub version won't read it - I doubt there is anything I can do about that except downgrading grub, and it probably isn't worth it).

My only problem with your solution is that I just know that one day I will get a kernel upgrade or somesuch and everything will completely fall apart. Of course that will happen immediately after I have forgotten how I set all this up and I won't have a clue how to repair it :lol:

Still that aside, this has been a very successful and educational little project for me and I would like to sincerely thank both contributors for their input - most valuable :D
Fujitsu Lifebook AH532. Intel i5 processor, 6Gb ram, Intel HD3000 graphics, Intel Audio/wifi. Realtek RTL8111/8168B Ethernet.Lubuntu 13.10,Ubuntu12.10 (Unity), Mint16 (Cinnamon), Manjaro (Xfce).
Image
User avatar
viking777
Level 14
Level 14
 
Posts: 5153
Joined: Mon Dec 01, 2008 11:21 am

Re: Is this possible with a shell script? SOLVED

Postby grizzler on Thu Dec 12, 2013 4:04 pm

viking777 wrote:
grizzler wrote:Symlink the lot? Make the grubenv in Lubuntu the 'master' and all the others in other distributions symlinks to that one. Just a thought. Haven't actually tried it myself, so no idea whether that would break anything.


grizzler, that was such a brilliant idea I tried it and what do you know - it worked

Great.
at least it worked on two out of three distros. The reason it didn't work on the third is because of a grub version mismatch and the latest grub version sends a different code to grubenv ("next_entry" instead of "saved_entry" and the old grub version won't read it - I doubt there is anything I can do about that except downgrading grub, and it probably isn't worth it).

Maybe use grub-editenv to write the required variable to grubenv as well? Again, no idea if that would break anything. I don't know how this value is cleared (grub-reboot is supposed to work for the next reboot only, so it must be cleared somehow...). Of course if that is a problem, using grub-editenv to remove/unset the extra variable before using grub-reboot might be the solution.
My only problem with your solution is that I just know that one day I will get a kernel upgrade or somesuch and everything will completely fall apart. Of course that will happen immediately after I have forgotten how I set all this up and I won't have a clue how to repair it :lol:

Heh. I used to have that kind of problem as well. So I created a list of all the crazy things I did to my installations. Now whenever something unexpected happens, I just check the list. Main problem now is remembering to keep the list up-to-date... :)
Still that aside, this has been a very successful and educational little project for me and I would like to sincerely thank both contributors for their input - most valuable :D

You're welcome.
grizzler
Level 4
Level 4
 
Posts: 399
Joined: Wed Jun 15, 2011 5:19 pm
Location: The Hague, NL

Re: Is this possible with a shell script? SOLVED

Postby Pilosopong Tasyo on Fri Dec 13, 2013 1:04 am

viking777 wrote:Still that aside, this has been a very successful and educational little project for me and I would like to sincerely thank both contributors for their input - most valuable :D

Cool! It didn't occur to me that grub entries start from 0 though (I'm not a multi-boot fan/distrohopper). I'd be thrown in a loop as well if I was in your shoes. :lol:
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
Pilosopong Tasyo
Level 5
Level 5
 
Posts: 996
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 3 guests