如何调节 bash 脚本以发送警告
How to condition a bash script to send a warning
我有一个从 sftp 获取文件的脚本,稍微修改一下路径和访问权限,然后用于在 mysql 中导入:
#!/bin/bash
sshpass -p password sftp -P 22001 ftp@server <<-'EOSSH1'
lcd path/where/to/be/grabbed
get *.csv
EOSSH1
#Moving and permission
mv original/path destination/path
chmod 777 file
#Insertion csv in mysql
mysql --login-path=user < cmds.txt (a file with commands to be executed in mysql)
现在,我需要的是设置如下条件:
如果 csv 文件不在 sftp 服务器中,则发送电子邮件而不是 运行 脚本。
如果文件在那里,一切都 运行 没问题。
知道怎么做吗?
BR
特里斯坦
运行 sftp
在批处理模式下使用 -o BatchMode=yes
。从 stdin 提供脚本,所以 -b -
.
如果 get *.csv
失败,则 sftp
将以非零状态退出。
在您的脚本中,您可以检查退出状态。
#!/bin/bash
sshpass -p password sftp -o BatchMode=yes -b - -P 22001 ftp@server <<-'EOSSH1'
lcd path/where/to/be/grabbed
get *.csv
EOSSH1
if [ "$?" -ne 0 ]; then
echo "sftp failed. exiting..." >&2
exit 1
fi
#Moving and permission
mv original/path destination/path
chmod 777 file
#Insertion csv in mysql
mysql --login-path=user < cmds.txt (a file with commands to be executed in mysql)
手册页:
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication to obviate the need to enter a password at connection time (see sshd(8) and ssh-keygen(1) for details).
A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, reget, reput, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir.
Termination on error can be suppressed on a command by command basis by prefixing the command with a ‘-’ character (for example, -rm /tmp/blah*). Echo of the command may be suppressed by prefixing the command with a ‘@’ character. These two prefixes may be combined in any order, for example
-@ls /bsd.
您也可以简单地使用 scp
:
sshpass -p password scp ftp@server:*.csv path/where/to/be/grabbed/ || exit 1
我有一个从 sftp 获取文件的脚本,稍微修改一下路径和访问权限,然后用于在 mysql 中导入:
#!/bin/bash
sshpass -p password sftp -P 22001 ftp@server <<-'EOSSH1'
lcd path/where/to/be/grabbed
get *.csv
EOSSH1
#Moving and permission
mv original/path destination/path
chmod 777 file
#Insertion csv in mysql
mysql --login-path=user < cmds.txt (a file with commands to be executed in mysql)
现在,我需要的是设置如下条件: 如果 csv 文件不在 sftp 服务器中,则发送电子邮件而不是 运行 脚本。 如果文件在那里,一切都 运行 没问题。
知道怎么做吗?
BR 特里斯坦
运行 sftp
在批处理模式下使用 -o BatchMode=yes
。从 stdin 提供脚本,所以 -b -
.
如果 get *.csv
失败,则 sftp
将以非零状态退出。
在您的脚本中,您可以检查退出状态。
#!/bin/bash
sshpass -p password sftp -o BatchMode=yes -b - -P 22001 ftp@server <<-'EOSSH1'
lcd path/where/to/be/grabbed
get *.csv
EOSSH1
if [ "$?" -ne 0 ]; then
echo "sftp failed. exiting..." >&2
exit 1
fi
#Moving and permission
mv original/path destination/path
chmod 777 file
#Insertion csv in mysql
mysql --login-path=user < cmds.txt (a file with commands to be executed in mysql)
手册页:
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication to obviate the need to enter a password at connection time (see sshd(8) and ssh-keygen(1) for details).
A batchfile of ‘-’ may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, reget, reput, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir.
Termination on error can be suppressed on a command by command basis by prefixing the command with a ‘-’ character (for example, -rm /tmp/blah*). Echo of the command may be suppressed by prefixing the command with a ‘@’ character. These two prefixes may be combined in any order, for example
-@ls /bsd.
您也可以简单地使用 scp
:
sshpass -p password scp ftp@server:*.csv path/where/to/be/grabbed/ || exit 1