Can i use if else inside expect command in shell script?

hii,,
I am trying to automate jira. during my scripting using bash script, in the terminal i got the terminal message like this:

"Configure which ports JIRA will use.
JIRA requires two TCP ports that are not being used by any other
applications on this machine. The HTTP port is where you will access JIRA
through your browser. The Control port is used to Startup and Shutdown JIRA.
Use default ports (HTTP: 8080, Control: 8005) - Recommended [1, Enter], Set custom value for HTTP and Control ports [2]"

here i want to change the port if i have entered a string for that port or just continue with the default.if i am trying with the custom values, i will get the message like that, i want to type enter the custome values there.

all these process iam doing with expect and i getting done when i go without changing the port values.my succeess code is as below:

/usr/bin/expect <<EOD

spawn sudo ./atlassian-jira-5.1-x64.bin

expect "This will install JIRA 5.1 on your computer."
send "\r" 
expect "Choose the appropriate installation or upgrade option."
send "\r" 
expect "Where should JIRA 5.1 be installed?"
send "\r" 
expect "Choose the appropriate installation or upgrade option."
send "\r" 
expect "Default location for JIRA data"
send "\r" 
expect "Configure which ports JIRA will use."
send "\r" 
expect "JIRA can be run in the background."
send "\r" 

EOD

but now when i tried to change that ports , my code is:

/usr/bin/expect <<EOD
spawn sudo ./atlassian-jira-5.1-x64.bin
expect "This will install JIRA 5.1 on your computer."
send "\r" 
expect "Choose the appropriate installation or upgrade option."
send "\r" 
expect "Where should JIRA 5.1 be installed?"
  if [ -z "$location" ]; then 
    send "\r"
  else
    send "$location\r" 
  fi 
expect "Choose the appropriate installation or upgrade option."
send "\r" 
expect "Default location for JIRA data"
  if [ -z "$default_locaton" ]; then
    send "\r"
  else
    send "$default_locaton\r" 
  fi 
expect "Configure which ports JIRA will use."
  if [ -z "$config_port" -a -z "$control_port" ]; then
    send "\r" 
  elif [ -n "$config_port" -a -z "$control_port" ]; then
    send "2\r"
    expect "HTTP Port Number"
    send "$config_port\r"    
    expect "Control Port Number"
    send "\r"
  elif [ -z "$config_port" -a -n "$control_port" ]; then
    send "2\r"
    expect "HTTP Port Number"
    send "\r"    
    expect "Control Port Number"
    send "$control_port\r"
  elif [ -n "$config_port" -a -n "$control_port" ]; then
    send "2\r"
    expect "HTTP Port Number"
    send "$config_port\r"    
    expect "Control Port Number"
    send "$control_port\r"
  fi 
expect "JIRA can be run in the background."
send "\r" 

EOD

and iam getting the error as:

invalid command name "-z"
    while executing
"-z "/opt/atlassian/jira/jiranew" "
    invoked from within
"if [ -z "/opt/atlassian/jira/jiranew" ]"

Please give mea solution ASAP.
thanks...:slight_smile:

That's just not how if statements work in expect, if I'm reading it right. Most of your checks come down around like the below:

I don't have expect handy this second, but I think this would be right

if {0 == [llength $config_port]} {
            send "Something\r"
}

You just need to adjust how you're doing if statements - bourne syntax isn't right for an expect/tcl script.

1 Like