Bash

使用腳本執行時,日期命令在遠端伺服器中不起作用

  • April 9, 2017

我正在從遠端伺服器的腳本中執行以下命令,但出現錯誤No such file or directory

ssh -t -t user@xx.xx.xx.xx \"sudo mysql nss_mysql < /home/user/scripts/Db_nss_mysql-`date +%Y-%m-%d`.sql && /bin/bash /home/user/scripts/jenkin-soft.sh\"

我跑得像bash jenkin-soft.sh,完整的錯誤是:

jenkin-soft.sh: line 36: /home/user/scripts/Db_nss_mysql-`date +%Y-%m-%d`.sql: No such file or directory. 

我嘗試了以下命令,它們工作正常::

givinv@ser1:~/scripts$ ssh -t -t user@xx.xx.xx.xx "sudo mysql"
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 66

mysql> Bye
Connection to xx.xx.xx.xx closed.
givinv@ser1:~/scripts$ ssh -t -t  user@xx.xx.xx.xx "sudo ls /home/user/scripts/Db_nss_mysql-`date +%Y-%m-%d`.sql"
/home/user/scripts/Db_nss_mysql-2017-04-07.sql
Connection to xx.xx.xx.xx closed.
givinv@ser1:~/scripts$ 

我做錯了什麼?

您的引用在這裡有錯,或者說沒有引用。因為當你寫: "sudo …." 不引用你的字元串,而是引用一個引號 char "

ssh -t -t user@xx.xx.xx.xx 'sudo mysql nss_mysql < /home/user/scripts/Db_nss_mysql-`date +%Y-%m-%d`.sql && /bin/bash /home/user/scripts/jenkin-soft.sh'

引用自:https://unix.stackexchange.com/questions/356551