| Author |
Message |
jpeps
Joined: 31 May 2008 Posts: 2421
|
Posted: 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
| 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
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1757
|
Posted: Mon 04 Feb 2013, 06:36 Post subject:
|
|
Was there a question in there somewhere?
|
|
Back to top
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2421
|
Posted: 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
|
|
 |
L18L
Joined: 19 Jun 2010 Posts: 1700 Location: Burghaslach, Germany
|
Posted: Mon 04 Feb 2013, 13:49 Post subject:
|
|
| my console wrote: | # MyGreetings ="greetings/thanks.txt"
bash: MyGreetings: command not found
# |
HTH
|
|
Back to top
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2421
|
Posted: Mon 04 Feb 2013, 14:23 Post subject:
|
|
| L18L wrote: | | my console wrote: | # MyGreetings ="greetings/thanks.txt"
bash: MyGreetings: command not found
# |
HTH  |
..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
|
|
 |
L18L
Joined: 19 Jun 2010 Posts: 1700 Location: Burghaslach, Germany
|
Posted: 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
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2421
|
Posted: 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. In the above scenario, you only have one shot at having it all correct.
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1757
|
Posted: 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
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2421
|
Posted: 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
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1757
|
Posted: 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
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2421
|
Posted: 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
|
|
 |
Ibidem
Joined: 25 May 2010 Posts: 243
|
Posted: Fri 15 Feb 2013, 20:00 Post subject:
|
|
This is what 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
|
|
 |
jpeps
Joined: 31 May 2008 Posts: 2421
|
Posted: Sun 24 Feb 2013, 12:15 Post subject:
|
|
| Ibidem wrote: | This is what 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
|
|
 |
|