LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   little confusion about variable initialization. (https://www.linuxquestions.org/questions/linux-newbie-8/little-confusion-about-variable-initialization-821451/)

pinga123 07-22-2010 06:08 AM

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


grail 07-22-2010 06:38 AM

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.

pinga123 07-22-2010 06:44 AM

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


Web31337 07-22-2010 06:56 AM

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.

Lost_Oracle 07-22-2010 09:10 AM

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.

pinga123 07-23-2010 12:12 AM

Quote:

Originally Posted by Web31337 (Post 4041726)
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


MTK358 07-23-2010 06:49 AM

You said "Enter file or folder", but rm without the -r option will not delete directories.

pinga123 07-23-2010 06:59 AM

Quote:

Originally Posted by MTK358 (Post 4042921)
You said "Enter file or folder", but rm without the -r option will not delete directories.

What can be done?

MTK358 07-23-2010 07:23 AM

Use the -r option.


All times are GMT -5. The time now is 08:30 AM.