bash 除了脚本错误 - 执行时关闭引号后的额外字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26999068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:48:41  来源:igfitidea点击:

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

bashshellscriptingshexcept

提问by Nikhil Mehta

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.

我写了一个脚本来 ssh 到不同的服务器并更改 .bash_profiles。在我的代码中,我遇到了 sed 命令的问题并且出现了以下错误。

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

执行“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.

操作系统 - 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

回答by Nikhil Mehta

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