我有一个脚本,可以测试我的 14.04 服务器上是否有可用的更新,如果有,就向我发送一条短信。
bash 脚本以以下内容开头:
IFS=';' read updates security_updates < <(/usr/lib/update-notifier/apt-check 2>&1)
然后测试:
if [ $updates != 0 ];
then
…
后来:
if [ $security_updates != 0 ];
then
问题是updates
和应该都包含像和这样security_updates
的更新数量。但有时,第一个命令会失败并返回:3
1
E: Unknown Error: '<class 'SystemError'>' (E:Could not open file /var/lib/apt/lists/ubuntu.mirrors.ovh.net_ftp.ubuntu.com_ubuntu_dists_trusty-backports_universe_i18n_Translation-en - open (2: No such file or directory), E:Could not open file /var/lib/apt/lists/ubuntu.mirrors.ovh.net_ftp.ubuntu.com_ubuntu_dists_trusty-backports_restricted_i18n_Translation-en - open (2: No such file or directory), E:Could not open file /var/lib/apt/lists/ubuntu.mirrors.ovh.net_ftp.ubuntu.com_ubuntu_dists_trusty-backports_multiverse_i18n_Translation-en - open (2: No such file or directory), E:Could not open file /var/lib/apt/lists/ubuntu.mirrors.ovh.net_ftp.ubuntu.com_ubuntu_dists_trusty-backports_main_i18n_Translation-en - open (2: No such file or directory), E:Could not open file /var/lib/apt/lists/ubuntu.mirrors.ovh.net_ftp.ubuntu.com_ubuntu_dists_trusty-backports_multiverse_binary-i386_Packages - open (2: No such file or directory), E:Could not open file /var/lib/apt/lists/ubuntu.mirrors.ov
h.net_ftp.ubuntu.com_ubuntu_dists_trusty-backports_universe_binary-i386_Packages - open (2: No such file or directory),
结果我的脚本失败了,cron 给我发了一封电子邮件。在测试变量本身的内容之前,我该如何测试错误?$?
没用……
编辑:
我也尝试过:
s="${updates}"
size=${#s}
if [[ $size > 3 ]] ;then
echo "DATA TOO LARGE";
exit;
fi
大多数情况下,这无法检测到字符串太长……我有时会得到数据太大,有时会得到错误
line 25: [: too many arguments
if
=> 这是第一条语句 的行=> 这是第二条语句line 38: [: !=: unary operator expected
的行if