Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5056

Difficulty using readlink in bash with mix of variables and string to return absolute paths

$
0
0

I have a config script where users can specify paths as variables in the header section. I want them to be able to use absolute paths, relative paths and variables (because this is actually called from another shell script from where they get the values for the variables). At the end of the script all the paths are written to a text file.

The challenge I have is that variables used within some of the paths can change in the middle of the script. I am having difficulties in re-evaluating the path to get the correct output.

### HEADER SECTION ###DIR_PATH="$VAR1/STRING1"InputDir_DEFAULT="$DIR_PATH/Input"### END HEADER ###...some codeif [[ some condition ]]; then DIR_PATH="$VAR2/STRING2"; fi...more code# $InputDir_DEFAULT needs re-evaluating hereInputDir=$(readlink -m $InputDir_DEFAULT)echo $InputDir >> $FILE

When I do as above and 'some condition' is met, the return of 'echo' is the absolute path for $VAR1/STRING1/Input, whereas what I want it the abs path for $VAR2/STRING2/Input.

Below is an alternative, where I try to stop InputDir_DEFAULT being evaluated until the end by storing itself as a string.

### HEADER SECTION ###DIR_PATH="$VAR1/STRING1"InputDir_DEFAULT='$DIR_PATH/Input' #NOTE: "" marks have changed to ''### END HEADER ###if [[ some condition ]]; then DIR_PATH="$VAR2/STRING2"; fiSTRING_TMP=$InputDir_DEFAULTInputDir=$(readlink -m $STRING_TMP)echo $InputDir >> $FILE

This time 'echo' returns a mix of the evaluated variables and un-evaluated string: $VAR2/STRING2/$DIR_PATH/Input which (for me) looks like /home/ubuntu/STRING2/$DIR_PATH/Input. It's just the $DIR_PATH/ that shouldn't be there.

This feels like it should be relatively straightforward. I'm hoping I'm on the right path and that it's my use of "" and '' that's at fault. But I've tried lots of variations with no success.


Viewing all articles
Browse latest Browse all 5056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>