LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2005, 03:05 AM   #1
chooi
LQ Newbie
 
Registered: Mar 2005
Posts: 4

Rep: Reputation: 0
shell script problem.


Hi all,
I am writing a shell script to read text file .The text file cointains the file path that will be for deletion.

Code:
      processing()
{
    exec < $textfile
    while read LINE
    do
	if [ echo $LINE | cut -c 2-2`" = "|" ]; then	    
	    FILETYPE="`echo $LINE | cut -c 1-1`"
	    FILEPATH="`echo $LINE | cut -f2 -d'|'`"
	    TMP="`echo $FILEPATH | cut -c 1-1`"
	    if [ $TMP = "/" ]; then
	        ABSOLUTE="0"
	    else
	        ABSOLUTE="1"
	    fi
	    
	    #echo "realline [$FILETYPE][$FILEPATH][$ABSOLUTE]"	    

	    if [ $FILETYPE = "f" -o $FILETYPE = "s" ]; then
		if [ $ABSOLUTE = "1" ]; then
		    FILEPATH=$DATAPATH/$SYSTEMNME/$FILEPATH
		fi
	        if [ -f $FILEPATH ]; then
		    echo "Removing file $FILEPATH" >> $LOGFILE
	            rm $FILEPATH
		else 
		    if [ -d $FILEPATH ]; then
		        echo "Removing directory $FILEPATH" >> $LOGFILE
		        rm -r $FILEPATH
		    else
		        echo "FAIL: $FILEPATH not exist." >> $LOGFILE
		    fi
		fi
	    else 
		if [ $ABSOLUTE = "1" ]; then
		    HASH_DIR=$DATAPATH/$SYSTEMNME/$FILEPATH
		fi
	        STR_TO_PARSE=$FILEPATH
		count_occurances
		COUNT2=`expr $COUNT - 1`
		HASH_FILE=$DATAPATH/$SYSTEMNME/"`echo $FILEPATH | cut -f-$COUNT2 -d'/'`"/D_"`echo $FILEPATH | cut -f$COUNT -d'/'`"

	        if [ -f $HASH_FILE ]; then
		    echo "Removing file $HASH_FILE" >> $LOGFILE
	            rm $HASH_FILE
		else
		    echo "FAIL: $HASH_FILE not exist." >> $LOGFILE
		fi
                
		if [ -d $HASH_DIR ]; then
	            echo "Removing directory $HASH_DIR" >> $LOGFILE
                    rm -r $HASH_DIR
		else
		    echo "FAIL: $HASH_DIR not exist." >> $LOGFILE
		fi
	    fi
	fi
    done 
}
context of the text file :
s|/tmp/text
<blank line>

But the problem is : the text file need blank line in order to process my script.Any problem with my script? Please help!
 
Old 03-26-2005, 04:40 AM   #2
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
What is the actual problem? The specification is very unclear.

Well, by inspection, there will be a problem with blank line. Becuase $filetype and $tmp will be empty string in this case and so by default $absolute is 1. If it is the case, the 'else' part of the if loop will be executed to clear the hash_dir.
 
Old 03-26-2005, 06:15 AM   #3
nixcraft
Member
 
Registered: Nov 2004
Location: BIOS
Distribution: RHEL3.0, FreeBSD 5.x, Debian 3.x, Soaris x86 v10
Posts: 379

Rep: Reputation: 30
If your file is as follows:
cat /home/me/files
/tmp/xyz
/tmp/abc

And would like to remvoe the files /tmp/abc ... then use code

Code:
a=$(cat /home/me/files)
for i in $a
do
   rm $i
done
 
Old 03-26-2005, 07:31 AM   #4
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
His input file is a bit more complicated than that.

First character is used to indicate file type. Either character f or s.
Second char is to indicate if it is an abs path or not.

Well, it would be a lot easier if it is done in perl.
 
Old 03-26-2005, 08:00 AM   #5
chooi
LQ Newbie
 
Registered: Mar 2005
Posts: 4

Original Poster
Rep: Reputation: 0
now the situation is : i don't want the blank line there..but without the blank line , my script will not succeed . Any solution?
 
Old 03-26-2005, 08:08 AM   #6
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
I have offered my idea in post #2. Please read carefully.

It is really a matter adding one more condition to the 'if' loop of "if [ $FILETYPE = "f" -o $FILETYPE = "s" ]"

e.g.

if [$FILETYPE = "" ]; then
# do nothing
else if ...
(your existing code)
 
Old 03-26-2005, 08:32 AM   #7
chooi
LQ Newbie
 
Registered: Mar 2005
Posts: 4

Original Poster
Rep: Reputation: 0
so ,if i change the code like wat u say ,and remove the blank line ..also not a issue anymore??
 
Old 03-26-2005, 05:16 PM   #8
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
Programming = codeing + tesing

Well, it is not possible to know if it solve all your problem if you don't test it.

And even you solved this problem, other unexpected problem may show up.

So, why not just give it a go and test it ? :-)
 
  


Reply



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
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Shell script problem maringat Linux - Software 5 07-25-2005 05:12 PM
Problem in shell script Kumar Programming 4 04-27-2004 08:48 AM
shell script problem steltner Linux - General 19 05-18-2003 02:42 PM
shell script problem steltner Programming 0 05-17-2003 03:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 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
Open Source Consulting | Domain Registration