Skip to main content
Tweeted twitter.com/StackUnix/status/921255988979814400
deleted 3 characters in body
Source Link
Brandon
  • 411
  • 4
  • 9

I have SSH executing some commands in a script, like so:

#!/bin/bash

$REMOTEUSER=$1REMOTEUSER=$1
$REMOTEHOST=$2REMOTEHOST=$2    
$new=$3newVh=$3

ssh "$REMOTEUSER"@"$REMOTEHOST" << EOF

cd

if [ ! -d "/data/web/someDirectory" ]
then
  echo -e "There is no vhost setup for someDirectory"
  exit 1
fi


if [ ! -d "git" ]
then
  echo -e "Home git directory not found. Creating it now."
  mkdir git
fi

if [ ! -d "git/$new$newVh.git" ]
then
  echo -e "Bare git repository not found for $new. Creating it now."
  mkdir git/"$new""$newVh".git
  git init --bare git/"$new""$newVh".git
fi

    
EOF

Which works fine and does what I want. I'm trying to check if the compass ruby gem is installed on the remote machine and if it's not, I want to install it. I added this line to the bottom of the script:

.
.
.
if [ `gem list compass -i` == 'false' ]
then
  echo -e "Compass not installed... Installing it now."
  gem install compass -V
fi

    
EOF

But `gem list compass -i`. is getting evaluated on my local machine beforehand (I have compass installed). Even running gem list compass -i in the script with no quotes to see the output is returning true which is incorrect. I tried escaping the command with a number of \s but it didn't work.

I guess two questions:

How is it that other commands in the script (mkdir, cd, git init etc...) work correctly on the remote machine but gem list does not?

How do I escape that command to run it remotely?

I have SSH executing some commands in a script, like so:

#!/bin/bash

$REMOTEUSER=$1
$REMOTEHOST=$2    
$new=$3

ssh "$REMOTEUSER"@"$REMOTEHOST" << EOF

cd

if [ ! -d "/data/web/someDirectory" ]
then
  echo -e "There is no vhost setup for someDirectory"
  exit 1
fi


if [ ! -d "git" ]
then
  echo -e "Home git directory not found. Creating it now."
  mkdir git
fi

if [ ! -d "git/$new.git" ]
then
  echo -e "Bare git repository not found for $new. Creating it now."
  mkdir git/"$new".git
  git init --bare git/"$new".git
fi

    
EOF

Which works fine and does what I want. I'm trying to check if the compass ruby gem is installed on the remote machine and if it's not, I want to install it. I added this line to the bottom of the script:

.
.
.
if [ `gem list compass -i` == 'false' ]
then
  echo -e "Compass not installed... Installing it now."
  gem install compass -V
fi

    
EOF

But `gem list compass -i`. is getting evaluated on my local machine beforehand (I have compass installed). Even running gem list compass -i in the script with no quotes to see the output is returning true which is incorrect. I tried escaping the command with a number of \s but it didn't work.

I guess two questions:

How is it that other commands in the script (mkdir, cd, git init etc...) work correctly on the remote machine but gem list does not?

How do I escape that command to run it remotely?

I have SSH executing some commands in a script, like so:

#!/bin/bash

REMOTEUSER=$1
REMOTEHOST=$2    
newVh=$3

ssh "$REMOTEUSER"@"$REMOTEHOST" << EOF

cd

if [ ! -d "/data/web/someDirectory" ]
then
  echo -e "There is no vhost setup for someDirectory"
  exit 1
fi


if [ ! -d "git" ]
then
  echo -e "Home git directory not found. Creating it now."
  mkdir git
fi

if [ ! -d "git/$newVh.git" ]
then
  echo -e "Bare git repository not found for $new. Creating it now."
  mkdir git/"$newVh".git
  git init --bare git/"$newVh".git
fi

    
EOF

Which works fine and does what I want. I'm trying to check if the compass ruby gem is installed on the remote machine and if it's not, I want to install it. I added this line to the bottom of the script:

.
.
.
if [ `gem list compass -i` == 'false' ]
then
  echo -e "Compass not installed... Installing it now."
  gem install compass -V
fi

    
EOF

But `gem list compass -i`. is getting evaluated on my local machine beforehand (I have compass installed). Even running gem list compass -i in the script with no quotes to see the output is returning true which is incorrect. I tried escaping the command with a number of \s but it didn't work.

I guess two questions:

How is it that other commands in the script (mkdir, cd, git init etc...) work correctly on the remote machine but gem list does not?

How do I escape that command to run it remotely?

Source Link
Brandon
  • 411
  • 4
  • 9

How to force ssh to execute command on the remote system instead of locally

I have SSH executing some commands in a script, like so:

#!/bin/bash

$REMOTEUSER=$1
$REMOTEHOST=$2    
$new=$3

ssh "$REMOTEUSER"@"$REMOTEHOST" << EOF

cd

if [ ! -d "/data/web/someDirectory" ]
then
  echo -e "There is no vhost setup for someDirectory"
  exit 1
fi


if [ ! -d "git" ]
then
  echo -e "Home git directory not found. Creating it now."
  mkdir git
fi

if [ ! -d "git/$new.git" ]
then
  echo -e "Bare git repository not found for $new. Creating it now."
  mkdir git/"$new".git
  git init --bare git/"$new".git
fi

    
EOF

Which works fine and does what I want. I'm trying to check if the compass ruby gem is installed on the remote machine and if it's not, I want to install it. I added this line to the bottom of the script:

.
.
.
if [ `gem list compass -i` == 'false' ]
then
  echo -e "Compass not installed... Installing it now."
  gem install compass -V
fi

    
EOF

But `gem list compass -i`. is getting evaluated on my local machine beforehand (I have compass installed). Even running gem list compass -i in the script with no quotes to see the output is returning true which is incorrect. I tried escaping the command with a number of \s but it didn't work.

I guess two questions:

How is it that other commands in the script (mkdir, cd, git init etc...) work correctly on the remote machine but gem list does not?

How do I escape that command to run it remotely?