Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 29 Jul 2014, 17:01
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
When scripting goes wrong.
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [13 Posts]  
Author Message
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Mon 04 Feb 2013, 01:01    Post subject:  When scripting goes wrong.  

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

Code:
 
#!/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
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2225

PostPosted: Mon 04 Feb 2013, 06:36    Post subject:  

Was there a question in there somewhere?
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Mon 04 Feb 2013, 12:47    Post subject:  

amigo wrote:
Was there a question in there somewhere?

Nothing you'd be able to answer.
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Mon 04 Feb 2013, 13:49    Post subject:  

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

HTH Wink
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Mon 04 Feb 2013, 14:23    Post subject:  

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:

/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
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2505
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Tue 05 Feb 2013, 05:47    Post subject:  

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?
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Tue 05 Feb 2013, 13:10    Post subject:  

Quote:


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. Smile In the above scenario, you only have one shot at having it all correct.
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2225

PostPosted: Tue 05 Feb 2013, 15:07    Post subject:  

Are you sure it's even bash? Isn't your /bin/sh a link to busybox?
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Tue 05 Feb 2013, 17:19    Post subject:  

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:

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)
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2225

PostPosted: Wed 06 Feb 2013, 04:29    Post subject:  

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.
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Wed 06 Feb 2013, 11:32    Post subject:  

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.
Back to top
View user's profile Send private message 
Ibidem

Joined: 25 May 2010
Posts: 464
Location: State of Jefferson

PostPosted: Fri 15 Feb 2013, 20:00    Post subject:  

This is what
Code:
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:
export VARNAME="some string"
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3220

PostPosted: Sun 24 Feb 2013, 12:15    Post subject:  

Ibidem wrote:
This is what
Code:
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:
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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [13 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1194s ][ Queries: 12 (0.0520s) ][ GZIP on ]