0

I have made a simple script to mount a DATA partition in order to fuel my distro hopping fever. I am not completely replacing /home with this partition, just replacing a few folders so I can have my files through multiple installs.

I was trying to streamline the code for other users but really don't have much experience. I am using a variable so a user can add their own name for the data partition. However, when it comes time to editing fstab it won't put in the correct name unless I reassign the variable within the if ... else statement. I will leave out my UUID to make it shorter. $name refers to the variable set at the beginning of the script $name=DATA

echo "Adding DATA part UUID to fstab"
if grep -Fxq "UUID=xxx /mnt/$name ext4 defaults,noatime 0 2" /etc/fstab; then
echo "Already in fstab"
else
name=DATA #If I dont put this line in then fstab is edited with '$name' instead of actually putting the variable in.
sudo echo "UUID=xxx /mnt/$name ext4 defaults,noatime 0 2" >> /etc/fstab
fi

This is my first post on Stack Exchange so please let me know if this is not formatted correctly.

AdminBee
  • 22,803
njakes
  • 1

1 Answers1

0

To re-use your code, there is no reason why this should not work.

#!/usr/bin/env bash

DEV_IDENTIFIER="UUID=xxx"
MOUNTPOINT="/mnt/data"

fstab_line="${DEV_IDENTIFIER} ${MOUNTPOINT} ext4 defaults,noatime 0 2"

echo "Adding DATA part UUID to fstab"
if grep -Fxq "${fstab_line}" /etc/fstab; then
  echo "Already in fstab"
else
  echo "${fstab_line}" | sudo tee -a /etc/fstab
fi
ckarles
  • 78
  • Thank you @ckarles very much for your quick reply. I was able to figure it by removing the sudo within the script and running the script as sudo. I can see that your implementation of that is a bit better than mine. If I post a link to the full script would you be willing to look it over and offer further advice? I think 115 lines is a bit long for here. – njakes Jan 28 '20 at 18:56
  • Sure, just add a link here in the comment (or just PM me). – ckarles Jan 29 '20 at 11:55
  • Here is the current code, there are some variations in there as I was trying new things to get it to work. https://github.com/njakes/bash-scripts/blob/master/test-asus-link-DATA-drive.sh – njakes Jan 29 '20 at 18:23