shell check if file exist in one line with ssh, then cd, and npm install

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have these lines in my script:



ipserver=1.1.1.1
fullpathfile="/var/www/html/mysite"
ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] echo "Directory exist" || cd $fullpathfile && npm install "


But I get this error:



Unexpected remote arg: root@1.1.1.1:/var/www/html/mysite/#012ssh root@1.1.1.1 [[


I need to connect through ssh and then check if a folder exist (node_module), if it does not exist, then do: cd folder and npm install.



what am I doing wrong ?







share|improve this question






















  • Did you by any chance write the script on a Windows machine? Is it a DOS text file?
    – Kusalananda
    Mar 22 at 19:15














up vote
0
down vote

favorite












I have these lines in my script:



ipserver=1.1.1.1
fullpathfile="/var/www/html/mysite"
ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] echo "Directory exist" || cd $fullpathfile && npm install "


But I get this error:



Unexpected remote arg: root@1.1.1.1:/var/www/html/mysite/#012ssh root@1.1.1.1 [[


I need to connect through ssh and then check if a folder exist (node_module), if it does not exist, then do: cd folder and npm install.



what am I doing wrong ?







share|improve this question






















  • Did you by any chance write the script on a Windows machine? Is it a DOS text file?
    – Kusalananda
    Mar 22 at 19:15












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have these lines in my script:



ipserver=1.1.1.1
fullpathfile="/var/www/html/mysite"
ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] echo "Directory exist" || cd $fullpathfile && npm install "


But I get this error:



Unexpected remote arg: root@1.1.1.1:/var/www/html/mysite/#012ssh root@1.1.1.1 [[


I need to connect through ssh and then check if a folder exist (node_module), if it does not exist, then do: cd folder and npm install.



what am I doing wrong ?







share|improve this question














I have these lines in my script:



ipserver=1.1.1.1
fullpathfile="/var/www/html/mysite"
ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] echo "Directory exist" || cd $fullpathfile && npm install "


But I get this error:



Unexpected remote arg: root@1.1.1.1:/var/www/html/mysite/#012ssh root@1.1.1.1 [[


I need to connect through ssh and then check if a folder exist (node_module), if it does not exist, then do: cd folder and npm install.



what am I doing wrong ?









share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 18:58

























asked Mar 22 at 18:27









stackdave

15116




15116











  • Did you by any chance write the script on a Windows machine? Is it a DOS text file?
    – Kusalananda
    Mar 22 at 19:15
















  • Did you by any chance write the script on a Windows machine? Is it a DOS text file?
    – Kusalananda
    Mar 22 at 19:15















Did you by any chance write the script on a Windows machine? Is it a DOS text file?
– Kusalananda
Mar 22 at 19:15




