LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-14-2019, 06:04 PM   #16
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 420

Original Poster
Blog Entries: 1

Rep: Reputation: 202Reputation: 202Reputation: 202

Thanks a lot for your help and direction.
Currently, I have settled for this, though it is not perfect.

Original File
Code:
# Commented Line followed by empty line

/dev/mapper/vg_root-lv_root     /       ext4    defaults 1 2
UUID=STRING1       /boot   ext4    defaults 1 2
/dev/mapper/vg_root-lv_home     /home   ext4    defaults 1 2
/dev/mapper/vg_root-lv_opt      /opt    ext4    defaults 1 2
# Commented Line followed by empty line

/dev/mapper/vg_root-lv_tmp      /tmp    ext4    defaults,noexec         1       2
/dev/mapper/vg_root-lv_usr      /usr    ext4    defaults 1 2
/dev/mapper/vg_root-lv_var      /var    ext4    defaults 1 2
/dev/mapper/vg_root-lv_swap     swap    swap    defaults 1 2
tmpfs   /dev/shm        tmpfs   nodev,nosuid,noexec     0       0
devpts  /dev/pts        devpts  STRING2  0       0
sysfs   /sys    sysfs   defaults 1 2
proc    /proc   proc    defaults 1 2
/tmp    /var/tmp        none    bind,nodev,nosuid,noexec    0      0
/dest1  /MP1    ext4    defaults,nodev 1 2
/dest2         /MP2      ext3    noexec 1 2
# Commented Line followed by empty line

/dest3           /MP3                  ext4    defaults        1       2
script
Code:
 cat /tmp/fstab_bk | while read line;do if [[ $line == *"vg_root"*  ||  $line == *"UUID"* ||  $line == "^#"* ||  $line == *"nosuid"* || $line == *"proc*"  || $line == *"sysfs"*  ||  $line == *"devpts"* ]]; then echo "$line" >> fstab_new; else echo "$line" | awk ' /^$/ { next }{ $4 = $4 ",nosuid"; print $0 }' >> fstab_new ;fi;done;
Output

Code:
# Commented Line followed,nosuid by empty line
/dev/mapper/vg_root-lv_root     /       ext4    defaults 1 2
UUID=STRING1       /boot   ext4    defaults 1 2
/dev/mapper/vg_root-lv_home     /home   ext4    defaults 1 2
/dev/mapper/vg_root-lv_opt      /opt    ext4    defaults 1 2
# Commented Line followed,nosuid by empty line
/dev/mapper/vg_root-lv_tmp      /tmp    ext4    defaults,noexec         1       2
/dev/mapper/vg_root-lv_usr      /usr    ext4    defaults 1 2
/dev/mapper/vg_root-lv_var      /var    ext4    defaults 1 2
/dev/mapper/vg_root-lv_swap     swap    swap    defaults 1 2
tmpfs   /dev/shm        tmpfs   nodev,nosuid,noexec     0       0
devpts  /dev/pts        devpts  STRING2  0       0
sysfs   /sys    sysfs   defaults 1 2
proc /proc proc defaults,nosuid 1 2
/tmp    /var/tmp        none    bind,nodev,nosuid,noexec    0      0
/dest1 /MP1 ext4 defaults,nodev,nosuid 1 2
/dest2 /MP2 ext3 noexec,nosuid 1 2
# Commented Line followed,nosuid by empty line
/dest3 /MP3 ext4 defaults,nosuid 1 2
This is very close to what I am looking for. Now if it can just conserve the empty lines and leave the commented lines alone (without adding nosuid to them), it'd be awesome.
 
