Note that there are some explanatory texts on larger screens.

plurals
  1. POExecute commands using function calls in shell scripts
    text
    copied!<p>I am trying to execute a command using function calls in a shell script. When I pass the command to that function as an argument of that function, it does not work. </p> <p>Function definition: </p> <pre><code>function ExecuteCommand() ( # $1: User@Host # $2: Password # $3: Command to execute # Collect current IFS value OLD_IFS=$IFS # Set IFS value to new line feed IFS=$'\n' #Execute the command and capture the output EXPECT_OUTPUT=($(expect ssh_exec.expect $1 $2 $3)) #Print the output OUTPUT_LINE_COUNT=${#EXPECT_OUTPUT[@]} for ((OUTPUT_LINE_INDEX=0; OUTPUT_LINE_INDEX&lt;OUTPUT_LINE_COUNT; OUTPUT_LINE_INDEX++)) ; do echo ${EXPECT_OUTPUT[$OUTPUT_LINE_INDEX]} done # Get back to the original IFS IFS=$OLD_IFS </code></pre> <p>) </p> <p>Function call:</p> <pre><code>ExecuteCommand oracle@192.168.***.*** password123 "srvctl status database -d mydb" </code></pre> <p>And the output I get is: </p> <pre><code>spawn ssh oracle@192.168.***.*** {srvctl status database -d mydb} oracle@192.168.***.***'s password: bash: {srvctl: command not found </code></pre> <p>But when I don't pass the command as an argument of the function, it works perfectly: </p> <p>Function definition in that case: </p> <pre><code>function ExecuteCommand() ( # $1: User@Host # $2: Password # Collect current IFS value OLD_IFS=$IFS # Set IFS value to new line feed IFS=$'\n' #Execute the command and capture the output EXPECT_OUTPUT=($(expect ssh_exec.expect $1 $2 srvctl status database -d mydb)) #Print the output OUTPUT_LINE_COUNT=${#EXPECT_OUTPUT[@]} for ((OUTPUT_LINE_INDEX=0; OUTPUT_LINE_INDEX&lt;OUTPUT_LINE_COUNT; OUTPUT_LINE_INDEX++)) ; do echo ${EXPECT_OUTPUT[$OUTPUT_LINE_INDEX]} done # Get back to the original IFS IFS=$OLD_IFS </code></pre> <p>) </p> <p>Function call:</p> <pre><code>ExecuteCommand oracle@192.168.***.*** password123 </code></pre> <p>And I get the output just as I expected: </p> <pre><code>spawn ssh oracle@192.168.***.*** srvctl status database -d mydb oracle@192.168.***.***'s password: Instance mydb1 is running on node mydb1 Instance mydb2 is running on node mydb2 Instance mydb3 is running on node mydb3 </code></pre> <p>Please help me about what was wrong while passing the command as a function parameter here in the first case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload