简体   繁体   中英

Unable to run for loop on the remote server

I have a file with different appnames called file.txt. Contents of file.txt is below. I am logging into a target server using ssh from my current server and then running a for loop on the target server to read contents of file.txt and replacing "appName" below.

The issue I run into is, instead of running the from loop on the target server the process is running it on the source server and it's unable to find the file.txt on the source server.

As an alternative, I have placed file.txt on the source server as well and this time the variable doesn't get read and replaced.

Contents of file.txt:
app1
app2
app3

ssh  -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /app/.ssh/key.pem -t  abc@$server1 << EOT
    sudo su - user1
    cd /app/dir1
    for appName in `cat file.txt`
    do
        mv /app/dir1/httpd/${appName}/ /app/dir1/backup/${appName}/
        cp -R /app/dir2/source/${appName}/ /app/dir1/httpd/${appName}
    done
EOT

I would like to run the loop n times (3 in this case) the number of rows present in file.txt and replace ${appName} with app1 in first run, app2 in second run and app3 in third run.

Thank you in advance!

I do a test like this, it seems work well

ssh root@test-server << EOF

cat somefile.txt | while read line; do
echo \$line
done

EOF

maybe you can try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM