LinuxQuestions.org
Help answer threads with 0 replies.
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 07-22-2010, 06:08 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
little confusion about variable initialization.


Whenever i execute the below scriptlet with out proper file name it deletes /tmp directory .

I guess this is because value of variable a didnt get initialized and there for rm -rf /tmp/ get executed and entire /tmp directory get deleted.

How would i avoid any empty variables to be used in script? as this is a classic case of destructive script.

Code:
#!/bin/bash

echo "Enter file to delete from tmp"
read input
a=`ls /tmp/$input`
if [ -z "/tmp/$a" ]
then
echo "File Doesnt Exist"
else
rm -rf /tmp/$a
fi

Last edited by pinga123; 07-22-2010 at 06:32 AM. Reason: read $input is made read input
 
Old 07-22-2010, 06:38 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,530

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
Maybe you should do a man test (test being the same as using [) and then look for which test checks if a files exists.
Also, always try your commands at the command line prior to using them in a script to see what the result is.
 
1 members found this post helpful.
Old 07-22-2010, 06:44 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 37
I have modified the code . Please share your views on the modified script.
Code:
#!/bin/bash
echo "Enter file or folder to delete from tmp"
read input
if [ ! -f "/tmp/$input" ]
then
echo "File Doesnt Exist"
else
rm -r "/tmp/$input"
fi
 
Old 07-22-2010, 06:56 AM   #4
Web31337
Member
 
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Blog Entries: 71

Rep: Reputation: 65
like this
Code:
#!/bin/bash

echo -n "Enter file to delete from tmp: "
read IN
#test -z "$IN" && echo No input given, exiting ... && exit 127
if [ -z "$IN" ]; then
	echo No input given, exiting ...
	exit 127
fi
FNAME="/tmp/$IN"
if [ -f "$FNAME" ]; then
	rm -f "$FNAME" 2>/dev/null
	exit $?
else
	echo Specified path does not exist or is not regular file, exiting ...
	exit 127
fi
There is a commented line with test command instead of [ command. You can use either.

Last edited by Web31337; 07-22-2010 at 06:58 AM.
 
Old 07-22-2010, 09:10 AM   #5
Lost_Oracle
LQ Newbie
 
Registered: Jul 2010
Distribution: Red Hat Commercial
Posts: 16

Rep: Reputation: 2
You probably already knew this, and I don't want to insult you, but being as this is in the newbie forum I figured it couldn't hurt to mention that the rm -rf command can be pretty dangerous. Especially when dealing with user input (doubly so if that user has full privileges). Your system will quite happily delete your OS if you tell it to. For and example of this (and a laugh) I suggest you watch this video.
 
Old 07-23-2010, 12:12 AM   #6
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by Web31337 View Post
like this
Code:
#!/bin/bash

echo -n "Enter file to delete from tmp: "
read IN
#test -z "$IN" && echo No input given, exiting ... && exit 127
if [ -z "$IN" ]; then
	echo No input given, exiting ...
	exit 127
fi
FNAME="/tmp/$IN"
if [ -f "$FNAME" ]; then
	rm -f "$FNAME" 2>/dev/null
	exit $?
else
	echo Specified path does not exist or is not regular file, exiting ...
	exit 127
fi
There is a commented line with test command instead of [ command. You can use either.
Thanks for your reply. I have modified my script as follows . Is there any potential loop hole in below script?
Code:
#!/bin/bash
echo "Enter file or folder to delete from tmp"
read input
if [ ! -f "/tmp/$input" ]
then
echo "File Doesnt Exist"
exit 1
else
rm -f "/tmp/$input"
fi
 
Old 07-23-2010, 06:49 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
You said "Enter file or folder", but rm without the -r option will not delete directories.
 
Old 07-23-2010, 06:59 AM   #8
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by MTK358 View Post
You said "Enter file or folder", but rm without the -r option will not delete directories.
What can be done?
 
Old 07-23-2010, 07:23 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Use the -r option.
 
  


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: How do I read data from a variable while that variable is being populated? theaceoffire Programming 4 04-23-2010 02:29 PM
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:11 PM.

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