LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-21-2010, 10:13 AM   #1
Potatos
LQ Newbie
 
Registered: Apr 2010
Distribution: Fedora 13
Posts: 20

Rep: Reputation: 1
Question If..else..fi shell script statements in Init


I have made some modifications to the init script that runs when my PC boots to take me to a /bin/sh shell so I can run some custom scripts. When I exit from here the init statement should mount the root file system and switch_root to it.

This all works correctly however the same init statement will be run on two types of pc's and the address of the root fs is different between them. On one PC it sits on /dev/sda1 and on the other /dev/hda1. To select the appropriate addresses I used dmesg to find out if it was the i5 PC and if/else statements to select the right hard drive location.

When I try use a if/else statement in the init script it ends up preforming both the if and the else code. Is there any reason the if else statements wouldn't work the exact same in the init script (it's all shell scripts isn't it?

Here is the if else statement from the init script.

Code:
echo Attempting to switch root

mkdir /mnt

# Check if the PC is an i5 otherwise its an AMD
dmesg | grep i5
if [ $? -ne 1 ]; then # It's a i5
        echo "i5 PC Detected"
        mount /dev/hda1 /mnt
else # It's a AMD
        echo "AMD PC Detected"
        mount /dev/sda1 /mnt
fi
exec switch_root /mnt /sbin/init
Thanks
 
Old 05-21-2010, 10:30 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Indeed that's a weird behavior and there is no apparent reason for that to happen. What if you slightly change the syntax with this?
Code:
if dmesg | grep -q i5
then
  echo "i5 PC Detected"
  mount /dev/hda1 /mnt
else
  echo "AMD PC Detected"
  mount /dev/sda1 /mnt
fi
 
Old 05-21-2010, 11:29 AM   #3
Potatos
LQ Newbie
 
Registered: Apr 2010
Distribution: Fedora 13
Posts: 20

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by colucix View Post
Indeed that's a weird behavior and there is no apparent reason for that to happen. What if you slightly change the syntax with this?
Thanks for the suggestion but it still seems to be a problem, getting the following:
Code:
Attempting to switch root
i5 PC Detected
kjournald starting. Commit interval 5 seconds.
EXT3 FS on hda1, internal journal
EXT3-fs: mounted filesystem with ordered data mode.
AMD PC Detected
switch_root: can't execute '/sbin/init': No such file or directory
Kernel panic - not syncing: Attempted to kill init!
Probably worth noting when I run any of my "dmesg | grep i5" statements in my custom scripts it spits back the lines where i5 is run, when running that statement in init nothing is returned. Is this just because i'm not in a /bin/sh shell?

Last edited by Potatos; 05-21-2010 at 11:31 AM.
 
Old 05-21-2010, 11:35 AM   #4
Potatos
LQ Newbie
 
Registered: Apr 2010
Distribution: Fedora 13
Posts: 20

Original Poster
Rep: Reputation: 1
Here is the rest of the init script in case it helps.

Quote:
#!/bin/nash

mount -t proc /proc /proc
setquiet
echo Mounting proc filesystem
echo Mounting sysfs filesystem
mount -t sysfs /sys /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs /dev /dev
mkdir /dev/pts
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
mkdir /dev/shm
mkdir /dev/mapper
echo Creating initial device nodes
mknod /dev/null c 1 3
<A lot of mknod removed for space>
mknod /dev/sr0 b 11 0
echo Setting up hotplug.
hotplug
echo Creating block device nodes.
mkblkdevs
echo "Loading ehci-hcd.ko module"
insmod /lib/ehci-hcd.ko
echo "Loading ohci-hcd.ko module"
insmod /lib/ohci-hcd.ko
echo "Loading uhci-hcd.ko module"
insmod /lib/uhci-hcd.ko
mount -t usbfs /proc/bus/usb /proc/bus/usb
echo "Loading scsi_mod.ko module"
insmod /lib/scsi_mod.ko
echo "Loading sd_mod.ko module"
insmod /lib/sd_mod.ko
echo "Loading libata.ko module"
insmod /lib/libata.ko
echo "Loading sata_nv.ko module"
insmod /lib/sata_nv.ko
echo "Loading dm-mod.ko module"
insmod /lib/dm-mod.ko
echo "Loading dm-log.ko module"
insmod /lib/dm-log.ko
echo "Loading dm-mirror.ko module"
insmod /lib/dm-mirror.ko
echo "Loading dm-zero.ko module"
insmod /lib/dm-zero.ko
echo "Loading dm-snapshot.ko module"
insmod /lib/dm-snapshot.ko
echo "Loading dm-mem-cache.ko module"
insmod /lib/dm-mem-cache.ko
echo "Loading dm-region_hash.ko module"
insmod /lib/dm-region_hash.ko
echo "Loading dm-message.ko module"
insmod /lib/dm-message.ko
echo "Loading dm-raid45.ko module"
insmod /lib/dm-raid45.ko
echo "Loading fat module"
insmod /lib/fat.ko
echo "Loading vfat module"
insmod /lib/vfat.ko
echo "Loading cdrom module"
insmod /lib/cdrom.ko
echo "Loading sr module"
insmod /lib/sr_mod.ko
echo "Loading ide cd module"
insmod /lib/ide-cd.ko
echo "Loading usb storage module"
insmod /lib/usb-storage.ko
echo Waiting for driver initialization.
stabilized --hash --interval 1000 /proc/scsi/scsi
mkblkdevs
echo Scanning and configuring dmraid supported devices

/bin/sh

echo Attempting to switch root

mkdir /mnt

# Check if the PC is an i5 otherwise its a AMD
if dmesg | grep -q i5
then
echo "i5 PC Detected"
mount /dev/hda1 /mnt
else
echo "AMD PC Detected"
mount /dev/sda1 /mnt
fi

exec switch_root /mnt /sbin/init
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through a shell script with sql statements novice82 Linux - Newbie 7 10-08-2009 10:02 PM
success and failure statements in /etc/init.d/functions on Fedora wmakowski Programming 3 03-11-2009 09:35 PM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 01:58 AM
/etc/init.d/cyrus Shell Script 'Permission denied' ServerStorm Linux - Software 3 01-20-2005 12:04 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 03:41 AM


All times are GMT -5. The time now is 06:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration