I need to run a remote script using ssh
via Ruby
(net/ssh) to recursively copy a folder and exclude a subfolder. I am looking for the fastest way to do it so rsync
is not good. Also, I understand that ssh
uses sh
and not bash
.
In bash I do:
cp -r srcdir/!(subdir) dstdir
and it works fine. However when I launch the script via ssh
I receive the error
sh: 1: Syntax error: "(" unexpected
because it is using sh
.
I have checked the sh
man page, but there is no option to exclude files.
Is it my assumption of ssh
using sh
correct?
Any alternative suggestion?
EDIT 1:
In case it is useful, the output of sudo cat /etc/shells
is the following:
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/tmux
/usr/bin/screen
EDIT 2:
OK. So bash it is available and that does not seems to be the problem. I have verified that the ssh is actually using bash
. The issue seems to be related to the escaping of parenthesis or exclamation mark.
I have tried to run the command from the shell (macos) and this is the actual command:
ssh -i .ssh/key.pem ubuntu@X.X.X.X 'mkdir /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/N; cp -r /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/mesh/!\(constant\) /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/N; ln -s /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/mesh/constant /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/N/constant'
In this way I receive a different error
cp: cannot stat '/home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/mesh/!(constant)': No such file or directory
EDIT 3:
Based on the comments I have changed my command adding extglob
If I use
ssh -i .ssh/key.pem ubuntu@X.X.X.X 'shopt -s extglob; mkdir /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/N; cp -r /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/mesh/!\(constant\) /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/N; ln -s /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/mesh/constant /home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/N/constant'
I receive the following error:
cp: cannot stat '/home/ubuntu/OpenFOAM/ubuntu-4.1/run/LES_New-Area_residuals2/mesh/!(constant)': No such file or directory
If I do not escape the parenthesis I get
bash: -c: line 0: syntax error near unexpected token `('
ssh
(wellsshd
) uses the login shell of the remote user. Could be anything. – Stéphane Chazelas Aug 19 '18 at 17:09