What Does `Cat File | Ssh Host 'Cat - >File'` Do?

What Does `Cat File | Ssh Host 'Cat - >File'` Do?

this is the script:

#!/bin/bash

usage() {
    echo "deploy-nginx.sh --production|--staging"
}

case "$1" in
    --staging) NGINX_CONF="conf/nginx-staging"; HOST="staging" ;;
    --production) NGINX_CONF="conf/nginx-production"; HOST="production" ;;
    *) usage; exit 2;
esac

cat "$NGINX_CONF" | ssh -F conf/ssh_config "$HOST" '
    cat - > /tmp/ironscales
    sudo cp /etc/nginx/sites-available//tmp/myapp{,-$(date +%Y%m%d-%H%M%S)}
    sudo mv /tmp/myapp /etc/nginx/sites-available/myapp
    sudo service nginx reload
'

I understand the usage function, and the case switch

what I don't understand at all, is the part following cat

0

1 Answer

The part following cat "$NGINX_CONF" serves to copy the file from the $NGINX_CONF variable to the remote machine using ssh and perform other actions.

  • cat "$NGINX_CONF" | ssh opens the file and pipes it to the ssh process, a less efficient way of letting the shell do the same with

    <"$NGINX_CONF" ssh
    

    The part in single quotes holds the command(s) to run on the remote machine. ssh forwards its input (i.e. the file content) to this command.

  • cat - > /tmp/ironscales redirects cat’s (= ssh’s) standard input (-) to the specified file
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.