0

I am attempting to write a script to run a script, but before I run the script I want to check if it is dos or unix format and convert it accordingly. Here is what I have so far:

spawn ssh root@login
expect "assword:"
send "myPass\r"
expect "#"
send "cd /myDir\r"
expect "#"

if head -1 *.sh | grep $'[\x0D]' >/dev/null  #check to see if dos or unix
   then   #if dos then convert to unix and change permissions
    dos2unix *.sh
    chmod 777 *.sh
   else   #else execute script
    ./*.sh
fi

expect "#"

Am I allowed to include if statement in the middle of the expect script like this? Also how can I execute multiple command after the if statement( the dos2unix and chmod).

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
tiger_groove
  • 956
  • 2
  • 17
  • 46
  • possible duplicate of [Using conditional statements inside 'expect'](http://stackoverflow.com/questions/1538444/using-conditional-statements-inside-expect) – Dan Cornilescu Jun 17 '15 at 03:21

1 Answers1

0

No, you can't mix shell and expect like that, but you can send a complex shell command:

send "cd /myDir\r"
expect "#"
send {for f in *.sh; do head -1 "$f" | grep -q $'[\x0D]' && { dos2unix "$f"; chmod 777 "$f"; }; ./"$f"; done}
send "\r"
expect "#"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352