Skip to content

Commit 02ab25d

Browse files
Fix saving environment variables with quoting
1 parent dd4c2c6 commit 02ab25d

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

sagemaker_ssh_helper/sm-save-env

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,31 @@
44

55
set -e
66

7+
if [[ "$1" == "print-vars" ]]; then
8+
# Wrap all vars into single quotes, wrap single quotes into double quotes
9+
# Also works for multi-line variables
10+
# A=test -> A='test'
11+
# B='test' -> B=''"'"'test'"'"''
12+
# C="test" -> C='"test"'
13+
# D=te -> D='te
14+
# st st'
15+
shift
16+
for arg in "$@"; do
17+
key=$(echo "$arg" | perl -0777 -pe 's/^([^=]*)=?.*$/\1/s')
18+
value=$(echo "$arg" | perl -0777 -pe 's/^[^=]*=?(.*)$/\1/s' | perl -0777 -pe s/\'/\'\"\'\"\'/gs)
19+
20+
if [[ $key =~ ^(BASH_FUNC_.*|HOME|USER|MAIL|LC_ALL|LS_COLORS|LANG|HOSTNAME|PWD|TERM|SHLVL|LANGUAGE|_)$ ]]; then
21+
: # skip vars that can cause problems
22+
else
23+
echo "$key='$value'"
24+
fi
25+
26+
done
27+
exit 0
28+
fi
29+
730
export START_SSH=false
8-
env | grep -E -v "^(HOME=|USER=|MAIL=|LC_ALL=|LS_COLORS=|LANG=|HOSTNAME=|PWD=|TERM=|SHLVL=|LANGUAGE=|_=)" \
9-
> /etc/environment
10-
cat /etc/environment
31+
env -0 | xargs -0 sm-save-env print-vars > /etc/environment
1132

12-
# FIXME: needs quoting of spaces, newlines and other special characters like other quotes
13-
# E.g., 'apt-get install stress' doesn't work correctly:
14-
# /usr/sbin/update-info-dir: 13: /etc/environment: cannot open 419: No such file
15-
# NVIDIA_REQUIRE_CUDA=cuda>=11.1 brand=tesla,driver>=418,driver<419 brand=tesla,driver>=450,driver<451
33+
echo "sm-save-env: Dumping environment"
34+
cat /etc/environment

0 commit comments

Comments
 (0)