IllinoisRoboticsInSpace / IRIS

Illinois Robotics in Space 2022-2023 Season
https://iris.ae.illinois.edu/
8 stars 0 forks source link

Check in setup scripts if .bashrc already has a command in it #12

Open ttchalakov opened 9 months ago

ttchalakov commented 9 months ago

One very convenient improvement that could be made to the setup scripts for both the Jetson Nano and the PC development environments is to make them reusable. Currently, each setup script should only be run once on their target platform. While there are multiple things that would need to be done to make everything reusable in the scripts, the first thing is to deal with the following problem caused by adding code to a user's .bashrc:

echo "export CUDA_HOME=/usr/local/cuda" >> ~/.bashrc

This command appends export CUDA_HOME=/usr/local/cuda to the end end of the user's .bashrc. If the script is run multiple times then this line in the setup script will add another copy of the command to the end of the .bashrc script. Therefore we wish to check if the command already exists and if it does we should remove the old version and place append the new version.

To implement this feature, it would be best to create a bash function with parameters so that the code can be reused. An example implementation is outlined below:

INPUT: command, replace_flag, replace_pattern
IF replace_flag is true and replace_pattern is in .bashrc:
    replace line that contains replace_pattern with command
    exit function
ENDIF
    append command to end of .bashrc

sed is a command that allows you to perform basic text transformations using regular expressions. Google tutorials to learn about it or stack overflow will have code for your specific operation.