LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-12-2006, 08:17 AM   #16
disorderly
Member
 
Registered: Sep 2003
Location: NJ
Distribution: RHEL5
Posts: 154

Original Poster
Rep: Reputation: 30

thanks for the update dive. i gave up for the night last night after 13 hours and the script erased half my files and then itself. i'll try the new code now that my head is clear again
 
Old 04-12-2006, 11:49 AM   #17
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
I can't really see how files are being erased here. Did you try using mv with the -v flag to get a more verbose output? It will say something like test.html -> test.php for each mv. That way you can check back in terminal for errors.

If the cat and sed commands are to blame for deleting files (maybe cat'ing a file back into itself?) you could try using a temporary file:

echo "<?php include(\"/home/functions.php\");validateUser(\$_SERVER['PHP_SELF']);?>" > tempfile
cat $y >> tempfile
mv -v tempfile "${y%.*}.php"

Last edited by dive; 04-12-2006 at 12:38 PM.
 
Old 04-13-2006, 02:20 PM   #18
disorderly
Member
 
Registered: Sep 2003
Location: NJ
Distribution: RHEL5
Posts: 154

Original Poster
Rep: Reputation: 30
there's always another way...

hi dive - excellent alternative solution! the script worked great after that. the problem seemed to be with sed; the script produced some very strange results:
1) most files were truncated (i think sed has a memory limit or something..)
2) files were being copied & dropped in random folders
3) files were having their contents erased so the byte size was 0

here is the finished script
Code:
#!/bin/bash

# this script recursively inserts a line into the head of all files 
# in the directory from where it was called
# it notes all files changed to output.txt
# it shows everything the script does in logs.txt

logs=/home/logs.txt
output=/home/output.txt
phead='<?php include("func.php");validateUser($_SERVER["PHP_SELF"]);?>'

for i in *
do
        if [ -d "$i" ] # if * is a directory
        then
                echo "1) $PWD" >> $logs
                cd "$i" # descend into the directory

                for y in *
                do
                        if echo "$y" | grep ".htm"
                        then
                                echo "2) modifying: $PWD/$y" >> $logs
                                echo $phead > temp
                                cat $y >> temp
                                mv -v temp "${y%.*}.php"
                                chmod 744 "${y%.*}.php"
                                rm -v $y
                                echo "$PWD/$y" >> $output #for testing
                        fi

                        if [ -d "$y" ]   # if a directory, call self
                        then
                                echo "3) $PWD" >> $logs
                                cd "$y"
                                sh /home/c.sh; # call self (recursively) 
                                cd ..
                                echo "4) going back up to: $PWD" >> $logs
                        fi
                done
                cd ..
        fi

        if echo "$i" | grep ".htm"
                then
                        echo "5) modifying: $PWD/$i" >> $logs
                        echo $phead > temp
                        cat $i >> temp
                        mv -v temp "${i%.*}.php"
                        chmod 744 "${i%.*}.php"
                        rm -v $i
                        echo "$PWD/$i" >> $output #for testing
        fi
done
it has one limitation - some of the files in these directories were made with Frontpage [puke] and thus have folder in the same directory named the same way but with the word "html" in them. for example the file "content.html" includes two files: ./_vti_cnf/content.html and ./_derived/content.html_sourcecontrol whatever the f those are, and they get changed by the script too.

for anyone that is intested i also found another way to check for the file extension:
Code:
for i in *;do
        if [ ! -d "$i" ];then
                ext=${i##*.}
                echo '$i' is $i and it\'s extension is $ext
                if [ $ext = htm ]; then
                       echo "$i ends with htm"
                if
        fi
done

Last edited by disorderly; 04-13-2006 at 02:25 PM.
 
Old 04-13-2006, 03:01 PM   #19
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
I'm glad that you got it working.

One thing: after test to see if $i is -d, it acts upon that and either changes the file, or descends into dir, and finally ascends out of it. Next it tests again for .htm and then changes the file, whether or not it is a dir. That is why folders are being changed too. Try this:

Code:
#!/bin/bash

# this script recursively inserts a line into the head of all files 
# in the directory from where it was called
# it notes all files changed to output.txt
# it shows everything the script does in logs.txt

logs=/home/logs.txt
output=/home/output.txt
phead='<?php include("func.php");validateUser($_SERVER["PHP_SELF"]);?>'

for i in *
do
        if [ -d "$i" ] # if * is a directory
        then
                echo "1) $PWD" >> $logs
                cd "$i" # descend into the directory

                for y in *
                do
                        if echo "$y" | grep ".htm"
                        then
                                echo "2) modifying: $PWD/$y" >> $logs
                                echo $phead > temp
                                cat $y >> temp
                                mv -v temp "${y%.*}.php"
                                chmod 744 "${y%.*}.php"
                                rm -v $y
                                echo "$PWD/$y" >> $output #for testing
                        fi

                        if [ -d "$y" ]   # if a directory, call self
                        then
                                echo "3) $PWD" >> $logs
                                cd "$y"
                                sh /home/c.sh; # call self (recursively) 
                                cd ..
                                echo "4) going back up to: $PWD" >> $logs
                        fi
                done
                cd ..

        elif echo "$i" | grep ".htm" # else if not a dir -d
                then
                        echo "5) modifying: $PWD/$i" >> $logs
                        echo $phead > temp
                        cat $i >> temp
                        mv -v temp "${i%.*}.php"
                        chmod 744 "${i%.*}.php"
                        rm -v $i
                        echo "$PWD/$i" >> $output #for testing
        fi
done

Last edited by dive; 04-13-2006 at 03:21 PM.
 
Old 04-13-2006, 03:46 PM   #20
disorderly
Member
 
Registered: Sep 2003
Location: NJ
Distribution: RHEL5
Posts: 154

Original Poster
Rep: Reputation: 30
aHA - nice catch dive i'll try that out tomorrow morning!
again, i appreciate all the help
-disorderly
 
Old 04-13-2006, 06:38 PM   #21
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
Something to test

Sort of going on the basis that if you call a script recursively you shouldn't need the same file writing code twice, I came up with this:

Code:
#!/bin/bash

# this script recursively inserts a line into the head of all files
# in the directory from where it was called
# it notes all files changed to output.txt
# it shows everything the script does in logs.txt

logs=/home/logs.txt
output=/home/output.txt
phead='<?php include("func.php");validateUser($_SERVER["PHP_SELF"]);?>'

for i in *
do
        if [ -d "$i" ] # if * is a directory
        then
                echo "1) $PWD" >> $logs
                cd "$i" # descend into the directory
                        sh /home/c.sh # call self (recursively)
                        echo "4) going back up to: $PWD" >> $logs
                cd ..
        elif echo "$i" | grep ".htm"
        then
                        echo "5) modifying: $PWD/$i" >> $logs
                        echo $phead > temp
                        cat $i >> temp
                        mv -v temp "${i%.*}.php"
                        chmod 744 "${i%.*}.php"
                        rm -v $i
                        echo "$PWD/$i" >> $output #for testing
        fi
done
It worked for me with a small test with dirs going 2 deep, but let me know how it works. No problem for the help btw - I love scripting and it's helped me with one of my own scripts too.

Last edited by dive; 04-13-2006 at 06:52 PM.
 
Old 04-13-2006, 07:13 PM   #22
disorderly
Member
 
Registered: Sep 2003
Location: NJ
Distribution: RHEL5
Posts: 154

Original Poster
Rep: Reputation: 30
short and sweet - that's much tighter coding! it worked perfectly - the end result was the same as the former longer script. thanks for all the help - i appreciate it. this stuff really intrigues me- i've now gone out and picked up the books, "learning the bash shell, unix shell programming" & "wicked cool shell scripts." hopefully i can learn to automate some of my my job so i can have more time to read slashdot at work
-disorderly
 
Old 04-13-2006, 07:56 PM   #23
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
I found this quite helpful: http://www.tldp.org/LDP/abs/html/ plus doing any sort of search for bash scripting or specific commands usually comes up with useful titbits
 
Old 04-13-2006, 08:19 PM   #24
disorderly
Member
 
Registered: Sep 2003
Location: NJ
Distribution: RHEL5
Posts: 154

Original Poster
Rep: Reputation: 30
oh cool - that site is a goldmine! would you happen to know why sed couldn't work on the files i was trying to edit? the text files were between 1k and 139k
 
Old 04-13-2006, 08:23 PM   #25
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
It may be a filesize limitation of sed I guess. But I've had problems in the past escaping characters in sed and iirc that also involved disapearing files.
 
Old 04-14-2006, 06:09 AM   #26
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
There are two things that you kept doing with the sed examples.

cat $file | sed '<sed program' >$file

First you could write this as sed '<sed program>' $file. You don't need the cat command.
Second if you redirect the output to the same file you use as the input, you will zero out the file before it starts. Either produce a different file, or use the '-i' option (in situ editing).

sed '<sed program>' "${file}" >"${file%.html}.php"
rm "$file"

or

sed -i '<sed program>' "${file}"
mv "$file" "${file%.html}.php"

Also, there is an insert command in sed.
sed '1i\
<? your php line ?>' $file >${file%.html}.php}

The newline is needed after the 'i\'

Last edited by jschiwal; 04-14-2006 at 06:14 AM.
 
Old 04-20-2006, 05:30 PM   #27
disorderly
Member
 
Registered: Sep 2003
Location: NJ
Distribution: RHEL5
Posts: 154

Original Poster
Rep: Reputation: 30
thanks jschiwal for the explaination! i feel much better understanding WHY something didn't work. i put
Code:
sed '<sed program>' "${file}" >"${file%.html}.php"
into the program and it worked like a charm

i think i had given up on the use of the -i switch because my version of sed doesn't seem to accept it - just gives me an error. might be too old
 
  


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
How do I insert special character with alt? Meriadoc Linux - Newbie 5 05-17-2006 10:42 AM
Insert a special character with the keyboard? Schreiberling Linux - General 0 03-05-2006 09:09 AM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
how to insert special characters set2004 Linux - Newbie 2 08-05-2004 10:50 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 09:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration