little confusion about variable initialization.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting little confusion about variable initialization.
# 1  
Old 07-22-2010
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 08:31 AM.. Reason: read $input is made input
# 2  
Old 07-22-2010
Hi.

Test that it's set. i.e.

Code:
unset input
while [[ -z $input ]]; do
  read input
done

Maybe a case statement is better:

Code:
unset input
while [[ -z $input ]]; do
  read input
  case "$input" in
   .|..|"") unset input;;
   esac
done

# 3  
Old 07-22-2010
correct read statement

Instead read $input try read input.
# 4  
Old 07-22-2010
Quote:
Originally Posted by CaapAjayShukla
Instead read $input try read input.
Good spot Smilie
# 5  
Old 07-22-2010
Code:
#!/bin/bash

echo "Enter file to delete from tmp"
read a
  if [ ! -e "/tmp/$a" ]
     then
     echo "File Doesnt Exist"
      else
        rm -f /tmp/$a
  fi

# 6  
Old 07-22-2010
1) Use "read input" not "read $input".
2) Test for the file (-f) not the environment variable (-z)
3) Echo your "rm" statement while testing to be sure what will be deleted
4) Don't use "rm -rf" - it deletes the directory too!
5) Test with "rm -i" (interactive) to give you chance of not deleting something unintentional

Code:
#!/bin/bash
echo "Enter file to delete from tmp"
read input
filename="/tmp/${input}"
if [ ! -f "${filename}" ]
then
        echo "File Doesnt Exist"
else
        echo rm -i "${filename}"
fi

This User Gave Thanks to methyl For This Post:
# 7  
Old 07-22-2010
Thanks methyl

Thanks methyl for making all points clear, Really appreciate.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error occurred during initialization of VM

Hi , I was invoking a sh file using the nohup command. But while invoking, I received a below error. Error occurred during initialization of VM Unable to load native library: /u01/libjava.so: cannot open shared object file: No such file or directory . Could you please help out. Regards,... (2 Replies)
Discussion started by: Kamal1108
2 Replies

2. Shell Programming and Scripting

Variable initialization

Hallo Team, I have a simple request and i would appreciate your help. I would like to use two dates in my script lets: A=$(date +"%d %B %Y") echo $A 23 June 2014 That's awesome now we cooking. Now i want B to be on the previous month eg: echo $B Should give me 23 May 2014 I would... (9 Replies)
Discussion started by: kekanap
9 Replies

3. Shell Programming and Scripting

Initialization error

new to shell scripting. below line is showing error in script. ${parameter:=word} in the o/p first it shows the below error. word: not found. and then in next line print "word" ---------------- p2: word: not found. word --------------------------- OS is AIX and shell is... (12 Replies)
Discussion started by: scriptor
12 Replies

4. Shell Programming and Scripting

Declaring variables without initialization

I get an error in my shell script that line 1: )unexpected. Line 1 in my script (using sh by the way) is the variable I declared but did not initialize it. result= Is this wrong? How can I fix it? I am using the variable later in the program, so I figured I could just declare it first... (4 Replies)
Discussion started by: itech4814
4 Replies

5. Programming

Class Pointer initialization C++

Hello everyone, I have a question, that are the following ways of pointer intialization same ? ClassA *point; point = 0; point = new ClassA; Thanks a load in advance!! Regards, (10 Replies)
Discussion started by: mind@work
10 Replies

6. Programming

Char initialization

Hi All, char a="\0"; a) a contains \0 a contains garbage value b) a contains \ a contains 0 a contains garbage value Pls, let me know correct result is a or b. I guess a. Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

7. Shell Programming and Scripting

Confusion about IFS Variable

================================================= #!/bin/sh HOST=eux091 if && grep -q $HOST /etc/maestab then IFS=: grep -v -e "^#" -e "^$" /etc/maestab | grep $HOST | \ read HOST MAESTRO_BIN FLAG MAESTRO_USER echo $MAESTRO_BIN MAESTRO_HOME=`dirname $MAESTRO_BIN`... (7 Replies)
Discussion started by: Awadhesh
7 Replies

8. UNIX for Dummies Questions & Answers

Shell initialization files

As you know, when a user logs in, the shell reads the initialization files in an order something like below... ################### Bourne Shell /etc/profile > $HOME/.profile Bash Shell /etc/profile > $HOME/.bash_profile > $HOME/.bash_login > $HOME/.profile > $HOME/.bashrc C Shell... (3 Replies)
Discussion started by: SeanWuzHere
3 Replies

9. UNIX for Dummies Questions & Answers

Help regarding storing the initialization parameters

Hi all, In my shell script, I am reading some files, processing them, and writing the out-put in the log files. The out put contains number of rows in the file etc. I want to fetch the input files from a particular directory and I want to write the logs in a particular directory with a particular... (1 Reply)
Discussion started by: VENC22
1 Replies

10. Programming

Struct Initialization

Hi We are using a code generator for initializing structures with the #define macro. Compiling it with the GCC 2.8.1 (with -ansi) it OK. But when we are using the SUN C 5.0 compiler it screams. Following is a code sample: #include <stdlib.h> #include <stdio.h> typedef struct TEST3 {... (4 Replies)
Discussion started by: amatsaka
4 Replies
Login or Register to Ask a Question