1

In the part of my script, I need to make ssh to a host and delete the elements of an array. In my current code for each element of the array I need to make ssh to host which takes time. I want to make ssh to the host at one time and then delete all elements of the array. How can I improve my below code from performance point of view?

for x in $Array
do
       echo "Value of array  are : $x"
       ssh [email protected] "rm -rf $x"
done

2 Answers 2

1

Why the loop at all? Using * as subscript gives all elements of an array.

ssh [email protected] "rm -rf ${Array[*]}"

Note that either way (with or without loop) will break if file names contain whitespaces.

Sign up to request clarification or add additional context in comments.

Comments

0

You must input the commands in a file on your local, then upload the file, and finally run the script. Here is how it goes:

echo > rmscript.sh
for x in $Array
do
    echo "Value of array  are : $x"
    echo  "rm -rf $x" >> rmscript.sh
done
#upload
scp rmscript.sh [email protected]:~
#run script
ssh [email protected] "sh ~/rmscript.sh"

Comments

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.