Did you by any chance write the script on a Windows machine? Is it a DOS text file?
– Kusalananda
Mar 22 at 19:15










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Note that [[ (and [ and test) are commands like any other. So you need && to only execute the echo if the [[ succeeds.



ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] && echo "Directory exist" || cd $fullpathfile && npm install "


However, enclosing a series of commands in double quotes like that can get hairy pretty fast, especially if your directory name contains spaces, or your commands need quoted arguments of their own.



To avoid headaches, better to install a little script on the server (for example, as "$HOME/bin/do_npm_install.sh") that does what you need, then invoke that with ssh, like



ssh "root@$ipserver" "bash ~/bin/do_npm_install.sh"





share|improve this answer
















  • 1




    The ~/ should be unnecessary as you will have the home directory as working directory when you ssh to a machine.
    – Kusalananda
    Mar 22 at 19:17










  • Should being the keyword. It doesn't hurt to write defensive code.
    – Martijn Dekker
    Mar 22 at 20:10










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f432898%2fshell-check-if-file-exist-in-one-line-with-ssh-then-cd-and-npm-install%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Note that [[ (and [ and test) are commands like any other. So you need && to only execute the echo if the [[ succeeds.



ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] && echo "Directory exist" || cd $fullpathfile && npm install "


However, enclosing a series of commands in double quotes like that can get hairy pretty fast, especially if your directory name contains spaces, or your commands need quoted arguments of their own.



To avoid headaches, better to install a little script on the server (for example, as "$HOME/bin/do_npm_install.sh") that does what you need, then invoke that with ssh, like



ssh "root@$ipserver" "bash ~/bin/do_npm_install.sh"





share|improve this answer
















  • 1




    The ~/ should be unnecessary as you will have the home directory as working directory when you ssh to a machine.
    – Kusalananda
    Mar 22 at 19:17










  • Should being the keyword. It doesn't hurt to write defensive code.
    – Martijn Dekker
    Mar 22 at 20:10














up vote
0
down vote



accepted










Note that [[ (and [ and test) are commands like any other. So you need && to only execute the echo if the [[ succeeds.



ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] && echo "Directory exist" || cd $fullpathfile && npm install "


However, enclosing a series of commands in double quotes like that can get hairy pretty fast, especially if your directory name contains spaces, or your commands need quoted arguments of their own.



To avoid headaches, better to install a little script on the server (for example, as "$HOME/bin/do_npm_install.sh") that does what you need, then invoke that with ssh, like



ssh "root@$ipserver" "bash ~/bin/do_npm_install.sh"





share|improve this answer
















  • 1




    The ~/ should be unnecessary as you will have the home directory as working directory when you ssh to a machine.
    – Kusalananda
    Mar 22 at 19:17










  • Should being the keyword. It doesn't hurt to write defensive code.
    – Martijn Dekker
    Mar 22 at 20:10












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Note that [[ (and [ and test) are commands like any other. So you need && to only execute the echo if the [[ succeeds.



ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] && echo "Directory exist" || cd $fullpathfile && npm install "


However, enclosing a series of commands in double quotes like that can get hairy pretty fast, especially if your directory name contains spaces, or your commands need quoted arguments of their own.



To avoid headaches, better to install a little script on the server (for example, as "$HOME/bin/do_npm_install.sh") that does what you need, then invoke that with ssh, like



ssh "root@$ipserver" "bash ~/bin/do_npm_install.sh"





share|improve this answer












Note that [[ (and [ and test) are commands like any other. So you need && to only execute the echo if the [[ succeeds.



ssh "root@$ipserver" "[[ -d $fullpathfile/node_modules ]] && echo "Directory exist" || cd $fullpathfile && npm install "


However, enclosing a series of commands in double quotes like that can get hairy pretty fast, especially if your directory name contains spaces, or your commands need quoted arguments of their own.



To avoid headaches, better to install a little script on the server (for example, as "$HOME/bin/do_npm_install.sh") that does what you need, then invoke that with ssh, like



ssh "root@$ipserver" "bash ~/bin/do_npm_install.sh"






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 19:14









Martijn Dekker

1013




1013







  • 1




    The ~/ should be unnecessary as you will have the home directory as working directory when you ssh to a machine.
    – Kusalananda
    Mar 22 at 19:17










  • Should being the keyword. It doesn't hurt to write defensive code.
    – Martijn Dekker
    Mar 22 at 20:10












  • 1




    The ~/ should be unnecessary as you will have the home directory as working directory when you ssh to a machine.
    – Kusalananda
    Mar 22 at 19:17










  • Should being the keyword. It doesn't hurt to write defensive code.
    – Martijn Dekker
    Mar 22 at 20:10







1




1




The ~/ should be unnecessary as you will have the home directory as working directory when you ssh to a machine.
– Kusalananda
Mar 22 at 19:17




The ~/ should be unnecessary as you will have the home directory as working directory when you ssh to a machine.
– Kusalananda
Mar 22 at 19:17












Should being the keyword. It doesn't hurt to write defensive code.
– Martijn Dekker
Mar 22 at 20:10




Should being the keyword. It doesn't hurt to write defensive code.
– Martijn Dekker
Mar 22 at 20:10












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f432898%2fshell-check-if-file-exist-in-one-line-with-ssh-then-cd-and-npm-install%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?