1

So I currently have a bash script using expect that needs to send an array of commands into a console. As can be seen below I use IFS to divide these on the newline and I then put them into a variable cmds_eval with the expect script. My hope was then to use $cmds_eval as seen below to send a bunch of commands into the expect script all at once. This doesn't work. It seems $cmds_eval is not being interpreted the way I was thinking it might. What would be the appropriate way to do this? I don't believe there is anyway to do the foreach inside the expect script but if there is... The script below has been simplified of course...

  cmds_eval=""                                                                  

  OLDIFS=$IFS                                                                   
  IFS="\n" read -ra CMDS <<< "$cmds"                                            
  for c in "${CMDS[@]}"; do                                                     
    cmds_eval+="send -- \"$c\\r\"\r\n"                                          
    cmds_eval+="expect \"*myprompt*\"\n"                                   
  done                                                                          
  IFS=$OLDIFS                                                                   

  expect <<- DONE                                                               
    spawn my ssh session                                                     
    expect "*foobar:*"                                                        
    send -- "foobar\r"                                                          
    expect "*foobar:*"                                                        

    $cmds_eval                                                                  

    send -- "foobar\r" 
DONE

cmds looks something like this:

cmds="pwd
mv myfile ..
ls"
2
  • Can you show the earlier parts of the script and the value of $cmds? Commented Sep 22, 2013 at 4:09
  • 1
    I've added an example of what cmds looks like... It in fact goes through a lot more than just that but that should be good enough... Commented Sep 22, 2013 at 4:12

1 Answer 1

2

I think your IFS does not set itself to a true newline:

IFS="\n" read -ra CMDS <<< "$cmds"

should be

IFS=$'\n' read -ra CMDS <<< "$cmds"

Also perhaps prefer readarray if possible:

readarray -t CMDS <<< "$cmds"
for c in "${CMDS[@]}"; do
  cmds_eval+="send -- \"$c\\r\"\r\n"
  cmds_eval+="expect \"*myprompt*\"\n"
done

Instead of

OLDIFS=$IFS
IFS="\n" read -ra CMDS <<< "$cmds"
for c in "${CMDS[@]}"; do
  cmds_eval+="send -- \"$c\\r\"\r\n"
  cmds_eval+="expect \"*myprompt*\"\n"
done
IFS=$OLDIFS

Also if you want to separate your lines with newlines, don't use "\n". Instead use $'\n':

  cmds_eval+="send -- \"$c\\r\"$'\n'
  cmds_eval+="expect \"*myprompt*\"$'\n'
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, works. Note to other people doing this. Ensure if you pass cmds from elsewhere you do "$cmds" otherwise the spaces make $cmds into part of the arg vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.