Problems inserting commands into bashrc via script

Asked

Viewed 121 times

0

I am trying to insert commands in bashrc via script.

I turn the remote

source powerpyenv.sh

# powerpyenv.sh
echo '### Added by pyenv' >> teste
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> teste
echo 'eval "$(pyenv init -)"' >> teste
echo 'eval "$(pyenv virtualenv-init -)"' >> teste

cat << EOF > temp.txt
### Activate virtualenv
alias manage="python $VIRTUAL_ENV/../manage.py"
alias r="manage runserver"
alias sa='source .venv/bin/activate; PS1="(`basename \"$VIRTUAL_ENV\"`)\e[1;34m:/\W\e[00m$ "; clear'

### Short prompt
alias pa='PS1="(`basename \"$VIRTUAL_ENV\"`)\e[1;34m:/\W\e[00m$ "; clear'
alias p='PS1="\e[1;34m/\W\e[00m$ "; clear'

alias rm="rm -i"
EOF

cat temp.txt >> teste
rm -f temp.txt

source teste

echo "Done"

I’ve played it all in one test for now.

See the test result:

### Added by pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
### Activate virtualenv
alias manage="python /../manage.py"
alias r="manage runserver"
alias sa='source .venv/bin/activate; PS1="("")\e[1;34m:/\W\e[00m$ "; clear'

### Short prompt
alias pa='PS1="("")\e[1;34m:/\W\e[00m$ "; clear'
alias p='PS1="\e[1;34m/\W\e[00m$ "; clear'

alias rm="rm -i"

What are the problems here?

  1. aliasManage looked different. You need to see how to insert the literal $VIRTUAL_ENV.
  2. alias was different. Both because of $VIRTUAL_ENV and because of the crase and basename.

Good how to fix it?

1 answer

1


Your command cat <<EOF will use shell expansion whenever you find subprocess -- ``, variable -- $, among others. If you want to include them literally in the file it is necessary to escape them, for example:

cat > temp.txt <<EOF
### Activate virtualenv
alias manage="python \$VIRTUAL_ENV/../manage.py"
...
EOF

Browser other questions tagged

You are not signed in. Login or sign up in order to post.