It works good for most of the cases. The only corner case that it does not work is that if you have a file that already contains "\$" (not just "$", already escaped). For example:sub resolve_envs { my ($path) = @_; while ($path =~ m/[^\\]\$\{?(\w+)\}?/) { my $env = $1; if (exists($ENV{$env})) { $path =~ s/([^\\])\$\{?(\w+)\}?/$1$ENV{$env}/; } else { $path =~ s/([^\\])\$\{?(\w+)\}?/$1\\\$$env/; } } $path =~ s/\\\$/\$/g; return $path; }
For the first one it works, but for the second one it does not because I remove the extra backslashes that I added so you get ${playground}'/fi$le2. Actually it won't work for any escaped chars. Any ideas how can I handle this corner case?set playground="${HOME}/playground" set file1=${playground}'/fi$le1' set file2=${playground}'/fi\$le2' mkdir -p ${playground} touch ${file1} touch ${file2}
In reply to Re^2: How to replace envs in path?
by ovedpo15
in thread How to replace envs in path?
by ovedpo15
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |