LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-04-2008, 07:47 AM   #1
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Rep: Reputation: 15
SED .htaccess script


Hey im searching for a SED script that rewrites every .htaccess file from /home folders and set something in .htaccess to the ownership of the folder.

Any help/link/tips?

grtz
 
Old 06-04-2008, 08:14 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This is much more that just a SED script. If I understand you correctly, you will need to parse the ls -l listing, store the name of the file owner, and then write that to the file.

Can you give an example of where the info should be stored in the file---or should it just get appended?
 
Old 06-04-2008, 08:17 AM   #3
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
yep thats exactly what i mean.

For example all .htaccess files in /home excist of the following line:
Quote:
AuthType Basic
AuthName "Give in Kerb 5 Username and Password Required active directory"
Require valid-user
Where Require valid-user needs to set too
Require user "fileowner"
For example Require user tom
 
Old 06-04-2008, 08:31 AM   #4
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
this is what i came up so far. Buggy!

for .htaccess in /home/*
ls -l > $owner
do
sed -i "s@Require valid-user @Require user $owner@g" $.htaccess
done
 
Old 06-04-2008, 08:34 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
OK:

In the script, do ls -l and extract the owner field to a variable (tools like awk or cut will do the extraction). Example:

owner=`ls -l filename|cut -d " " -f3`

Then use SED to parse the file and substitute the contents of the variable into the "Require" line.
 
Old 06-04-2008, 08:37 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by zerocool22 View Post
this is what i came up so far. Buggy!

for .htaccess in /home/*
ls -l > $owner
do
sed -i "s@Require valid-user @Require user $owner@g" $.htaccess
done
No....

You can't use ">" to write to a variable (it writes to a file)

You don't want all of the ls -l, just the owner field.

The SED statement looks like you are on the right track....
 
Old 06-04-2008, 08:38 AM   #7
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
Thx! so far i got:

for .htaccess in /home/*
$owner=`ls -l filename|cut -d " " -f3`
do
sed -i "s@Require valid-user @Require user $owner@g" $.htaccess
done
 
Old 06-04-2008, 08:49 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You don't say if it works...(but it wont)

The syntax for the "for" loop is:

for <something> ; do
<stuff>
done

Hint: First, run the individual commands on one file--to be sure that they do what you want. Then write the script.

Also, this does not look right:
for .htaccess in /home/*
This will find every file in /home and assign it to the variable ".htaccess". Is ".htaccess" an extension? If so, you need something like:
for file in /home/*.htaccess
 
Old 06-04-2008, 08:52 AM   #9
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
oh ok thx, no .htaccess is just a file that restricts access for apache users. SO i mean every .htaccess file in every folder in home?? so not sure what to change it too

#!/bin/bash
for $.htaccess in /home/*;
do
$owner=`ls -l home$i|cut -d " " -f3`
sed -i "s@Require valid-user @Require user $owner@g" $.htaccess
done
 
Old 06-04-2008, 02:06 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
In this construct:
for name in <something>

"name" is a variable to which you are assigning the result of <something>. Suppose I do "ls" to list the contents of a directory.
for name in ls
As I go thru this loop, the variable "name" is given the value of each entry in the "ls" listing.

So---I think you need:
for name in /home/*/.htaccess

Then use $name in the later statements, as required.

What texts are you using for scripting? I recommend starting with the Bash Guide for Beginners by Machtelt Garrels. It's free at http://tldp.org
 
Old 06-05-2008, 02:06 AM   #11
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
yeah im using bash script at the moment. Thx for the info
I got some errors?
./sed.sh: line 1: me: command not found
./sed.sh: line 2: =: command not found
./sed.sh: line 3: syntax error near unexpected token `do'
./sed.sh: line 3: `do'

#!/bin/bash
for name in /home/*/.htaccess
$owner=`ls -l /home$i|cut -d " " -f5`
do
sed -i "s@Require valid-user @Require user $owner@g" $name
done

Last edited by zerocool22; 06-05-2008 at 02:22 AM.
 
Old 06-05-2008, 03:04 AM   #12
zerocool22
Member
 
Registered: Feb 2008
Posts: 95

Original Poster
Rep: Reputation: 15
ok my bad, i fixed it
thx!
 
  


Reply



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
Sed script help genderbender Programming 4 05-04-2008 04:38 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed script cranium2004 Programming 4 06-21-2006 09:33 AM
sed script glam1 Programming 2 08-31-2004 11:28 AM
sed script help/ideas rjcrews Programming 2 07-30-2004 02:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 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