Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
07-16-2003, 12:39 PM
|
#1
|
LQ Newbie
Registered: Mar 2003
Location: Austin, TX
Posts: 6
Rep:
|
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  ).
|
|
|
07-16-2003, 02:16 PM
|
#2
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
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
Last edited by david_ross; 07-16-2003 at 02:18 PM.
|
|
|
07-16-2003, 02:29 PM
|
#3
|
LQ Guru
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163
Rep:
|
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"
Last edited by DavidPhillips; 07-16-2003 at 02:32 PM.
|
|
|
07-16-2003, 02:49 PM
|
#4
|
LQ Newbie
Registered: Mar 2003
Location: Austin, TX
Posts: 6
Original Poster
Rep:
|
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...
|
|
|
07-16-2003, 03:27 PM
|
#5
|
LQ Guru
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163
Rep:
|
might have left of something
it works on mine
|
|
|
07-16-2003, 03:42 PM
|
#6
|
LQ Guru
Registered: Feb 2003
Location: Sparta, NC USA
Distribution: Ubuntu 10.04
Posts: 5,141
Rep:
|
Don't forget the she in the shebang
|
|
|
07-16-2003, 04:45 PM
|
#7
|
LQ Newbie
Registered: Mar 2003
Location: Austin, TX
Posts: 6
Original Poster
Rep:
|
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
|
|
|
07-16-2003, 04:57 PM
|
#8
|
LQ Guru
Registered: Feb 2003
Location: Sparta, NC USA
Distribution: Ubuntu 10.04
Posts: 5,141
Rep:
|
touch tmp (create the file) and see if it works
|
|
|
07-16-2003, 05:04 PM
|
#9
|
LQ Newbie
Registered: Mar 2003
Location: Austin, TX
Posts: 6
Original Poster
Rep:
|
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.
|
|
|
07-16-2003, 05:16 PM
|
#10
|
LQ Guru
Registered: Feb 2003
Location: Sparta, NC USA
Distribution: Ubuntu 10.04
Posts: 5,141
Rep:
|
Perhaps you meant to store all the files in a directory /tmp or something??
|
|
|
07-16-2003, 05:32 PM
|
#11
|
LQ Newbie
Registered: Mar 2003
Location: Austin, TX
Posts: 6
Original Poster
Rep:
|
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?
|
|
|
07-16-2003, 07:03 PM
|
#12
|
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126
Rep:
|
Never use space character in filenames or bear the consequences...
|
|
|
07-16-2003, 07:19 PM
|
#13
|
LQ Guru
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163
Rep:
|
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
|
|
|
07-16-2003, 07:22 PM
|
#14
|
LQ Guru
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163
Rep:
|
you must use the double quotes around the text to add and the filter
like this
prepend "#!/usr/bin/php" "*.php"
Last edited by DavidPhillips; 07-16-2003 at 07:26 PM.
|
|
|
07-16-2003, 07:24 PM
|
#15
|
LQ Guru
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163
Rep:
|
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 07:58 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|