You are not logged in.
Hello everyone,
So I'm learning how to Bash script so I can improve my AUR packages, and also make some scripts for my personal use.
I've been working on a basic script that completely cleans your kernel source tree while saving your config file.
I was wondering if this is good and up to today's standards (Meaning checking existence of files, and checking return status for previous command):
#!/bin/bash
mv .config config
if [ -e "config" ] ; then
echo "Configuration has been backed up successfully."
else
echo "Error renaming, quitting program."
exit
fi
echo "Cleaning your kernel source tree!"
make distclean
if [ $? -eq 0 ]; then
echo "Your kernel source tree was clean successfully."
else
echo "Error cleaning your kernel source."
fi
mv config .config
if [ -e ".config" ] ; then
echo "Configuration has been installed successfully."
else
echo "Error renaming, quitting program."
exit
fi
Offline
What happens if you accidentally run it in your home directory? You probably don't want to be moving your .config folder around like this, even if it doesn't get hurt. Also, what happens if there's a pre-existing "config" file or folder? You should probably inform the user to deal with it, and bail out.
I would also consider using more verbosity, eg in the calls to "mv", and make the echo statements more descriptive and maybe use some colour.
In summary, if it were me, a first draft before playing with it more might look a bit like this:
#!/bin/bash
set -e
RED="\[\033[31m\]"
GREEN="\[033[32m\]"
BLUE="\[033[34m\]"
RESET_COLOURS="\[\033[0m\]"
CONFIG_FILE=".config"
BACKUP_FILE="config"
if [[ -f "$CONFIG_FILE" && ! -f "$BACKUP_FILE" ]]; then
printf "Backing up existing configuration: $BLUE"
mv -v "$CONFIG_FILE" "$BACKUP_FILE"
printf "${RESET_COLOURS}\n"
else
echo "Existing configuration not found or backup already exists, nothing to do."
exit
fi
printf "Cleaning kernel source tree in $(pwd) ... "
make distclean
if [ $? -eq 0 ]; then
printf "${GREEN}Success!${RESET_COLOURS}\n"
else
printf "${RED}Failed!${RESET_COLOURS}\n"
fi
if [[ -f "$BACKUP_FILE" && ! -f "$CONFIG_FILE" ]] ; then
printf "Restoring pre-existing configuration: $BLUE"
mv -v "$BACKUP_FILE" "$CONFIG_FILE"
printf "${RESET_COLOURS}\n"
else
echo "Backed up configuration not found or pre-existing configuration already there, nothing to do."
exit
fi
Last edited by /dev/zero (2012-01-13 23:03:10)
Offline
Thanks for the help, but remember though that this is a personal script and it's not mean't to be redistributed. So I know exactly when and where I'm running it. That's why I didn't include some checks, even though as time goes on I probably would end up adding them anyways.
Offline
Thanks for the help, but remember though that this is a personal script and it's not mean't to be redistributed. So I know exactly when and where I'm running it. That's why I didn't include some checks, even though as time goes on I probably would end up adding them anyways.
Well, I wasn't telling you how to do it. Do it how you want.
I've just made enough mistakes over the years that I know I should treat myself as just another stupid user who needs checks in place. I'm constantly switching between different terminals which each have ssh sessions open to different computers at different times, so it's easy to lose track.
All you have to do is run the script when you're a bit tired and you'll see why it might make sense to be a bit more cautious.
Edit: I was going to say something about you being ungracious, but okay, you did say thank you.
Last edited by /dev/zero (2012-01-14 04:22:26)
Offline
I wasn't trying to offend you, and I also did say that since I'm still learning, I just wrote what I could to get the job done, without any checks as well. Eventually though when I become better at bash scripting, I will add more checks and take your advice into consideration. I also love the color codes .
Last edited by Cows (2012-01-14 04:52:24)
Offline
Another way to handle this this, with error handling is like this
#!/bin/bash
echo "Making backup of config"
mv .config config || { echo "Error renaming, quitting program."; exit 1; }
You can do the same with all the other testing you do.
Also, don't forget that a non-zero exit status indicates failure (by convention) so if you are exiting because of an error, you should do so with non-zero.
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
mv -n
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline