[SOLVED] Unable to edit fstab with sed to edit mount options
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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 !!)
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) !!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.