LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 07:04 PM   #16
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 409

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, 03:15 PM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,857

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
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 03:54 PM.
 
Old 03-16-2019, 04:56 AM   #18
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,213

Rep: Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825
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, 04:58 AM   #19
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 409

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, 05:58 AM   #20
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,213

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

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, 08:09 AM   #22
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 17,213

Rep: Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825Reputation: 5825
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, 12:11 PM   #23
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,857

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
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, 12:21 PM   #24
Honest Abe
Member
 
Registered: May 2018
Distribution: CentOS 7, OpenSUSE 15
Posts: 409

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


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

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

All times are GMT -5. The time now is 06:28 PM.

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