Tôi đang cố gắng để tạo ra một tập tin kịch bản sử dụng chuỗi thay thế từ ENV mà còn muốn ngăn chặn một số từ thoátLàm thế nào để tránh các biến mở rộng heredoc?
export PLACEHOLDER1="myPlaceholder1Value"
export PLACEHOLDER2="myPlaceholder2Value"
sudo /bin/su -c "cat <<EOF> /etc/init.d/my-script
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: my-script
# Required-Start: \$remote_fs \$syslog
# Required-Stop: \$remote_fs \$syslog
# Should-Start: \$network \$time
# Should-Stop: \$network \$time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: blabla
# Description: bla bla desc
#
### END INIT INFO
#
myvariable_1=toto$PLACEHOLDER1
myvariable_2=titi$PLACEHOLDER2
myvariable_final=\"dynamicvar=\${myvariable_1},\${myvariable_2}\"
EOF
"
Nó là kết quả, trong đó là không tốt như myvariable_final không trốn thoát và thay thế như một từ phụ thuộc init script ($ remote_fs, $ syslog, $ mạng, $ thời gian)
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: my-script
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: blabla
# Description: bla bla desc
#
### END INIT INFO
#
myvariable_1=totomyPlaceholder1Value
myvariable_2=titimyPlaceholder2Value
myvariable_final="dynamicvar=,"
Nếu tôi cố gắng để đặt một dấu chéo ngược \
đằng sau dolla rs $
, tôi quản lý để tránh sự thay thế nhưng tôi nhận được một dấu chéo ngược không mong muốn \
:
export PLACEHOLDER1="myPlaceholder1Value"
export PLACEHOLDER2="myPlaceholder2Value"
sudo /bin/su -c "cat <<EOF> /etc/init.d/my-script
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: my-script
# Required-Start: \$\remote_fs \$\syslog
# Required-Stop: \$remote_fs \$syslog
# Should-Start: \$network \$time
# Should-Stop: \$network \$time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: blabla
# Description: bla bla desc
#
### END INIT INFO
#
myvariable_1=toto$PLACEHOLDER1
myvariable_2=titi$PLACEHOLDER2
myvariable_final=\"dynamicvar=\$\{myvariable_1},\$\{myvariable_2}\"
EOF
"
kết quả trong:
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: my-script
# Required-Start: $\remote_fs $\syslog
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: blabla
# Description: bla bla desc
#
### END INIT INFO
#
myvariable_1=totomyPlaceholder1Value
myvariable_2=titimyPlaceholder2Value
myvariable_final="dynamicvar=$\{myvariable_1},$\{myvariable_2}"
Wanted/kết quả tham dự whould đã:
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: my-script
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $network $time
# Should-Stop: $network $time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: blabla
# Description: bla bla desc
#
### END INIT INFO
#
myvariable_1=totomyPlaceholder1Value
myvariable_2=titimyPlaceholder2Value
myvariable_final="dynamicvar=${myvariable_1},${myvariable_2}"
được giải quyết bằng cách đặt báo giá xung quanh EOF như dưới đây và sử dụng dấu gạch chéo để kiểm soát thoát khi cần thiết
export PLACEHOLDER1="myPlaceholder1Value"
export PLACEHOLDER2="myPlaceholder2Value"
sudo /bin/su -c "cat << 'EOF' > /etc/init.d/my-script
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: my-script
# Required-Start: \$remote_fs \$syslog
# Required-Stop: \$remote_fs \$syslog
# Should-Start: \$network \$time
# Should-Stop: \$network \$time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: blabla
# Description: bla bla desc
#
### END INIT INFO
#
myvariable_1=toto$PLACEHOLDER1
myvariable_2=titi$PLACEHOLDER2
myvariable_final=\"dynamicvar=\${myvariable_1},\${myvariable_2}\"
EOF
"
Trích dẫn xung quanh 'EOF' đã thực hiện các mẹo như với thoát, tôi có thể kiểm soát những gì được thay thế và wnat không được thay thế – TheCodeKiller
@ TheCodeKiller đẹp! Nó khá phức tạp ... cho đến khi bạn biết điều đó :) Rất vui vì điều này hữu ích cho bạn! – fedorqui