A challenging Linux question (advanced scripting related)
-
I'm studing for RHCSA exam at the moment. One of the topics there is to add mounts to the fstab file. Because I won't be able to copy and paste with my mouse I would have to use a command to copy the UUID to the file. I've come up with a command:
blkid -p /dev/nvme0n1p2 | awk '{ print $2 }' | sed 's/"//g' >> /etc/fstab
That's pretty much enough for me for the test. However I wanted to take it one step further (love challenges) and type the whole line without editing the file. So the command above will only append the UUID of the partition that I need to fstab, but I also want to add the: /mnt xfs defaults 0 0 parameters for example in the same command and can't find an elegant way to do that. (Don, Daniel? wink wink ) Thanks for the help :) -
I find that 9 times out of 10 sed and awk aren't necessary (which is good because sed and awk are hard). In this case, I would try to leverage some of the options in blkid to make it easier to get what you want. Try this out and see if it meets your needs:
echo "$(blkid /dev/sda1 -o value -s UUID) /mnt/Storage xfs defaults 0 0" >> /etc/fstab
In that example, I use blkid's filters to only return values for the drive I want that are tagged for the UUID field. Next, I use Bash's command expansion to build a string suitable to append to /etc/fstab. The result is a pretty straightforward command that doesn't require a lot of CLI-Jedi tricks to accomplish.
On a side note, remember that command expansion can behave weird when used in scripts. Running it on-off like you described will be fine, but in a script we would need to do the expansion in a variable first.
Hope that helps,
Don