LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 04-24-2010, 01:14 PM   #1
technocp
LQ Newbie
 
Registered: Oct 2008
Posts: 18

Rep: Reputation: 0
awk and sed help for script


hello friends,
I am creating a script for making a public folder in linux. The idea behind is to allow any user to write files in this folder and further each of those files can be used by any user.

for this purpose I am using acl. This first requires to set acl as mount option in /etc/fstab which I need to do with help of (awk and sed) command available in linux.

the line below is the fstab entry which I need to make change in.

UUID=e9a3db4b-d8c0-40b4-a661-131e13afea1e / ext3 relatime,errors=remount-ro 0 1

I want my script to identify this line (most probably with / which denotes the mountpoint) and then add (acl,) without brackets in options

currently I use the command as below
sed 's/realtime/acl,relatime/' /etc/fstab

but the above command is in reference with (relatime) what I am planning is that my script should be able to find the line which has / and then go to options and add acl there.

can anyone please help me for this purpose
 
Old 04-24-2010, 02:53 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
I suggest a solution without sed or awk:
Code:
while read line
do
  mp=$(echo $line | cut -d' ' -f2)
  if [[ "$mp" == "/" ]]
  then
    op=$(echo $line | cut -d' ' -f4)
    line=${line/$op/acl,$op}
  fi
  echo "$line"
done < /etc/fstab
Indeed awk would have the advantage to use a more compact and elegant code, but there is not a simple way to preserve the original (multiple) spacing of /etc/fstab (the code above preserve it). On the other hand sed has the ability to edit the file in place, without going through temporary files. It is a matter of choice at this point..
 
Old 04-24-2010, 10:40 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,168

Rep: Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775
I would caution using / as your identifier as remember you may have /usr or /home lines as well.
For your sed you can add a search to the fron of your change:
Code:
sed '/<something on line your looking for>/s/realtime/acl,relatime/' /etc/fstab
This will tell sed to only make a change to the line you find.

awk on the other hand would be a little nicer as the whole field will equal just / and if you make
your change to the line and not the field you can maintain the formatting:
Code:
awk '$2=="/"{sub(/realtime/,"acl,&")}1' /etc/fstab
 
Old 05-16-2010, 12:14 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,168

Rep: Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775Reputation: 1775
Please mark as SOLVED, unless you still have questions.
 
  


Reply

Tags
acl, awk, fstab, sed


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script using sed or awk? houms Linux - Newbie 10 06-21-2009 09:14 AM
awk sed shell script slcalice Programming 3 03-09-2009 05:40 AM
Help with BASH script and AWK and SED NickJH Linux - Newbie 11 03-07-2009 04:08 PM
need help with a script that involves awk and sed LinuxLala Programming 2 09-06-2007 09:10 AM
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM


All times are GMT -5. The time now is 02:36 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration