首页 > 解决方案 > BASH:使用管道创建变量

问题描述

我有一个 bash 文件,它希望在某个目录中查找早于某个日期的文件并将其删除。它工作正常,我能够回显已删除文件的数量,但是当我尝试将整数放入变量时遇到问题。

#!/bin/bash

# Make this dynamic to look at different directories.
pathtofolder=/var/www/website/temp2/

if [ $hours ]; then
    # To specify older than one day it is better to talk in hours because 
    # the days integer is just an integer so everything less than 2 days 
    # would be 1 day, so 1 day 23 hours and 59 minutes is not greater than
    # 1 day.
    # For this reason I am using mmin and using the hours in minutes.
    timeinmins=$(($hours*60))
elif [ $mins ]
then
    timeinmins=$mins
else
    # The default is 24 hours but we want to test with 24 minutes
    timeinmins=24
fi

find "$pathtofolder"* -mmin +$timeinmins -exec rm -vr {} \; | output="$(wc -l)"
echo "Files deleted: $output"
echo "Minutes: $timeinmins"

在上述情况下,$output为空白。

但这在下面起作用,只是为了回声......

find "$pathtofolder"* -mmin +$timeinmins -exec rm -vr {} \; | "Files deleted: $(wc -l)"

有任何想法吗?提前致谢。

标签: bash

解决方案


推荐阅读