LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-16-2003, 12:39 PM   #1
bobdinkel
LQ Newbie
 
Registered: Mar 2003
Location: Austin, TX
Posts: 6

Rep: Reputation: 0
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 ).
 
Old 07-16-2003, 02:16 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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.
 
Old 07-16-2003, 02:29 PM   #3
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
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.
 
Old 07-16-2003, 02:49 PM   #4
bobdinkel
LQ Newbie
 
Registered: Mar 2003
Location: Austin, TX
Posts: 6

Original Poster
Rep: Reputation: 0
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...
 
Old 07-16-2003, 03:27 PM   #5
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
might have left of something
it works on mine
 
Old 07-16-2003, 03:42 PM   #6
fancypiper
LQ Guru
 
Registered: Feb 2003
Location: Sparta, NC USA
Distribution: Ubuntu 10.04
Posts: 5,141

Rep: Reputation: 60
Don't forget the she in the shebang
 
Old 07-16-2003, 04:45 PM   #7
bobdinkel
LQ Newbie
 
Registered: Mar 2003
Location: Austin, TX
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 07-16-2003, 04:57 PM   #8
fancypiper
LQ Guru
 
Registered: Feb 2003
Location: Sparta, NC USA
Distribution: Ubuntu 10.04
Posts: 5,141

Rep: Reputation: 60
touch tmp (create the file) and see if it works
 
Old 07-16-2003, 05:04 PM   #9
bobdinkel
LQ Newbie
 
Registered: Mar 2003
Location: Austin, TX
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 07-16-2003, 05:16 PM   #10
fancypiper
LQ Guru
 
Registered: Feb 2003
Location: Sparta, NC USA
Distribution: Ubuntu 10.04
Posts: 5,141

Rep: Reputation: 60
Perhaps you meant to store all the files in a directory /tmp or something??
 
Old 07-16-2003, 05:32 PM   #11
bobdinkel
LQ Newbie
 
Registered: Mar 2003
Location: Austin, TX
Posts: 6

Original Poster
Rep: Reputation: 0
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?
 
Old 07-16-2003, 07:03 PM   #12
J_Szucs
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: Reputation: 58
Never use space character in filenames or bear the consequences...
 
Old 07-16-2003, 07:19 PM   #13
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
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
 
Old 07-16-2003, 07:22 PM   #14
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
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.
 
Old 07-16-2003, 07:24 PM   #15
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
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
 
  


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
bash: <command name> command not found smash Programming 5 03-13-2006 09:48 AM
The ! bash command. How do I use it? funkynesh Linux - Newbie 2 10-12-2005 09:36 PM
bash: command not found intels_ss Linux - Newbie 5 07-20-2005 08:41 PM
-bash: ls: command not found jchun Linux - General 3 11-02-2004 11:11 AM
bash command question BajaNick Linux - Software 8 10-05-2003 08:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:58 AM.

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