LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-07-2012, 08:15 AM   #1
smilemukul
Member
 
Registered: Jun 2009
Distribution: Redhat,CentOS,Ubuntu,Puppet
Posts: 292

Rep: Reputation: 34
Not able to delete self script after executing


Hi,
I am not able to delete self script after executing the script through $0, i am getting the following error message as,

"rm: cannot lstat `bash': No such file or directory"

I had set my environment to bash in the system.
 
Old 02-07-2012, 08:19 AM   #2
deep27ak
Senior Member
 
Registered: Aug 2011
Location: Bangalore, India
Distribution: rhel 5x,6.0,6.2, centOS 5x,6.0,6.2
Posts: 1,194
Blog Entries: 4

Rep: Reputation: 221Reputation: 221Reputation: 221
what and where is the script located?

post the output of

Code:
#echo $PATH
 
Old 02-07-2012, 09:39 AM   #3
smilemukul
Member
 
Registered: Jun 2009
Distribution: Redhat,CentOS,Ubuntu,Puppet
Posts: 292

Original Poster
Rep: Reputation: 34
Script is located in path:-- /root/script

echo $PATH :--
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
 
Old 02-08-2012, 01:24 AM   #4
deep27ak
Senior Member
 
Registered: Aug 2011
Location: Bangalore, India
Distribution: rhel 5x,6.0,6.2, centOS 5x,6.0,6.2
Posts: 1,194
Blog Entries: 4

Rep: Reputation: 221Reputation: 221Reputation: 221
what do you get when you use

Code:
#rm -r /root/script
 
Old 02-08-2012, 01:36 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by smilemukul View Post
Script is located in path:-- /root/script

echo $PATH :--
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
/root/script is not in the PATH; /root/bin is.
 
Old 02-08-2012, 01:38 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by deep27ak View Post
what do you get when you use

Code:
#rm -r /root/script
What is the purpose of the -r? Given that the OP wants to delete a single file it is unnecessary and may delete files the OP does not want to delete.
 
Old 02-08-2012, 01:53 AM   #7
stilian.lazarov
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: 1
touch script
vi script
put in file script the text above.

#!/bin/sh
rm -r ./script

after that make file script executable with command

chmod +x ./script
then run ./script
This is all.
 
1 members found this post helpful.
Old 02-08-2012, 07:36 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The -r is unnecessary and dangerous, especially as the script may be run with the current directory other than its own directory, perhaps a directory that has a subdirectory called script. Here's a command prompt session illustrating the danger:
Code:
c@CW8:/tmp$ mkdir -p a/b/c
c@CW8:/tmp$ touch a/x a/b/y a/b/c/z
c@CW8:/tmp$ tree a
a
|-- b
|   |-- c
|   |   `-- z
|   `-- y
`-- x

2 directories, 3 files
c@CW8:/tmp$ rm -r a
c@CW8:/tmp$ ls -l a
ls: cannot access a: No such file or directory
BTW, a running shell script is not always able to determine the file that contains the code it is running for reasons explained here.
 
Old 02-08-2012, 07:52 AM   #9
stilian.lazarov
LQ Newbie
 
Registered: Feb 2012
Posts: 2

Rep: Reputation: 1
Yes it is dangerous to use -r but If you use full path to script it will be not dangerous.
 
Old 02-08-2012, 09:06 AM   #10
smilemukul
Member
 
Registered: Jun 2009
Distribution: Redhat,CentOS,Ubuntu,Puppet
Posts: 292

Original Poster
Rep: Reputation: 34
Thanks all, its solved.
 
Old 02-08-2012, 11:33 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Glad you found a solution

Threads can be marked SOLVED via the Thread Tools menu.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script - executing answer to another script domz Programming 4 11-07-2010 04:19 PM
Run an external script, without holding up the currently executing script onesikgypo Programming 2 03-07-2009 09:26 PM
need help executing a script that someone sent me giantjoebot Linux - Newbie 16 08-13-2008 12:35 AM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 05:05 PM
executing a script!! vishamr2000 Linux - Security 5 07-29-2005 02:34 AM


All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration