liunx crontab运行脚本之 more test.txt |wc -l 命令的问题
求大神解答一下我遇到的现象,实在是好奇啊。
问题描述:
当test.txt文件为空时 more test.txt |wc -l 命令 在corntab定时执行时,为什么结果为3而手工执行结果却为0
PS:验证发现 脚本保存路径和执行用户默认路径不一致时 如果指定绝对路径可以正常执行,不指定路径就会出现上述问题。
(用-s判断文件可以避免这样的问题。我只是想知道上面的现象是为什么)
详细步骤:
设置了一个系统定时任务,
corntab -l
*/2 * * * * sh /opt/ces.sh
查看执行结果为
not
3
/home
手工执行是结果为
not
not
ces.sh脚本保存在/opt 系统默认路径为/home ;
ces.sh内容为
#!/bin/sh
touch test.txt
aa=‘111’
if [ -s test.txt ];
then
echo $aa >> log.txt
echo 'pwd'
else
echo “not ”
fi
bb='more test.txt |wc -l'
if [ $bb -ne 0];
then
echo $bb >> log.txt
echo 'pwd'
else
echo “not 11”
fi