You are not logged in.

#1 2008-08-10 11:10:13

hunterthomson
Member
Registered: 2008-06-22
Posts: 794
Website

What do you think of my Bash Script? What about the error checking?

Well what do you think of this Bash script big_smile
It works smile
I gave it some problems (i.e. unpluged the ethernet, messed up the URL's, uninstaled some programs...) to see if would report errors and stop or just keep going... But it did smile

I just kind of thought up a way to do some error checking with the commands that I know.....
What is a better way to do error checking?
What do you think I should add/Do to this script?


#!/bin/bash
#
# Shell script to make a USB Tumb Drive for Flashing BIOS on a Lenovo Ideapad Y510. 
# This script needs to be owned and run as ROOT with the "sudo command"
# i.e.  sudo usbbiosflasher
#
# If you have anyideas send me a PM on ubuntufourms.org  my user name is HunterThomson 
# Name/Rename this script usbbiosflasher and save it to the ~/home directory.
# Then run the command-  chown root:root usbbiosflasher
# Then run the command-  chmod 755 usbbiosflasher
# Then copy the script to the directory /usr/bin.
# Run this comand to do that- sudo cp ~/usbbiosflasher /usr/bin  
#
# You also must have the program "mbr" installed
# You can install the mbr program by running this comand in the shell on Ubuntu
# sudo apt-get install mbr
# In Arch Linux you have to get it from Aur
#
# First you will need to know a few things...
#
# You will also need to know the Mount Point i.e. /media/disk and the /dev path i.e. /dev/sdb1.
# You can find these by using the df -T comand. 
# Run   df -T  in the shell. Then plug in the USB Thumb Drive and run the  df -T  comand agin.
# The new listing is the USB Thumb Dirve.
# Also check to make sure the File System tipe is vFAT or FAT16 or FAT32. 
# If it is not use gparted to format it to FAT32.
# I am farly certen that all USB Thumb drives come formated with FAT file system out of the BOX.
# You may want to fromat it anyway just to make sure.
#
#
#
#

echo "Interactive Shell Script to Make a USB Thumb Drive \for Flashing BIOS On a Lenovo Ideapad Y510"
echo ""
echo "You will need to have the program   mbr   installed"
echo "If you are on Ubuntu Linux you can retreve it form the repositories"
echo "If you are on Arch Linux you will need to get it from the Aur repository"
echo "Open anuther shell and \do that now..."
echo ""

verify="n"
while [ "$verify" != y ]
do
    printf "Do you have mbr installed... yes or no?"
    read AN1
    echo ""
    printf "You answered... $AN1 I have installed mbr. Is this correct... y or n?"
    read verify
done

echo ""

if [ "$AN1" == "no" ]
then
    echo "Install mbr now. Then run this script agin"
    exit
else
    echo "contunuing script"
fi

echo ""

# The next comand will make a directory to put needed files into. Note this file and everything init will be owned by root.
mkdir ~/usbbiosfiles &&  check1="yes"

if [ "$check1" = "yes" ]
then
    echo "Made directory usbbiosfiles... OK"
else
    echo "Could not \make directory usbbiosfiles"
    echo "look above \for \info"
    echo "Fix the problem and run this scrip agin"
    exit
fi

# The next two comands will get the FreeDOS file and the .ROM file.
cd ~/usbbiosfiles && checka="yes"

if [ "$checka" = "yes" ]
then
    echo "Changing to the usbbiosfiles directory... OK"
else
    echo "Could not Change to the usbbiosfiles directory"
    echo "look above \for \info"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi


wget  "http://www.fdos.org/bootdisks/autogen/FDOEM.144.gz" && check2="yes"

if [ "$check2" = "yes" ]
then
    echo "Download of FreeDOS... OK"
else
    echo "Could not Download FreeDOS"
    echo "look above \for \info"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

wget "http://ubuntuforums.org/attachment.php?attachmentid=78460&d=1216648756" && check3="yes"

if [ "$check3" = "yes" ]
then
    echo "Download of the BIOS.ROM \file... OK"
else
    echo "Could not Downlad the BIOS.ROM \file"
    echo "look above \for \info"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

# The next comand will name the .ROM file to the right name.
mv ~/usbbiosfiles/attachment.php?attachmentid=78460\&d=1216648756 ~/usbbiosfiles/06CN29WW.bios.update.tar.bz2 && check4="yes"

if [ "$check4" = "yes" ]
then
    echo "Renameing of the BIOS.ROM \file... OK"
else
    echo "Could not rename the BIOS.ROM \file"
    echo "look above \for \info"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

echo ""

# The next two comands set the variables. DEVX for the path i.e. /dev/xxx and MOUNTX for the mount point i.e. /media/xxx
verify="n"
while [ "$verify" != y ]
do
    echo "You will need to know the Mount Point and the dev Path. You will also need to \make sure the File System \type is vFAT, FAT16 or FAT32." 
    echo ""
    echo "With the USB Thumb Drive unpluged, Open another shell and run the comand  df -T  Then plug \in the USB Thumb Drive and run the comand df -T one \more time. The new device listed is the USB Thumb Drive. Note the Mount Point and The dev Path and the File system Type i.e. vFAT... If the File System \type is not vFAT, FAT16 or FAT32 you will need to fromat it with gparted. You may want to format the USB Thumb Drive anyway just to \make sure. In any \case delete all files and directorys on the USB drive before you go any ferther with this program."
    echo ""
    printf "Enter the dev path the USB Thumb Drive is at?"
    read DEVX
    echo ""
    echo "Are you sure $DEVX is the dev path of the USB Thumb Drive... y or n?"
    read verify
done 

echo ""

verify="n"
while [ "$verify" != y ]
do
    printf "What is the Mount Point of the USB Thumb Drive?"
    read MOUNTX
    echo ""
    echo "Are you sure $MOUNTX is the Mount Point of the USB Drive... y or n?"
    read verify
done 

echo ""

install-mbr --enable A1 --partition 1 --force --timeout 0 $DEVX && check5="yes"

if [ "$check5" = "yes" ]
then
    echo "Installing MBR on USB Thumb Dirve... OK"
else
    echo "Could not install MBR on USB Thumb Drive"
    echo "look above \for \info"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

tar xjf ~/usbbiosfiles/*.tar.bz2 && check7="yes"

if [ "$check7" = "yes" ]
then
    echo "Unpacking BIOS.ROM file... OK"
else
    echo "Could not unpack BIOS.ROM file"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

gunzip ~/usbbiosfiles/FDOEM.144.gz && check8="yes"

if [ "$check8" = "yes" ]
then
    echo "Unpacking FreeDOS files... OK"
else
    echo "Could not unpack FreeDOS files"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

mkdir ~/usbbiosfiles/fdoem144 && check9="yes"

if [ "$check9" = "yes" ]
then
    echo "Made directory fdoem144 in direcoty usbbiosfiles... OK"
    echo ""
    echo "Going to \sleep \for 5secs"
else
    echo "Could not make directory fdoem144 in usbbiosfiles directory"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

modprobe loop && sleep 5 && check0="yes"

if [ "$check0" = "yes" ]
then
    echo "Modprobeing loop... OK"
else
    echo "Could not \modprobe loop"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

mount -o loop ~/usbbiosfiles/FDOEM.144 ~/usbbiosfiles/fdoem144 && check10="yes"

if [ "$check10" = "yes" ]
then
    echo "Mounting FreeDOS on the fdoem144 directory... OK"
else
    echo "Could not \mount FreeDOS on the fdoem144 directory"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

cp ~/usbbiosfiles/fdoem144/* $MOUNTX && check11="yes"

if [ "$check11" = "yes" ]
then
    echo "Copying FreeDOS files to $MOUNTX... OK"
else
    echo "Could not copy FreeDOS files to $MOUNTX"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

cp ~/usbbiosfiles/*.ROM $MOUNTX && check12="yes"

if [ "$check12" = "yes" ]
then
    echo "Copying BIOS.ROM files to $MOUNTX... OK"
else
    echo "Could not copy BIOS.ROM files to $MOUNTX"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

sync && check13="yes"

if [ "$check13" = "yes" ]
then
    echo "Runing the syncing command... OK"
else
    echo "Could not run the syncing command"
    echo "look above \for \info"
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this scrip agin"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    rm -r ~/usbbiosfiles
    exit
fi

umount ~/usbbiosfiles/fdoem144 && check14="yes"

if [ "$check14" = "yes" ]
then
    echo "Unmounting of FreeDOS... OK"
else
    echo "Could not unmount FreeDOS"
    echo "Look above for errors or problems reported and fix the problem"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this script agin"
    rm -r ~/usbbiosfiles
    exit
fi

verify="n"
while [ "$verify" != y ]
do
    printf "Do you see any errors... yes or no?"
    read AN2
    echo ""
    printf "You answered... $AN2 to errors. Is this correct... y or n?"
    read verify
done

echo ""

if [ "$AN2" == "yes" ]
then
    echo "User Repoted... Error"
    echo "Look above for errors or problems reported and fix the problem"
    echo ""
    echo "removeing directory usbbiosfiles..." 
    echo ""
    echo "Reformat the USB Stick to FAT32 with gparted"
    echo "Fix the problem and run this script agin"
    rm -r ~/usbbiosfiles
    exit
else
    echo "Success"
    echo "I did a lot of error checking too and didnt find anything"
    echo ""
    echo "Go get a pen and paper to write down these instructions"

printf "Then hit the Enter to continue"
read WAIT
echo ""
echo "Now leave the USB Thumb Drive pluged into your computer and Reboot. When the Lenovo Logo POST screen appears hit F2 to enter the CMOS setup utility. Go over to BOOT tab and go down to HardDrive \(Not Boot Order) \then \select the USB Thumb Drive as the 1st hard drve. Then F10 and yes to save changes. Your compter will reboot agin. Then when the Lenovo Logo POST Screen appers on reboot hit F4 to enter the BIOS FLASHING program. The USB Thumb Drive will be seen as the C drive \in the list on the Left, Select it. Then \select the .ROM \file \in the list on the Right and start the BIOS FLASH. \(NOTE Your hart may stop beating... This is normal) Pray to any God you know of and your computer should restart just like normal. Hit F2 and the BIOS will now stay it is 06CN29WW. You will need to \set the boot order to the way you like it and other things \if you need to because they have been changed to the default."
fi
echo ""
echo "End of script"

Last edited by hunterthomson (2008-08-10 11:17:47)


OpenBSD-current Thinkpad X230, i7-3520M, 16GB CL9 Kingston, Samsung 830 256GB
Contributor: linux-grsec

Offline

#2 2008-08-10 11:33:28

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,223
Website

Re: What do you think of my Bash Script? What about the error checking?

Personally.....  (this is just how I would have written it - if it works, then it's good enough for me though smile)

I would change this whole block:

verify="n"
while [ "$verify" != y ]
do
    printf "Do you have mbr installed... yes or no?"
    read AN1
    echo ""
    printf "You answered... $AN1 I have installed mbr. Is this correct... y or n?"
    read verify
done

echo ""

if [ "$AN1" == "no" ]
then
    echo "Install mbr now. Then run this script agin"
    exit
else
    echo "contunuing script"
fi

To this much shorter code:

MBR='/usr/bin/install-mbr'   # Or where ever you expect it to be
if [ ! -x $MBR ] ; then
    echo "mbr doesn't appear to be installed."
    echo "If it is installed, check it's location, make sure it's executable and then make sure the MBR variable in this script is correct"
    exit 1
fi

I wouldn't have used the checkXX variables for each stage:

mkdir ~/usbbiosfiles
if [ $? != 0 ] ; then
    #failed
    echo "Could not \make directory usbbiosfiles"
    echo "look above \for \info"
    echo "Fix the problem and run this scrip agin"
    exit 1
else
    echo "Made directory usbbiosfiles... OK"
fi

There is an issue with the way you do your verifications - the user can never get out unless they answer 'y' or hit CTRL+C. Something like this gives them options:

verify="n"
while [ "$verify" != "y" && "$verify" != "n" ]; do
    echo "You need to answer 'y'es or 'n'o" 
    read verify
    echo $verify | tr "[:upper:]" "[:lower:]"    # This converts the answer to lowercase so replies entered in upper case will still work
done
if [ $verify != 'y' ] ; then
   exit 1
fi

One last thing I try to do in scripts... Declare all your binaries as variables at the start of the program, then execute the binary program by using the variable. For example:

# Binaries
TAR='/bin/tar'
CP='/bin/cp'
CHMOD='/bin/chmod'

# Execute tar and chmod the created file
$TAR cvzf /tmp/tarfile.tar.gz /etc/*.conf
$CHMOD 400 /etc/*.conf

This way, it's easy to change the path in future without having to hunt through the script if the paths change, and it also ensures you're calling the programs using the full paths to make sure you're not executing some strange variant or alias that someone has setup. If I use `chmod` 30 times in a script, and the path changes in the future or on a different system (`chmod` is a bad example cause it's highly unlikely to change, but you know what I mean), then all you need to do is update the variable at the start of the script, and it all works again without having to script-hunt and change it 30 times.

Offline

#3 2008-08-10 12:15:59

hunterthomson
Member
Registered: 2008-06-22
Posts: 794
Website

Re: What do you think of my Bash Script? What about the error checking?

Thank you for the good info big_smile

I realy liked this...However, it dosn't seem to work... it repots that I don't have it installed but I do and at /usr/bin/install-mbr...

MBR='/usr/bin/install-mbr'   # Or where ever you expect it to be
if [ ! -x $MBRX] ; then

I love this... I wanted to find out how to do that... I meen I kept reading "this reports true if bla" I just didn't know how to use that info.

mkdir ~/usbbiosfiles
if [ $? != 0 ]

------------------------------------------
I was thinking I could add more "$verify" != "blabla"  I just didn't know if it would mess up the script....I think I will add a "q" for exit 1

So, in your code it will exit if you enter "n"
------------------------------------------
I never thougt of doing this....

# Binaries
TAR='/bin/tar'
CP='/bin/cp'
CHMOD='/bin/chmod'

# Execute tar and chmod the created file
$TAR cvzf /tmp/tarfile.tar.gz /etc/*.conf
$CHMOD 400 /etc/*.conf

Last edited by hunterthomson (2008-08-10 12:42:16)


OpenBSD-current Thinkpad X230, i7-3520M, 16GB CL9 Kingston, Samsung 830 256GB
Contributor: linux-grsec

Offline

#4 2008-08-10 22:41:04

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,223
Website

Re: What do you think of my Bash Script? What about the error checking?

MBR='/usr/bin/install-mbr'   # Or where ever you expect it to be
if [ ! -x $MBRX] ; then

Your if statement has the wrong variable - you've got an extra X on the end smile

Offline

#5 2008-08-10 23:44:56

hunterthomson
Member
Registered: 2008-06-22
Posts: 794
Website

Re: What do you think of my Bash Script? What about the error checking?

Nope that was just a typo in the email. I copyed your code strate in to my /usr/bin/Tscript and ran it but it keeps telling me it is not installed?

I want the varify to loop untill the person gives the answer they intended to give. If they say "no I was wrong I that is not the corect Mount Point" then it will ask them the question agin untill they give the ansor they intend to give. But to just exit the script if they didn't give the answer they wanted to give is not a good idea. The scrip dosn't have to end nore do I think the person would want it to end. They just want to give a difrent answer. However, I would be nice to give them a Quit option. But I can not get the code to work I keep getting...

/usr/bin/Tscript: line 6: [: missing `]'

or if I change it around

/usr/bin/Tscript: line 6: : "n"  unary

It would be nice to have a "Not sure" or just report back some infor on the Mount Point they geve. Like Size or List of file or File System type. Something to give them a red flag that they are going to mess up there computer. That is the main reson I don't have it format the USB Drive. If someone entered the wrong /dev/sda I would not want to format there harddrive.... Installing  a new MBR can be recovered. I could maybe have it make a copy of the MBR apon the request of the user.

I was also trying to have it read the USB drive at the end and compare the (ls $MOUNTX) to a list of the file names it shold have on it but I can't get it to work. I guess because ls reports back a lot more infor then just the file name. Do you have any ideas on how to varifi the contents of the USB Thumb Drive ???

I love the new error checking smile that is in the code now. I also like the form  if [ bob=tom] ; then that is in the code now too. I also put the change uper to lower in there.

Last edited by hunterthomson (2008-08-10 23:50:52)


OpenBSD-current Thinkpad X230, i7-3520M, 16GB CL9 Kingston, Samsung 830 256GB
Contributor: linux-grsec

Offline

#6 2008-08-14 10:32:43

kumyco
Member
From: somewhere
Registered: 2008-06-23
Posts: 153
Website

Re: What do you think of my Bash Script? What about the error checking?

you need a space ' ' after ] as it's an arg to the `[` command
so you should have

...-x $MBR ];...

Offline

Board footer

Powered by FluxBB