LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash command help (https://www.linuxquestions.org/questions/linux-newbie-8/bash-command-help-73284/)

bobdinkel 07-16-2003 11:39 AM

bash command help
 
Hi,
I'm sorry if this has already been covered - I'm not really sure how to look for an answer for this...
I'm looking for a way to append a line to the beginning of several files. Specifically, I want to add the she-bang line as the first line to several php files. I developed an app on a windows box that doesn't need the path, but the linux box does. I was hoping that there would be something similar to the following that would put the text as the first line rather than the last:
echo "#!/usr/bin/php" >> *.php

Or I'd also be happy knowing how to change things so that it would no longer be necessary to add the path to each file (Apache 2.0.40 / PHP4.2.2).
Thanks. Feel free to let me know if I'm a dumbass (I know you won't let me down:) ).

david_ross 07-16-2003 01:16 PM

I haven't tested this but you could probably use this script:

#!/bin/bash
IFS="
"
for i in `find /path/to/files -regex *.php`; do
mv $i $i.backup-file
echo "#!/usr/bin/php" > $i
cat $i.backup-file >> $i
# Uncomment the next line if you want to delete the backup file
#rm -f $i.backup-file
done

DavidPhillips 07-16-2003 01:29 PM

beat me to it, LOL

oh well here's mine

script name prepend

#!/bin/sh
for file in `ls $2`
do
mv $file tmp
echo $1 > $file
cat tmp >> $file
rm tmp
done

run like this

prepend "#!/usr/bin/php" "*.php"

bobdinkel 07-16-2003 01:49 PM

Wow. Thanks, guys. That was fast. DavidPhillips, I tried your script because it looked like a little easier to understand. However, when I ran it, I got this error:
bash: !/usr/bin/php": event not found

I'm off to try the other script...

DavidPhillips 07-16-2003 02:27 PM

might have left of something
it works on mine

fancypiper 07-16-2003 02:42 PM

Don't forget the she in the shebang

bobdinkel 07-16-2003 03:45 PM

I'm not sure what I'm doing wrong. The error that was getting (mentioned in an earlier post) made me think that the script was choking on the first argument (in this case #!/usr/bin/php). So I tried hard-coding the info. This is what I ended up with:
#!/bin/sh
for file in 'ls *.php'
do
mv $file tmp
echo "#!/usr/bin/php" > $file
cat tmp >> $file
rm tmp
done

When running it in the same dir as all the php files, I get this error:
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
cat: tmp: No such file or directory
rm: cannot lstat `tmp': No such file or directory

Any further help would be greatly appreciated. I promise I'm not an idiot - just not too linux saavy. Thanks

fancypiper 07-16-2003 03:57 PM

touch tmp (create the file) and see if it works

bobdinkel 07-16-2003 04:04 PM

Well, that made the error shorter:
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.

But still no dice.

fancypiper 07-16-2003 04:16 PM

Perhaps you meant to store all the files in a directory /tmp or something??

bobdinkel 07-16-2003 04:32 PM

I can't say what all is supposed to be going on in the above scripts, but storing all the files in /tmp was never a goal of mine. I think that tmp is supposed to be a temporary file name, but somehow it isn't working out.
I know this should be a simple matter. But I think I'll try to figure out a way to make php files not need the path. Or is that just the way it is in the *nix world?

J_Szucs 07-16-2003 06:03 PM

Never use space character in filenames or bear the consequences...

DavidPhillips 07-16-2003 06:19 PM

mv $file tmp is great if the filename is like this "onename"

if a file is named "my file" then "mv $file tmp" will try to move the file "my" and the file "file" . not good


is that the problem

DavidPhillips 07-16-2003 06:22 PM

you must use the double quotes around the text to add and the filter

like this

prepend "#!/usr/bin/php" "*.php"

DavidPhillips 07-16-2003 06:24 PM

the problem with your script is that you are using quotes where apostrophies go

copy and paste the script above, you'll see what I mean

DavidPhillips 07-16-2003 06:41 PM

Ok scratch the above, I have found the problem

you need quotes not double qoutes or else you get an error with the #!/usr/bin/php part of it due to the "!" I guess.

here is an example

[david@zeus david]$ echo file1 > 1.php

[david@zeus david]$ echo file2 > 2.php

[david@zeus david]$ echo file3 > 3.php

[david@zeus david]$ cat prepend
#!/bin/sh
for file in `ls $2`
do
mv $file tmp
echo $1 > $file
cat tmp >> $file
rm tmp
done

[david@zeus david]$ cat *.php
file1
file2
file3

[david@zeus david]$ sh ./prepend "#!/usr/bin/php" "*.php"
bash: !/usr/bin/php": event not found

[david@zeus david]$ sh ./prepend '#!/usr/bin/php' '*.php'

[david@zeus david]$ cat *.php
#!/usr/bin/php
file1
#!/usr/bin/php
file2
#!/usr/bin/php
file3

dalai 07-17-2003 08:10 AM

you might like to use Perl.
All on one single line, it looks like

for i in *.php;do perl -pi -e '$_="\#\!\/usr\/bin\/php\n$_" if $. == 1' $i ;done

ie for all the first lines in all php files replace this first line by what you want

dalai 07-17-2003 08:27 AM

Just an addition, if you do not like Perl, maybe you can stay with Bash

for i in *.php;do (tac $i ;echo '#!/usr/bin/php')>toto;tac toto >$i; done

Of course in the end you are left with a temp file toto you'll have to remove (which is not the case via Perl

moses 07-17-2003 11:35 AM

No temporary files, one line in bash:
Code:

for i in *.php; do echo '#!/bin/sh' | cat - $i | dd of=$i;done

bobdinkel 07-17-2003 12:28 PM

Hot damn! I got it to work! Just so you folks know, there were no files with spaces in their names. Initially, I did copy and paste your script, but couldn't get it to work. I added the "#!/usr/bin/php" in double quotes in place of $1 out of desperation. I went back and changed those to single quotes and it worked. But I still had to hard-code the values. It was still choking on the first argument before I did that.
Thanks. I very much appreciate your help. Mighty neighborly.

DavidPhillips 07-17-2003 02:21 PM

cool


you gotta love allthe possibilities


All times are GMT -5. The time now is 10:36 PM.