LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Slackware Configuration Script - my first script (https://www.linuxquestions.org/questions/slackware-14/slackware-configuration-script-my-first-script-4175492694/)

moisespedro 01-27-2014 12:40 AM

Slackware Configuration Script - my first script
 
Hi guys, as I said on this thread I am trying to write a script to help me out on fresh slackware installations. Currently it only has two functions:
1 - enabling multilib support
2 - switching from huge to generic kernel

The code can be viewed here. I know, it is silly but it is my first script (don't be too harsh please). This is its initial state, I pretend to add more functions. Any suggestion, help and *GOOD* criticism is appreciated, thanks.

EDIT: version 0.0.2 uploaded

Ser Olmy 01-27-2014 01:27 AM

It's not silly at all. I say any operation that will be done enough times to justify making a script (or something similar), should be automated. Even if it takes you just as long to create the script as it would take to do everything manually, or perhaps even longer, you will have learned something along the way.

I've had a quick look, and here are my comments:
  • You're greeting the user, then telling him/her the script must be run as root. Why not just check for root instead?
  • You could try auto-detecting the Slackware version using the /etc/slackware-version file, and present the user with an option to override.
  • The script makes a "multilib" subdirectory in the current directory. Why not use mktemp -d instead?
  • Several of the operations in the script could fail under various circumstances, such as the lftp download. How about adding some error checking/handling? Even "|| exit" beats having nothing at all.

Richard Cranium 01-27-2014 01:31 AM

  • You might want to use grep, awk, or even cut to get an initial SLACKVER value from /etc/slackware-version. Or cat it to the screen.
  • You can simply use
    Code:

    $(/usr/share/mkinitrd/mkinitrd_command_generator.sh -r)
    to generate the initrd
  • /boot/vmlinuz-generic-* will match more than one kernel on a 32 bit system:
    Code:

    # ls /boot/vmlinuz-generic-*
    /boot/vmlinuz-generic-3.2.29  /boot/vmlinuz-generic-smp-3.2.29-smp


TobiSGD 01-27-2014 02:39 AM

Quote:

Originally Posted by Ser Olmy (Post 5105897)
I've had a quick look, and here are my comments:
  • You could try auto-detecting the Slackware version using the /etc/slackware-version file, and present the user with an option to override.

Quote:

Originally Posted by Richard Cranium
You might want to use grep, awk, or even cut to get an initial SLACKVER value from /etc/slackware-version. Or cat it to the screen.

Might be easier to just source /etc/os-release, that way you will have the version number in the variable VERSION. Of course this would limit the script to be used on Slackware version that have that file, IIRC 14.0 and later.

Richard Cranium 01-27-2014 02:51 AM

Quote:

Originally Posted by TobiSGD (Post 5105922)
Might be easier to just source /etc/os-release, that way you will have the version number in the variable VERSION. Of course this would limit the script to be used on Slackware version that have that file, IIRC 14.0 and later.

I did not know of that file. Cool!

moisespedro 01-27-2014 08:32 AM

Quote:

Originally Posted by Ser Olmy (Post 5105897)
It's not silly at all. I say any operation that will be done enough times to justify making a script (or something similar), should be automated. Even if it takes you just as long to create the script as it would take to do everything manually, or perhaps even longer, you will have learned something along the way.

I've had a quick look, and here are my comments:
  • You're greeting the user, then telling him/her the script must be run as root. Why not just check for root instead?

  • Ok, done that :)
    Quote:

  • You could try auto-detecting the Slackware version using the /etc/slackware-version file, and present the user with an option to override.
  • I am still not really sure about how to do that, I am learning and coding. Basically I am googling the option I want, seeing examples and putting into my script.
    Quote:

  • The script makes a "multilib" subdirectory in the current directory. Why not use mktemp -d instead?
  • What would "mktemp -d" do? Create a temporary directory that would be removed in the end?
    I was thinking about creating a "/tmp/scs/" directory and work there, I am just thinking if it would be the better option and, if so, what would be the best way to implement it.
    Quote:

  • Several of the operations in the script could fail under various circumstances, such as the lftp download. How about adding some error checking/handling? Even "|| exit" beats having nothing at all.
I am pretty sure all Slackware installations have lftp

Quote:

Originally Posted by Richard Cranium (Post 5105902)
  • You might want to use grep, awk, or even cut to get an initial SLACKVER value from /etc/slackware-version. Or cat it to the screen.
  • You can simply use
    Code:

    $(/usr/share/mkinitrd/mkinitrd_command_generator.sh -r)
    to generate the initrd
  • /boot/vmlinuz-generic-* will match more than one kernel on a 32 bit system:
    Code:

    # ls /boot/vmlinuz-generic-*
    /boot/vmlinuz-generic-3.2.29  /boot/vmlinuz-generic-smp-3.2.29-smp


Oh, thanks. Didn't know about "$(/usr/share/mkinitrd/mkinitrd_command_generator.sh -r)" and I didn't know about 32 bits slackware either, thanks for the heads up.

Quote:

Originally Posted by TobiSGD (Post 5105922)
Might be easier to just source /etc/os-release, that way you will have the version number in the variable VERSION. Of course this would limit the script to be used on Slackware version that have that file, IIRC 14.0 and later.

I want to make it compatible with as many slackware versions as posible

TobiSGD 01-27-2014 08:48 AM

Quote:

Originally Posted by moisespedro (Post 5106102)
I am pretty sure all Slackware installations have lftp

What about servers being temporary unavailable or an unreliable network connection (for example with a bad WLAN signal)?

Ser Olmy 01-27-2014 08:50 AM

Quote:

Originally Posted by moisespedro (Post 5106102)
What would "mktemp -d" do? Create a temporary directory that would be removed in the end?

Type man mktemp and all will be revealed.

OK, it creates a unique temporary directory in /tmp, and echos the directory name to stdout. Thus,
Code:

TMP_DIR=$(mktemp -d)
would create the directory and store the name in the TMP_DIR variable. You could then end your script with rm -r $TMP_DIR.
Quote:

Originally Posted by moisespedro (Post 5106102)
I am pretty sure all Slackware installations have lftp

Are you also sure all Slackware systems have Internet connectivity that won't be interrupted, and have ample free diskspace for the downloaded files? If they don't, your script will just go ahead and install whatever has been downloaded. That could have quite unfortunate consequences.

moisespedro 01-27-2014 08:53 AM

Oh, now I get what you are saying. Will look into that, thanks.

genss 01-27-2014 09:25 AM

i dont know about lftp, but wget is persistent by default
so if the connection goes down it will continue when its back
guess it still can fail if there is no space on the disk

moisespedro 01-27-2014 09:36 AM

multilib directory is only 248Mb, what about a message "Please make sure you have at least 1Gb free" or something like that and a md5sum check before installing the packages? If it is correct, it installs them. If not, it tries to run lftp again. It seems reasonable to me.

Ser Olmy 01-27-2014 10:16 AM

The md5 check is a great idea.

When it comes to software design (and make no mistake, you're creating software here), it's usually best to make as few assumptions as possible. In this case, it is quite reasonable to assume that the script is being run on a Slackware system, since that's the whole point of the script, but beyond that you should check all prerequisites, or handle error conditions, or both.

The more you play with the core components of the system, the more careful you'll need to be. And you certainly do some potentially dangerous stuff; among other things, your script replaces glibc. That's an operation you want to make sure is atomic; either glibc gets upgraded or it's left completely untouched. Not "glibc was upgraded, but the other components weren't" or "the upgrade process for glibc started but was interrupted by [whatever], so who knows what the state of glibc is now".

Same goes for direct user input (which should be avoided if at all possible; selection menus are better): Expect characters instead of numbers, the Slackware version spelled out completely as "Slackware 14.1", the wrong version number being entered (you script should double-check and produce an "are you sure" warning), nonsense being entered because something was accidentally pasted or stuck in the keyboard buffer etc. etc.

Of course, some or all of this may be omitted if the script is for personal use only, and you're sure you'll never make any mistakes (yeah, right), but if others may use this particular script, you need to be prepared for anything.

moisespedro 01-27-2014 10:26 AM

Quote:

Originally Posted by Ser Olmy (Post 5106153)
The md5 check is a great idea.

When it comes to software design (and make no mistake, you're creating software here), it's usually best to make as few assumptions as possible. In this case, it is quite reasonable to assume that the script is being run on a Slackware system, since that's the whole point of the script, but beyond that you should check all prerequisites, or handle error conditions, or both.

The more you play with the core components of the system, the more careful you'll need to be. And you certainly do some potentially dangerous stuff; among other things, your script replaces glibc. That's an operation you want to make sure is atomic; either glibc gets upgraded or it's left completely untouched. Not "glibc was upgraded, but the other components weren't" or "the upgrade process for glibc started but was interrupted by [whatever], so who knows what the state of glibc is now".

Same goes for direct user input (which should be avoided if at all possible; selection menus are better): Expect characters instead of numbers, the Slackware version spelled out completely as "Slackware 14.1", the wrong version number being entered (you script should double-check and produce an "are you sure" warning), nonsense being entered because something was accidentally pasted or stuck in the keyboard buffer etc. etc.

Of course, some or all of this may be omitted if the script is for personal use only, and you're sure you'll never make any mistakes (yeah, right), but if others may use this particular script, you need to be prepared for anything.

It was going to be for personal use but I am trying to make it as good as posible (with what I know/can find on Google). I am tyring to make it as user-friendly as posible. And by user-friendly I mean a script that tells you exactly what it is going to do and it easily tells you how to use it. I finally figured out how to detect Slackware version (although I know it won't work on all cases but it is the best that I can do, the way I am doing it, it assumes the user never edited '/etc/slackware-version/' file. And about direct user input I can't think about a better way of getting the slackware version (if it is not correct) other than asking the user.

moisespedro 01-27-2014 11:10 AM

Having trouble with the md5. I am following http://www.slackware.com/~alien/mult.../CHECKSUMS.md5 but the file alienbob provides check for files that doesn't exist. And besides that I don't know how would I implement it :(

EDIT: Actually it only fails while checking
e2ffbeed099cd689a86626eaf050e95d ./slackware64-compat32/HEADER.txt
EDIT2: Already figured out how to workaround it, it has the same md5 as README so the script will duplicate README as Header.txt. Now I just need to know how to implement it.

moisespedro 01-27-2014 11:46 AM

Ok, you can check out the new code here. I've change a few things and there is a few commentaries on it, thanks.


All times are GMT -5. The time now is 01:08 AM.