Click to See Complete Forum and Search --> : Linux equivalent to a windows batch file?
Nameless
09-09-2001, 05:46 PM
Hello fellow linux users!
my question:
Is there an equivalent to a windows batch file (*.bat) in linux? I used to have alot of fun with those in windows and they make simple tasks simpler.
For those of you who dont know what a batchi file is it is a file that you can enter dos commands in it and when you run the file it executes the command.
Thanks
bdg1983
09-09-2001, 05:55 PM
SCRIPTS
Linux uses scripts for just about everything.
http://www.linuxnewbie.org/nhf/intel/programming/index.html
First line starts with
#!/bin/bash
or
#!/bin/sh
Add your commands and make the script executable.
bdg1983
09-09-2001, 05:59 PM
Here's a sample of a script that I wrote to compile the kernel. I have another to cleanup, backup libraries etc. prior to compiling the kernel. And yet another to modify the initrd (initial ramdisk).
#!/bin/bash
# Script to Compile the 2.4 Series Kernel
# #########################################
# #
# compile-kernel new old #
# #
# Commandline syntax is 'compile-kernel 2.4.9 2.4.2 #
# #
# #########################################
if [ -z "$1" ] ; then
echo 'Type compile-kernel (new)2.4.9 (old)2.2.14'
exit 1
fi
if [ -z "$2" ] ; then
echo 'Type compile-kernel (new)2.4.9 (old)2.2.14'
exit 1
fi
if [ -d /lib/modules/$2 ] ; then mv /lib/modules/$2 /lib/modules/old-$2
else
echo 'Cannot find /lib/modules/'$2
fi
cd /usr/src
if [ -d linux ] ; then rm linux
else
echo 'Cannot find symlink linux'
fi
if [ -f linux-$1.tar.gz ] ; then tar zxvpf linux-$1.tar.gz
else
echo 'Cannot find kernel source (linux-'$1'.tar.gz in /usr/src'
exit 1
fi
if [ -d linux ] ; then mv linux linux-$1
else
echo 'Cannot move linux to linux-'$1
exit 1
fi
if [ -d linux-$1 ] ; then ln -s linux-$1 linux
else
echo 'Cannot link linux to linux-'$1
exit 1
fi
chown -R root.root /usr/src/linux-$1
cd linux
echo Building a new kernel . . .
es=$?
make mrproper 2>&1 | tee mrproper.out
if [ $es -gt 0 ] ; then
echo 'make mrproper failed'
exit es
fi
make xconfig
if [ $es -gt 0 ] ; then
echo 'make xconfig failed'
exit es
fi
make dep 2>&1 | tee dep.out
if [ $? -gt 0 ] ; then
echo 'make dep failed'
exit 1
fi
make clean 2>&1 | tee clean.out
if [ $? -gt 0 ] ; then
echo 'make clean failed'
exit 1
fi
make bzImage 2>&1 | tee bzImage.out
if [ $? -gt 0 ] ; then
echo 'make bzImage failed'
exit 1
fi
make modules 2>&1 | tee modules.out
if [ $? -gt 0 ] ; then
echo 'make modules failed'
exit 1
fi
make modules_install 2>&1 | tee modules_install.out
if [ $? -gt 0 ] ; then
echo 'make modules_install failed'
exit 1
fi
if [ -d /lib/modules/$1 ] ; then mv /lib/modules/old-$2 /lib/modules/$2
else
echo 'Cannot move /lib/modules/old-'$2' to /lib/modules/'$2
exit 1
fi
if [ -f arch/i386/boot/bzImage ] ; then cp -f arch/i386/boot/bzImage /boot/vmlinuz-$1-modular
else
echo 'Cannot copy arch/i386/boot/bzImage to /boot/vmlinuz-'$1'-modular'
exit 1
fi
if [ -f System.map ] ; then cp -f System.map /boot/System.map-$1
else
echo 'Cannot copy System.map to /boot/System.map-'$1
exit 1
fi
if [ -f /usr/src/linux-$1/.config ] ; then cp -f /usr/src/linux-$1/.config /mdw/.config-$1-backup
else
echo 'Cannot copy /usr/src/linux-'$1'/.config to /mdw/.config-'$1'-backup'
exit 1
fi
depmod -a $1
if [ $? -gt 0 ] ; then
echo 'depmod -a' $1 'failed'
fi
echo 'End of Kernel '$1' Compile Process'
echo 'Create new /boot/initrd-'$1'.gz if necessary'
echo 'Add new kernel section to /boot/grub/menu.lst OR /etc/lilo.conf (rerun /sbin/lilo)'
strings /boot/vmlinuz-$1-modular | grep 2001
Nameless
09-09-2001, 07:10 PM
Thankyou!
I shall have much fun with this newly acquired knowledge....
Thanks Again!
Fondor
09-10-2001, 01:28 AM
watts, clean up your code, man ;) Btw can you use C or C++ like scripts so they perform system tasks like your example? I figure that you could, but just checking before I waste my time. Thanks.
bdg1983
09-10-2001, 01:40 AM
Never have learned to program in C though I certainly would like to one day.
If you have any suggestions to improve on my script, then please post them.
All I wanted was a script to take care of all the kernel compiling steps and it seems to work fine for what I want. There's always room for improvement though.
xhadow
09-10-2001, 09:57 AM
Originally posted by Fondor:
<STRONG>watts, clean up your code, man ;) Btw can you use C or C++ like scripts so they perform system tasks like your example? I figure that you could, but just checking before I waste my time. Thanks.</STRONG>
yes, you could, but you have to use csh (C Shell), instead of bash (Bourne Again Shell)
[ 10 September 2001: Message edited by: xhadow ]