When scripting goes wrong.

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

When scripting goes wrong.

#1 Post by jpeps »

Okay..I was nice and added a test directory. The "greetings" directory could have been installed in /root :)

Code: Select all

 
#!/bin/sh

mkdir -p /tmp/test/greetings 

Greeting="In thanks for the wonderful time we've spent together"
echo "$Greeting"

touch /tmp/test/greetings/thanks.txt

echo  $Greeting > /tmp/test/greetings/thanks.txt
MyGreetings ="greetings/thanks.txt"

## clean
rm -r "/tmp/test/$MyGreetings"

cd /tmp/test

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#2 Post by amigo »

Was there a question in there somewhere?

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#3 Post by jpeps »

amigo wrote:Was there a question in there somewhere?
Nothing you'd be able to answer.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#4 Post by L18L »

my console wrote:# MyGreetings ="greetings/thanks.txt"
bash: MyGreetings: command not found
#
HTH :wink:

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#5 Post by jpeps »

L18L wrote:
my console wrote:# MyGreetings ="greetings/thanks.txt"
bash: MyGreetings: command not found
#
HTH :wink:
..and the test directory was wiped out. If it had been placed in /root/greetings/thanks.txt, the root directory would be gone.

Code: Select all

/mnt/sda2/Desktop # ./testit
In thanks for the wonderful time we've spent together
./testit: line 14: MyGreetings: command not found
./testit: line 21: cd: /tmp/test: No such file or directory

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#6 Post by L18L »

jpeps wrote:..and the test directory was wiped out...
Not if
#rm -r "/tmp/test/$MyGreetings"
[ -f $MyGreetings ] && rm -r "/tmp/test/$MyGreetings"

or does a "halt on all errors" exist?

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#7 Post by jpeps »


Not if
#rm -r "/tmp/test/$MyGreetings"
[ -f $MyGreetings ] && rm -r "/tmp/test/$MyGreetings"

That would still wipe out your test directory.

[ $MyGreetings ] && rm -r "/tmp/test/$MyGreetings" would provide safety.

edit:

adding quotes seems to work:

[ -f "$MyGreetings" ]

Bash is a bit temperamental. :) In the above scenario, you only have one shot at having it all correct.

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#8 Post by amigo »

Are you sure it's even bash? Isn't your /bin/sh a link to busybox?

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#9 Post by jpeps »

amigo wrote:Are you sure it's even bash? Isn't your /bin/sh a link to busybox?
No; ash is linked to busybox. That's inconsequential, however. The results are the same.

Code: Select all

lrwxrwxrwx 1 root root 4 2011-09-16 23:00 /bin/sh -> bash
GNU bash, version 4.1.0(1)-release (i686-pc-linux-gnu)

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#10 Post by amigo »

It's far from inconsequential which exact shell is being used to run a script. No two of them support exactly the same features and syntax. So, many scripts which simply point to /bin/sh assume features which may or may not be there on another system.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#11 Post by jpeps »

amigo wrote:It's far from inconsequential which exact shell is being used to run a script. No two of them support exactly the same features and syntax. So, many scripts which simply point to /bin/sh assume features which may or may not be there on another system.
It's a simple script that wipes out the mother directory, either with bash or with ash.

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

#12 Post by Ibidem »

This is what

Code: Select all

set -e
is for, according to Google.

Amigo: the problem is the space between MyGreetings and ="greetings/thanks.txt", which makes any shell assume you meant "execute MyGreetings with the parameter =greetings/thanks.txt"
Then, when you try to use the variable MyGreetings, it's empty.

Now if someone malicious had exported MyGreetings=../../sbin/init just before you executed this...
And then there's the chance that someone dropped an executable by that name somewhere.
I usually use

Code: Select all

export VARNAME="some string"

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#13 Post by jpeps »

Ibidem wrote:This is what

Code: Select all

set -e
is for, according to Google.

Amigo: the problem is the space between MyGreetings and ="greetings/thanks.txt", which makes any shell assume you meant "execute MyGreetings with the parameter =greetings/thanks.txt"
Then, when you try to use the variable MyGreetings, it's empty.

Now if someone malicious had exported MyGreetings=../../sbin/init just before you executed this...
And then there's the chance that someone dropped an executable by that name somewhere.
I usually use

Code: Select all

export VARNAME="some string"
Typically, spaces are commonly used when declaring variables, for example in java or C,
and wouldn't have a protective "test" directory to prevent deletion of the parent directory.

Post Reply