Tuesday, May 14, 2024
12
rated 0 times [  12] [ 0]  / answers: 1 / hits: 1676  / 2 Years ago, fri, february 4, 2022, 2:32:54

I need to add:



user_xattr,acl,barrier=1


to my /etc/fstab file. Is there a way to do this via the via shell script?



I only want to edit the '/' mount.



Here is what I have before I manually edit it:



UUID=eb287d10-60a8-4a9a-9148-5f907fc7a8be / ext4 errors=remount-ro 0 1


Here is what it looks like after I manually add the lines:



UUID=eb287d10-60a8-4a9a-9148-5f907fc7a8be / ext4 user_xattr,acl,barrier=1,errors=remount-ro 0 1


I know that the sed -i command will not work here because it only adds lines...



Also script be run on multiple computers so it has to be able to "know" that the UUID will be different.


More From » command-line

 Answers
0

Actually, sed could work perfectly well, but here are some more choices as well:



1. sed



sudo sed -i.old -r '/[ 	]/[ 	]/{s/(ext4[	 ]*)([^	 ]*)/12,user_xattr,acl,barrier=1/}' /etc/fstab


Explanation:




  • -i.old : Edit the file in place and create fstab.old as a backup of the original file before the changes.

  • -r : Enable extended regular expressions.

  • /[ ]/[ ]/{} : If this line matches a / surrounded by a space or tab (if this line describes the / mountpoint).

  • s/(ext4[ ]*)([^ ]*)/12,user_xattr,acl,barrier=1/ : s/pat/replacement/ is the substitution operator, it will replace pat with replacement. Here, we are matching ext4 and any following space or tab (needed to anchor the match, if your file systems are not ext4, you'll need to change that) and then capturing (that's what the parentheses do) the longest stretch of non-whitespace characters after that. In other words, capturing the options field of fstab. We then replace those with 1 (the first captured pattern), 2 (the 3nd capturef pattern, the original options) plus the extra options you wanted to add.



2. Perl



sudo perl -i.old -pane 's/$F[3]/$F[3],user_xattr,acl,barrier=1/ if $F[1] eq "/"' /etc/fstab


Explanation:




  • -i.old : Again, this will cause the file to be edited in place and a backup called fstab.old will be created.

  • -pane : -p means print each line, -a automatically splits input lines into fields on white space and saves them as the array @F. -n means read line by line and -e lets you pass a script on the command line.

  • The actual script will add the extra options to the current value of the 4th field ($F[3],1st field is $F[0]) only if the second ffield ($F[1], the mount point) is /.



3. awk



sudo cp /etc/fstab /etc/fstab.old && 
awk '($2=="/"){$4=$4",user_xattr,acl,barrier=1"}1;' /etc/fstab.old | sudo tee /etc/fstab


Explanation:



Most versions of awk don't allow in place editing so the first command will create a backup copy. The awk will then check if the 2nd field is / and if so, will add the required text to the 4th field, the options. The 1; is awk shorthand for "print the line". The sudo tee is just a trick to allow printing to /etc/fstab since simple redirection won't work with sudo.



4. pure bash



sudo cp /etc/fstab /etc.fstab.old
while read fs mp ty op du pa; do
[[ $mp = "/" ]] && op="user_xattr,acl,barrier=1,""$op";
printf "%s %s %s %s %s %s
" "$fs" "$mp" "$ty" "$op" "$du" "$pa";
done < /etc/fstab.old | sudo tee /etc/fstab


Explanation




  • while read fs mp ty op du pa; do ...; done < /etc/fstab : Read each fstab line and split into the relevant fields.

  • [[ $mp = "/" ]] && op="user_xattr,acl,barrier=1,""$op"; if the mount point is /, add the extra options to $op.

  • The printf just prints each line correctly.


[#26198] Saturday, February 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ainlyyor

Total Points: 210
Total Questions: 129
Total Answers: 116

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
;