Home > front end >  How to pass variable inside the ssh command inside for loop
How to pass variable inside the ssh command inside for loop

Time:09-17

I have a VM where I have a folder A which contains doc1.txt doc2.txt king1.so and king2.so
I am trying to list the files of a specific extension using ssh from another VM.

VAR_PATH="/predefined/path/to/A"
echo $VAR_PATH

for file_path in `ssh [email protected] "find $VAR_PATH -iname "*.so""`
do
        echo $file_path
done

This is not working and giving me error as

find: paths must precede expression: sample.so Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

the same command works when I am hardcoding the path in find command

for file_path in `ssh [email protected] "find /predefined/path/to/A -iname "*.so""`
do
        echo $file_path
done

How do I pass path variable inside the ssh command inside this for loop

CodePudding user response:

try this one: (centos 8 test passed)

D=/tmp/test/ && for i in `ssh xxx@xxxxx "find $D -name '*.so'"`;do echo $i;done

CodePudding user response:

Better to do without the for-loop and store the output in an array, then you can loop or anything you want.

array_result=(`ssh [email protected] "find ${VAR_PATH} -iname *.so"`)
  • Related