except script error - extra characters after close-quote while executing

5.9k views Asked by At

I have a written an except script to ssh to different servers and change .bash_profiles. In my code, i am facing issue with sed command and getting below error.

extra characters after close-quote while executing "send "sed -i '/TERM/a \COL_YELLOW="$(tput setaf 3)"' .bash_profile_backup\r" expect eof"

I tried with all the combination, but its not working. I know its a small indentation issue. If someone could please help with the code.

OS - RHEL6.2.

Code

for server in $(cat temp.lst);
do
{
expect -c "
    spawn ssh -t -t "nmehta\@$server"
    expect "?assword:"
    send \"$password\r\"
    send \"\r\"
    expect "?nmehta@?"
    send \"sudo -u oracle -i\r\"
    sleep 5
    expect "?assword:"
    send \"$password\r\"
    sleep 5
    send \"cp .bash_profile .bash_profile_backup\r\"
    send \"sed -i.bck 's/vt100/xterm/g' .bash_profile_backup\r\"
    send \"sed -i '/TERM/a \COL_YELLOW=\"\$(tput setaf 3)\"' .bash_profile_backup\r\"
    send \"sed -i '/TERM/a \COL_END="$(tput sgr0 1)"' .bash_profile_backup\r\"
    expect eof"
}
done
1

There are 1 answers

0
Nikhil Mehta On

Trying different indentation I finally was able to execute the script as below.

PATH=$PATH:$ORACLE_HOME/bin:/usr/bin
export PATH

password=test123
export password

export TEMP_1='\"\$(tput setaf 3)\"'
export TEMP_1

export TEMP_2='\"\$(tput sgr0 1)\"'
export TEMP_2

TEMP_3="PS1='\''\\\${HOSTNAME} \[\\\$ORACLE_SID\] \\\$PWD> '\''"
export TEMP_3

for server in $(cat temp.lst);
do
{
expect -c "
     spawn ssh -t -t "nmehta\@$server"
    expect "?assword:"
    send \"$password\r\"
    send \"\r\"
    expect "?nmehta@?"
    send \"sudo -u oracle -i\r\"
    sleep 5
    expect "?assword:"
    send \"$password\r\"
    sleep 5
    send \"cp .bash_profile .bash_profile_backup\r\"
    send \"sed -i.bck s/vt100/xterm/g .bash_profile_backup\r\"
    send \"sed -i '/TERM/a \COL_YELLOW=$TEMP_1' .bash_profile_backup\r\"
    send \"sed -i '/TERM/a \COL_END=$TEMP_2' .bash_profile_backup\r\"
    expect eof"
}
done