我基本上试图将我在网上找到的两个脚本放在一起。第一个部分分为两部分,检查目录是否存在,如果不存在则创建目录,如果输入了错误的登录密码,则继续通过网络摄像头拍照。据我所知,这部分工作正常。
#!/bin/bash
## Variables
dir=/tmp/gotcha
got=/tmp/gotcha/gotcha.jpg
[email protected]
[email protected]
SMTPSERVER=smtp.googlemail.com:587
[email protected]
SMTPPASS=xxxxx
SUBJECT="Someone tried to access your computer while you were away."
if test [ -d "$dir" ]
then
echo "Found directory /tmp/gotcha/" ]
else
mkdir /tmp/gotcha/
fi
ts=`date +%s`
ffmpeg -f video4linux2 -s vga -i /dev/video0 -vframes 1 $got
exit 0 ## NOTE - must exit with status 0
第二个是应该通过电子邮件将拍摄的照片发送给我,然后从存储目录中删除照片,但据我所知,即使我可以通过命令行和 GUI 找到有问题的文件,它也找不到照片。
## sendEmail script, found in /home/josh/scripts/emailscript
if test -f [ "$got" ]
then
sendemail -f $SMTPFROM -t $SMTPTO -u $SUBJECT -a $got -s $SMTPSERVER -$
fi
## Remove gotcha.jpg
if test -f [ "$got" ]
then
rm $got
当我在终端中运行脚本时,输出如下:
/usr/local/bin/gotcha: line 13: test: too many arguments
mkdir: cannot create directory ‘/tmp/gotcha/’: File exists
ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
WARNING: library configuration mismatch
avcodec configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 10364.818498, bitrate: 147456 kb/s
Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 147456 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0x558898563320] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to '/tmp/gotcha/gotcha.jpg':
Metadata:
encoder : Lavf57.83.100
Stream #0:0: Video: mjpeg, yuvj422p(pc), 640x480, q=2-31, 200 kb/s, 30 fps, 30 tbn, 30 tbc
Metadata:
encoder : Lavc57.107.100 mjpeg
Side data:
cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame= 1 fps=0.0 q=5.7 Lsize=N/A time=00:00:00.03 bitrate=N/A speed=3.32x
video:24kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
就像我说的,我是 bash 脚本新手,所以我可能忽略了一些愚蠢的事情,但是任何帮助都是值得赞赏的!
答案1
1) 使用[ -d ...]
或者test -d ...
但是不是 test -d [ ... ]
(同样适用于-f
)
2)test -f [ $file ]
将要
- 查找名为的文件
[
- 抱怨额外的争论
3)在
if test [ -d "$dir" ]
then
echo "Found directory /tmp/gotcha/" ]
else
mkdir /tmp/gotcha/
fi
你可以使用dir
var
if test -d "$dir"
then
echo "Found directory $dir" ]
else
mkdir "$dir"
fi
答案2
我设法找出我的(重大)错误。我尝试清理很多内容,在此过程中我注意到我使用的 sendEmail 脚本缺少一个选项:
-o tls=yes
最后,脚本完全可以工作,并且看起来更加整洁:
#!/bin/bash
##########
## variable key
file=/tmp/gotcha/pic.jpg
dir=/tmp/gotcha
[email protected]
[email protected]
SMTPSERVER=smtp.googlemail.com:587
[email protected]
SMTPPASS=xxxxx
SUBJECT="Someone tried to access your computer while you were away."
MESSAGE="Someone tried to access your computer while you were away. Here's a photo!"
##########
## create directory if it doesn't exist
if test -d "$dir"
then
echo "Found directory...";
else
echo "Directory not found..."
sleep 1.5
echo "Creating directory $dir..."
mkdir -p $dir
sleep 1.5
echo "Directory created...";
fi
##########
## ensures directory is clear before beginning script
if test -f "$file"
then
echo "Clearing directory of any past files..."
sudo rm $file;
fi
##########
## takes picture with webcam if incorrect password is entered
## stores picture in created directory
ts=`date +%s`
ffmpeg -f video4linux2 -s vga -i /dev/video0 -vframes 1 $file
#exit 0 ## NOTE - must exit with status 0
##########
## send $file in email
sudo sendemail -f $SMTPFROM -t $SMTPTO -o tls=yes -a $file -u $SUBJECT -m $MESSAGE -s $SMTPSERVER -xu $SMTPUSER -xp $SMTPPASS
##########
不要介意所有额外的哈希值,只要我的组织方式即可。我确信我还有很多地方可以做得更好,也有很多不必要的事情,但它确实有效!感谢所有的投入!