title: Shell script with ssh command with an if statement
body: <p>I want to write a shell script to do the following:</p>
  
  <ol>
  <li>login some remote machines, say host1 and host2, each at a time</li>
  <li>go to the /tmp directory and check whether a particular directory named 'dirname' exists or not. If it doesn't exist, make a directory 'dirname'</li>
  <li>go to the directory /tmp/dirname</li>
  <li>copy a file with a variable runNo from the local machine to this directory </li>
  </ol>
  
  <p>I tried something like this, but it doesn't work.. any help would be much appreciated. Thanks!</p>
  
  <pre><code>#! /bin/bash
  dir1=dirname
  runNo=0
  
  for i in 1 2
  do
    runNo=$(($runNo+1))
    echo "host$i runNo$runNo"
  
    ssh host$i "cd /tmp;
      if [ -d $dir1 ]; then
        cd $dir1;
      else
        mkdir $dir1;
        cd $dir1;
      fi;
      cp ~/local/file$runNo file"
  
    echo "done"
  done
  </code></pre>