Old 03-15-2019, 02:15 PM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,807

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
More elegant is a case-esac (I also fixed your *"proc*" and "^#" and required "nosuid" in field#4 and others at the begining of the line),
and redirecting the loop's input and output
Code:
while read line; do
  case $line in
  ( *"vg_root"* | "UUID"* | "#"* | "proc"* | "sysfs"* | "devpts"* )
    echo "$line"
  ;;
  ( * )
    echo "$line" | awk 'NF==0 { next } $4!~/nosuid/ { $4 = $4 ",nosuid" } { print }'
  esac
done < /tmp/fstab_bk > fstab.new

Last edited by MadeInGermany; 03-15-2019 at 02:54 PM.
 
Old 03-16-2019, 03:56 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
Quote:
Originally Posted by MadeInGermany View Post
More elegant is a case-esac (I also fixed your *"proc*" and "^#" and required "nosuid" in field#4 and others at the begining of the line),
and redirecting the loop's input and output
And just because I do not like to implement something using two languages I suggest you to use either awk or shell, but not both.

you can use something like this (if you don't like awk):
Code:
while read -r line; do
  case $line in
  ( *"vg_root"* | "UUID"* | "#"* | "proc"* | "sysfs"* | "devpts"* | *nosuid* )
  ;;
  ( * )
    line=${line/defaults/defaults,nosuid}
  esac
  echo $line
done < /tmp/fstab_bk > fstab.new
 
1 members found this post helpful.
Old 03-19-2019, 03:58 AM   #19
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 420

Original Poster
Blog Entries: 1

Rep: Reputation: 202Reputation: 202Reputation: 202
Quote:
Originally Posted by MadeInGermany View Post
More elegant is a case-esac (I also fixed your *"proc*" and "^#" and required "nosuid" in field#4 and others at the begining of the line),
and redirecting the loop's input and output
Code:
while read line; do
  case $line in
  ( *"vg_root"* | "UUID"* | "#"* | "proc"* | "sysfs"* | "devpts"* )
    echo "$line"
  ;;
  ( * )
    echo "$line" | awk 'NF==0 { next } $4!~/nosuid/ { $4 = $4 ",nosuid" } { print }'
  esac
done < /tmp/fstab_bk > fstab.new
Thanks for the suggestion. Yes, it looks cleaner, though it removes empty lines. (That'd be a bug vs feature debate !!)
 
Old 03-19-2019, 04:58 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
so you need to add a line to print them too.
 
Old 03-19-2019, 06:25 AM   #21
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 420

Original Poster
Blog Entries: 1

Rep: Reputation: 202Reputation: 202Reputation: 202
@pan64,

Yes, but a residue of Sun Solaris Administration tells me that blank lines in /etc/passwd or /etc/shadow file is not a good practice anyway.
I remember a Solaris 6 (yes, 5.6) server becoming unbootable bcoz someone thought it was a bright idea to add a blank line in topmost position in /etc/shadow (i.e. over the root entry) !!

So I'll call it a feature
 
Old 03-19-2019, 07:09 AM   #22
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
yes, that is passwd and shadow files. But /etc/fstab may contain empty lines and comments too. Never mind, it is your decision. From my side it is ok.
 
Old 03-21-2019, 11:11 AM   #23
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,807

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
I saw your
Code:
 /^$/ { next }
So I thought you intended to delete blank lines and put
Code:
 NF==0 { next }
Meaning: if NumberFields == 0 then skip the following code and jump to the next cycle (next input line).
Simply omit it, and blank lines are printed!
 
Old 03-21-2019, 11:21 AM   #24
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 420

Original Poster
Blog Entries: 1

Rep: Reputation: 202Reputation: 202Reputation: 202
Thanks @MadeInGermany, yes, I am aware of that as I had to explain another person what the code was doing. I hope my awk is slightly better now.

This snippet code is part of a function which handles text files containing delimiter separated fields. It's no concern if a blank line gets deleted as long as commented lines don't get messed up.
 
  


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
[SOLVED] SED Multiline edit -- Quit After First Match/Edit RandyTech Linux - Newbie 9 06-09-2014 03:15 AM
[SOLVED] Help Edit the /etc/fstab specifically the dump and fsck options jollibee Linux - Newbie 3 07-16-2009 11:30 PM
Why unable to mount my nas in fstab get "mount error(5) Input/output error fi5ban Linux - Networking 0 04-01-2009 10:42 AM
fstab problem: mount: can't find dvd in /etc/fstab or /etc/mtab Nikon01 Slackware 5 11-17-2006 06:15 AM

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

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