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


All times are GMT -5. The time now is 09:41 PM.