Linux n00b needs help!

Where Penguins and Daemons chill together in the warmth of the Sun.

Moderators: SecretSquirrel, notfred

Linux n00b needs help!

Postposted on Fri Aug 01, 2008 6:39 am

I'm trying to run a script in Ubuntu Desktop 64-bit to inject the grub bootloader into an IMG file. Here's the script:

Code: Select all
#!/bin/sh
IMAGE="EGATE.IMG"
offset=$(($(sfdisk -d $IMAGE|awk '/start=/{print $4}'|head -n1|sed 's/,//')*512))
uuid=$(dd if=$IMAGE skip=$((0x27+$offset)) bs=1 count=4 2>/dev/null|hexdump -e '"%X"'|sed 's/\(....\)/\1-/')
LOOP="/dev/loop0"
OPTIONS="umask=000,shortname=mixed,quiet,utf8"
DIR=/tmp/target
mkdir -p $DIR
losetup -d $LOOP
losetup $LOOP $IMAGE
umount $DIR
mount -o loop,offset=$offset,$OPIONS $IMAGE $DIR
mkdir -p $DIR/boot/grub
cat > $DIR/boot/grub/menu.lst <<EOT
timeout 0
title Test
kernel /ce_bz
EOT
cp -v /usr/lib/grub/x86_64-pc/stage1 $DIR/boot/grub
cp -v /usr/lib/grub/x86_64-pc/stage2 $DIR/boot/grub
grub --device-map=/dev/null --batch <<EOT
device (hd0) $LOOP
root (hd0,0)
setup --stage2=$DIR/boot/grub/stage2 (hd0)
EOT
cat > $DIR/splash.idx <<EOT
root=UUID=$uuid
EOT
umount $DIR
losetup -d $LOOP



But I get the following error when I try to run the script:

Code: Select all
~/Desktop$ ./inject
bash: ./inject: /bin/sh^M: bad interpreter: No such file or directory


What am I doing wrong? Also please take a look at the script and point out any errors in it. Thanks!
Igor_Kavinski
Gerbil XP
 
Posts: 302
Joined: Fri Dec 22, 2006 1:34 am

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 7:24 am

That ^M indicates to me that you've got MS-DOS line endings in your text file, that is, CR LF. You need to convert it to Unix line endings, with just LF. There are a few Linux programs out there to do that.

I have the recode program installed, and an alias:

alias dos2ux='recode ibmpc..latin1'

and then IIRC you type

# dos2ux file
Think for yourself, schmuck!
i5-2500K@4.3|Asus P8P67-LE|8GB DDR3-1600|Powercolor R7850 2G|1.5TB 7200.11|1988 Model M|Saitek X-45 & P880|Logitech MX 518|Dell 2209WA|Sennheiser PC151|Asus Xonar DX
bthylafh
Grand Gerbil Poohbah
 
Posts: 3073
Joined: Mon Dec 29, 2003 10:55 pm
Location: Southwest Missouri, USA

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 7:31 am

A faster way would be to type the command "tr -d "\015" < inject > inject-converted". That's assuming that the current directory is where the script file is, and that the script's filename is inject. That command calls the tr program to delete (hence -d) all "\015" characters (that ^M line-ending) in the file inject, then saves the trimmed file to as inject-converted (you can change inject-converted to any filename you want.)
Still living with single core, low-end.
AvKn
Gerbil
 
Posts: 23
Joined: Fri Dec 29, 2006 2:27 pm
Location: Philippines

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 7:48 am

not to take things off topic, but this is a prime example of why linux fails.
blitzy
Gerbil Jedi
 
Posts: 1775
Joined: Thu Jan 01, 2004 5:27 pm
Location: New Zealand

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 8:27 am

You're an example of why trolls fail, actually.
Think for yourself, schmuck!
i5-2500K@4.3|Asus P8P67-LE|8GB DDR3-1600|Powercolor R7850 2G|1.5TB 7200.11|1988 Model M|Saitek X-45 & P880|Logitech MX 518|Dell 2209WA|Sennheiser PC151|Asus Xonar DX
bthylafh
Grand Gerbil Poohbah
 
Posts: 3073
Joined: Mon Dec 29, 2003 10:55 pm
Location: Southwest Missouri, USA

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 9:30 am

This is an example of why Linux fails for you. It's different for others.

I have this ironic belief that Linux works best for either novice computer users or very advanced users. It's because that Linux actually is so simple and painless to use, as long as you don't go off the beaten path and do something like installing applications from source. (Have you even tried Linux recently? Installing applications there is even easier than in Windows, as long it's a standard app.) On the other hand, an ordinary user from time to time tries things that beginners wouldn't do, like trying to improve performance by installing new drivers manually. This, admittedly, is much harder in Linux than in Windows, and something that often only advanced users can do without the hassle of googling.

In Igor's case, do you think anyone who has only recently learned how to use a computer care about bootloaders?
Still living with single core, low-end.
AvKn
Gerbil
 
Posts: 23
Joined: Fri Dec 29, 2006 2:27 pm
Location: Philippines

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 11:15 am

blitzy wrote:not to take things off topic, but this is a prime example of why linux fails.

I can think of a lot more important shortcomings than failing to execute an MS-DOS formatted text file as a shell script.
bitvector
Grand Gerbil Poohbah
 
Posts: 3234
Joined: Wed Jun 22, 2005 3:39 pm
Location: Mountain View, CA

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 11:35 am

Thanks AvKn! The "tr" command worked beautifully :D
Igor_Kavinski
Gerbil XP
 
Posts: 302
Joined: Fri Dec 22, 2006 1:34 am

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 8:15 pm

I guess I should have clarified my point, it's not the fact that the bootloader doesnt work. That is something only an advanced user would want to dig with. My point is regarding the line endings, and lack of an easy to understand plain english error message. Or would it even be that hard for their application to gracefully handle a file with the incorrect ms-dos line endings given how common an issue it is likely to be. From my experience a lot of command line apps in linux are not as easy to use as they could be, using weird syntax and generally lacking easy to understand instructions.

and yes i have used linux, recently ubuntu hardy which is pretty good.
blitzy
Gerbil Jedi
 
Posts: 1775
Joined: Thu Jan 01, 2004 5:27 pm
Location: New Zealand

Re: Linux n00b needs help!

Postposted on Fri Aug 01, 2008 8:32 pm

It's a perfectly clear error message, it says exactly what the problem is - the interpreter that is specified in the script "/bin/sh^M" doesn't exist. It isn't one of those error messages that don't say what the problem is but guess at what the solution might be, because if it isn't that particular problem then you are SoL.

This is also not likely to be a common issue, most POSIX scripts are written on POSIX compliant systems, not on systems that violate standards and use extra characters at the end of lines like the Microsoft family of OSs do. You also have to remember that this way of scripting comes from the early days of UNIX, way before Bill Gates wrote his first OS.

There are plenty of simple ways of dealing with this: "file blah" will tell you precisely what blah is (whether it is MS-DOS line endings or plain ASCII text) and dos2unix and unix2dos have been around since DOS first got network support.

I don't want to come across as one of those stereotypical "RTFM" Linux fans, but please stop and think before posting something like that. There may be other failings in Linux that mean it has not spectacular uptake on the desktop yet, but this really isn't one of them.
notfred
Grand Gerbil Poohbah
 
Posts: 3650
Joined: Tue Aug 10, 2004 9:10 am
Location: Ottawa, Canada

Re: Linux n00b needs help!

Postposted on Sat Aug 02, 2008 12:01 am

Igor: no problem. :D

In the first place, today's Linux distributions actually tries to make people avoid the command line. It's why they're now making graphical interfaces for every conceivable function of the OS, but the terminal will always be there when something fails. The terminal isn't for everyone; it's an artifact of Linux's text user interface-only roots, and it will never be changed to be extremely user-friendly for today's point-and-click world.
Still living with single core, low-end.
AvKn
Gerbil
 
Posts: 23
Joined: Fri Dec 29, 2006 2:27 pm
Location: Philippines

Re: Linux n00b needs help!

Postposted on Sat Aug 02, 2008 3:11 am

OK the script ran fine and the grub bootloader was injected successfully, thus letting me get a taste of Asus Express Gate :D However, I got the following errors during the script:

ioctl: LOOP_CLR_FD: No such device or address
umount: /tmp/target: not mounted

What do these mean? How come the script succeeded even though some of the commands in it failed? I suppose the commands WERE needed otherwise why would the script's author include them in the first place?
Igor_Kavinski
Gerbil XP
 
Posts: 302
Joined: Fri Dec 22, 2006 1:34 am

Re: Linux n00b needs help!

Postposted on Sat Aug 02, 2008 8:22 am

Early in the script, $DIR is defined as /tmp/target. You see a few lines down that it's unmounted with a umount command -- but it hasn't been mounted yet. I'm not sure why the script writer did that; if it was to be "extra safe" then he should have put a comment in or echoed something.

Towards the end of the script, it umounts $DIR again, because it's a temporary mount and isn't needed any more.
Think for yourself, schmuck!
i5-2500K@4.3|Asus P8P67-LE|8GB DDR3-1600|Powercolor R7850 2G|1.5TB 7200.11|1988 Model M|Saitek X-45 & P880|Logitech MX 518|Dell 2209WA|Sennheiser PC151|Asus Xonar DX
bthylafh
Grand Gerbil Poohbah
 
Posts: 3073
Joined: Mon Dec 29, 2003 10:55 pm
Location: Southwest Missouri, USA

Re: Linux n00b needs help!

Postposted on Sat Aug 02, 2008 9:32 am

In a similar way to the unmount before mounting, the script also deletes the loopback device "losetup -d $LOOP" before it actually sets it up, hence the ioctl error message. It's not really a professional way to do that stuff, things should be checked before using if there is a danger of them already being in use and then aborting with the reason why and potentially recommendations to fix. As an example, in the case of the loopback device you can find the next free one with "losetup -f" rather than hard coding to use /dev/loop0 and if that was done (along with a test for that failing) then the script would be more robust.
notfred
Grand Gerbil Poohbah
 
Posts: 3650
Joined: Tue Aug 10, 2004 9:10 am
Location: Ottawa, Canada


Return to Linux, Unix, and Assorted Madness

Who is online

Users browsing this forum: No registered users and 3 guests