user1601716
user1601716

Reputation: 1993

expect in bash script with special characters 2 parts

So I am trying to get expect within bash to work correctly.

Here is the script contents...

[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;"

Here is the output of the script...

[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'
[email protected]'s password:
bash: ls -lart: command not found

The command is not executing correctly. I also tried /bin/ls and it still can not find it.

Second Part... same script...

I have a variable in bash, specifically, a password. In this case the password is "as$5!@?" What I want to do is go through each character, test if it is a special character and escape out of it. So for instance...

pass=as$5!@?

What I have so far is close but not working for special characters, it will work for non-special...

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

Anyone have thoughts on how I can add a \ before each special character?

Hope that clears up the question.

Upvotes: 2

Views: 3905

Answers (1)

sampson-chen
sampson-chen

Reputation: 47377

In your last line block, try manually adding a \ before each special character that needs to be escaped. That shouldn't be much work.

Also, use == for the equality check, i.e.:

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

Upvotes: 2

Related Questions