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 21 Oct 2014, 16:30
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 Posts_count  
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: 2257

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: 2525
Location: Moved from Hosla to www.eussenheim.de

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: 2525
Location: Moved from Hosla to www.eussenheim.de

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: 2257

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: 2257

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: 501
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:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


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