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.

1

1 Answer 1

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
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.