JavaSheriff
JavaSheriff

Reputation: 7677

bash script, execute shell from another shell and assign results to a variable

I have this legacy delete script script, It is doing some delete work on a remote application. When processing of delete is done it will return #completed successfully# or it will return

#
    *nothing more to delete*
    *Program will exit*
#

I would like to assign its output and execute the delete script as long its output is "completed successfully".

I am unable to assign the results of the script to a variable. I am running the shell script from folder X while the delete script is in folder Y. Besides the script below, I also tried:

response=$(cd $path_to_del;./delete.sh ...)

I am unable to make this work.

#!/bin/bash
path_to_del=/apps/Utilities/unix/
response='completed successfully'

counter=1
while [[ $response == *successfully*  ]]
do
         response= working on batch number: $counter ...
         echo $response
         (cd $path_to_del;./delete.sh   "-physicalDelete=true") > $response

         echo response $response
         ((counter++))
done

echo deleting Done!

Upvotes: 1

Views: 249

Answers (1)

Deleted User
Deleted User

Reputation: 2541

general ways to pass output from a subshell to the higher level shell is like this:

 variable="$(command in subshell)"

or

read -t variable < <(command)

therefore the modifications to your script could look like:

response="$(cd $path_to_del;./delete.sh   "-physicalDelete=true")"

or

cd $path_to_del
response="$(./delete.sh   "-physicalDelete=true")"

this line will fail and needs fixing:

response= working on batch number:

Upvotes: 2

Related Questions