2

所以我试图让 bash 中的期望正常工作。

这是脚本内容...

[root@mysql1 ~]# cat mysql_repl.sh
#!/bin/bash
read -p "Master server ip: " masterip
read -p "What is the regular user to log into the master server: " masteruser
read -p "What is the password for the regular user for the master server: " masterpass
read -p "What is the root password for the master server: " masterrootpass

read -p "Slave server ip: " slaveip
read -p "What is the regular user to log into the slave server: " slaveuser
read -p "What is the password for the regular user for the slave server: " slavepass
read -p "What is the root password for the slave server: " slaverootpass



expect -c "set slaveip $slaveip;\
set slaveuser $slaveuser;\
set slavepass $slavepass;\
set timeout -1;\
spawn /usr/bin/ssh $slaveip -l $slaveuser 'ls -lart';\
match_max 100000;
expect *password:;\
send -- $slavepass\r;\
interact;"

这是脚本的输出...

[root@mysql1 ~]# ./mysql_repl.sh
Master server ip:
What is the regular user to log into the master server:
What is the password for the regular user for the master server:
What is the root password for the master server:
Slave server ip: xxx.xxx.xxx.xxx
What is the regular user to log into the slave server: rack
What is the password for the regular user for the slave server: test
What is the root password for the slave server: DVJrPey99grJ
spawn /usr/bin/ssh 198.61.221.179 -l rack 'ls -lart'
rack@198.61.221.179's password:
bash: ls -lart: command not found

该命令未正确执行。我也试过 /bin/ls 还是找不到。

第二部分...相同的脚本...

我在 bash 中有一个变量,特别是密码。在这种情况下,密码是“as$5!@?” 我想要做的是遍历每个字符,测试它是否是一个特殊字符并逃脱它。那么例如...

pass=as$5!@?

到目前为止我所拥有的很接近但不适用于特殊字符,它适用于非特殊字符......

echo -n $pass | while read -n 1 c; do [[ "$c" = [!@#$%^&*().] ]] && echo -n "\\"; echo -n $c; done

有人对如何在每个特殊字符之前添加 \ 有想法吗?

希望能解决这个问题。

4

1 回答 1

2

在您的最后一个行块中,尝试在每个需要转义的特殊字符之前手动添加一个 \。这应该不是很多工作。

此外,使用 == 进行相等检查,即:

echo -n $pass | while read -n 1 c; do [[ "$c" == [!@#$%^&*().] ]] && echo -n "\\"; echo -n $c; done
于 2012-10-17T05:44:48.810 回答