Привет всем! Моя специализация - C, C++, PHP.
Bash - писал простенькие скрипты для себя. Дали тестовое задание -
написать простой настройщик для меню grub на bash.
Это мой первый большой скрипт на bash, я хочу, чтобы вы раскритиковали и
оценили :)
Все писал сам и с нуля. Скрипт на english.
Скрипт в аттаче или по ссылке nemereno.com/upload/grubconf
Скрипт НЕ тронет ваш реальный конфиг груба!!! Так что запускайте смело!
Заранее спасибо!
#!/bin/bash
# print color
function c {
echo -n -e "\033[$1m"
}
# string to lower
function toLower {
echo $1 | tr "[:upper:]" "[:lower:]"
}
# is it number?
function isnum {
if [ $1 -eq $1 2> /dev/null ]; then
return 0;
else
return 1;
fi
}
# is it bool answer?
function isbool {
x=`toLower $1`
if [ "$x" == "1" ] || [ "$x" == "0" ] || [ "$x" == "yes" ] || [ "$x" ==
"no" ]; then
return 0
else
return 1
fi
}
# reread answer on error
function reread {
c 37
echo "Error: Invalid answer"
echo -n "Your answer: "
c 0
read a
}
# question function
function q {
c 36
echo -n -e $1
def=$2
t=0;
if [ ! -z $3 ]; then
t=$3
fi
if [ "$t" == "1" ]; then
if [ "$def" == "0" ]; then
def="no"
fi
if [ "$def" == "1" ]; then
def="yes"
fi
fi
c 33
c 1
if [ ! -z "$def" ]; then
echo -n " [$def]"
else
echo -n " []"
fi
c 36
echo -n ": "
c 0
read a
if [ "$a" == "" ]; then
a=$def
fi
case $t in
0)
;;
1)
until (isbool $a); do
c 37
echo "Error: Answer must be yes/no"
echo -n "Your answer: ";
c 0
read a
done
a=`toLower $a`
if [ "$a" == "yes" ]; then
a=1
fi
if [ "$a" == "no" ]; then
a=0
fi
;;
2)
until (isnum $a); do
c 37
echo "Error: Answer must be a number"
echo -n "Your answer: ";
c 0
read a
done
;;
esac
}
c 35
echo Welcome to GRUB config utility, made by www.nemereno.com group
echo Grub - is a package, that is need for booting up your system. Without GRUB
you cannot start your system
echo "Some questions have default answers in the [], if you don't know the
answer - simply press enter"
echo
c 34
########
q "Would you like to try to configure grub automatically?" 0 1
if [ "$a" == "1" ]; then
echo "Trying to autoconfigure... Starting update-grub (fake)"
#update-grub
exit 0
fi
q "Enter number of menu entries" 1 2
entriesc=$a
for i in `seq 1 $entriesc`;do
c 38
echo "Configuring entry #$i"
q "Type of OS for entry #$i: \n1 - Linux\n2 - Mac OS\n3 - M\$
Mustdie\nYour choice" 1 2
until [ ! "$a" -ge 4 ] && [ ! "$a" -le 0 ];do reread; done
OStypes[$i]=$a
if [ "$a" == "1" ] || [ "$a" == "2" ]; then
def="`uname -o` `uname -r`"
else
def="M\$ Mustdie"
fi
q "Title for menu entry #$i" "$def" 0
titles[$i]=$a
q "Root device,partition" "0,0" 0
until (echo $a | grep -q '[0-9],[0-9]');do reread; done
roots[$i]=$a
let letter=${a%,*}+97
let num=${a#*,}+1
case ${OStypes[$i]} in
1)
q "Kernel String" "/boot/vmlinuz-`uname -r`
root=/dev/sd`echo $letter | awk '{printf("%c",$1)}'`$num ro quiet splash"
kernel[$i]=$a
q "Initrd string" "initrd.img-`uname -r`" 0
initrd[$i]=$a
;;
2|3)
#
;;
esac
done
echo
q "Default entry number (will boot if no choice)" "0" 2
default=$a
q "Timeout (sec) for menu" "3" 2
timeout=$a
q "Hide the menu by default? (press ESC to see the menu)" 0 1
hiddenmenu=$a
data="# this file is generated by grubconf utility, made by
www.nemereno.com"$'\n\n'
data+="default $default"$'\n'
data+="timeout $timeout"$'\n'
if [ "$hiddenmenu" == 1 ];then
data+="hiddenmenu"$'\n'
fi
data+=$'\n'
data+="# menu list"$'\n'
for i in `seq 1 $entriesc`;do
data+="title ${titles[$i]}"$'\n'
data+="root hd(${roots[$i]})"$'\n'
case ${OStypes[$i]} in
1)
data+="kernel ${kernel[$i]}"$'\n'
data+="initrd ${initrd[$i]}"$'\n'
;;
2|3)
data+="makeactive"$'\n'
data+="chainloader +1"$'\n'
;;
esac
data+=$'\n'
done
echo
q "Configuration done. What to do next?\n1 - Write grub config to the
/boot/grub/menu.lst (root only)\n2 - Write grub config to tmp file
(/tmp/menu.lst)\n3 - print grub config in less\nYour choice" 2 2
until [ ! "$a" -ge 4 ] && [ ! "$a" -le 0 ];do reread; done
case $a in
1)
echo "$data" > /tmp/menu.lst.real
;;
2)
echo "$data" > /tmp/menu.lst
;;
3)
echo "$data" | less
;;
esac
c 38
echo "Thank you for using our utility!"
c 0
echo--
ubuntu-ru mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-ru