bash scripting anyone?

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

bash scripting anyone?

Post by ayu »

I am trying to grab the first 3 characters from a string and do a regular string comparison, but for some reason I get an error saying "lpe.sh: 4: Bad substitution
". I have tried to write the ${VAR:0:3} with different syntax but without success.

Here's the code

Note: In a terminal by just typing this, it works ... but not in a script for some reason.

Code: Select all

#!/bin/bash

VAR=$(uname -r)
VER=${VAR:0:3}
echo "Detected kernel version "`uname -r`

if [ "$VER" == '2.6' ]; then
	echo "Trying 2.6 exploits"
fi
Anyone? =)
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: bash scripting anyone?

Post by bad_brain »

um...wtf?
I've copy&paste'ed your code, ran it and also got the "bad substitution" error.
then I've tried something different by using the cut command instead of 0:3, and I got the same error.
then I browsed back in the text editor, replaced the script again with the old version...and it worked... 8O
lenny:~# ./test.sh
Detected kernel version 2.6.26-2-686
Trying 2.6 exploits
I'll attach "my version", would be funny if it works for you too, or maybe you can see a difference to your version...I can't find anything.

P.S. sorry, had to pack it because shell scripts can't be attached.
Attachments
script.tar.gz
(281 Bytes) Downloaded 54 times
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: bash scripting anyone?

Post by ayu »

hmm, still the same problem here.
Even with your version.

What the hell : /

This is slightly annoying that my progress has to be stopped by something as trivial as this ^^
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: bash scripting anyone?

Post by bad_brain »

hm, tried it on Lenny and Etch now: works on both.
what distro are you using? :-k
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: bash scripting anyone?

Post by ayu »

bad_brain wrote:hm, tried it on Lenny and Etch now: works on both.
what distro are you using? :-k

Squeeze =F
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: bash scripting anyone?

Post by bad_brain »

hm, what version is bash on Squeeze?

Code: Select all

bash --version
I can ask some Debian people if there are some astonishing changes, which I kinda doubt, but you never know... :lol:
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: bash scripting anyone?

Post by ayu »

Code: Select all

cats@akane:~$ bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: bash scripting anyone?

Post by bad_brain »

ok, in Etch and Lenny it's version 3.x, maybe some functions have been kicked out in version 4 (which would be odd because those are basic functions), I am waiting for a reply now... :wink:
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: bash scripting anyone?

Post by ayu »

Ok, thanks man :)
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: bash scripting anyone?

Post by bad_brain »

hm, no bugs known, and for all users I've asked it works on squeeze (even on sid), what's the output when you run it like this:

Code: Select all

bash -x script.sh
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: bash scripting anyone?

Post by ayu »

Then it works, but not if I do

Code: Select all

sh script.sh
Strange ..... and utterly annoying
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: bash scripting anyone?

Post by bad_brain »

ohhhh......now that clears it up. I thought you simply run it by ./script.
you have to keep in mind that sh != bash, in some distros (Ubuntu for example) sh is simply linked to bash, but not in Debian, so if you run it that way you are NOT running it in bash but in sh shell (line 4 is a bash function and not supported by the really oooooold sh, that's why you get the error).

simply use one of those methods to run it:
./script.sh
/path/to/script.sh
bash script.sh
here you can see the differences between shells, also between sh and bash:
http://www.faqs.org/faqs/unix-faq/shell ... fferences/" onclick="window.open(this.href);return false;

:wink:
Image

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: bash scripting anyone?

Post by ayu »

ahhaaaaa xD

That explains a lot ^^

Thanks a lot man, I'll throw you some man love right here <3 lol
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: bash scripting anyone?

Post by bad_brain »

thanks for the love.... :lol:
(check the IT Crowd clip in the "links" board I've posted, kinda fits :lol: )

and no problem, I also learned something, didn't knew Debian is one of the few distros that REALLY still have sh (not just as link to bash)... :D
Image

Post Reply