将日期/时间值传递给bash函数内的touch命令

huangapple go评论68阅读模式
英文:

Passing a date/time value to touch command within bash function

问题

I am trying to write a bash function (Mac OS X) which searches for a specific range of files between two input date/times. Most variations I have tried for evaluating the two inputs $1 and $2 fail. Hardcoding the times works fine (as per usage line below) i.e. the search syntax is fine. Grateful for pointers where I'm going wrong on passing the two inputs to the touch commands.

function ffiles_search1 () {
echo "usage start 201911270000 end 201912102359 "
touch -t $(eval echo "$1") /tmp/lower-date && touch -t $(eval echo "$2") /tmp/upper-date && find . -path "./Library" -prune -o -type f -a -newer /tmp/lower-date -a ! -newer /tmp/upper-date -a -size +32k -a ! -size +1024k -print0 | xargs -0 ls -ld | egrep -iv "|ppt|doc"
}
英文:

I am trying to write a bash function (Mac OS X) which searches for a specific range of files between two input date/times. Most variations I have tried for evaluating the two inputs $1 and $2 fail. Hardcoding the times works fine (as per usage line below) i.e. the search syntax is fine. Grateful for pointers where I'm going wrong on passing the two inputs to the touch commands.

function ffiles_search1 () {
echo "usage start 201911270000 end 201912102359 "
touch -t $(eval echo "$1")  /tmp/lower-date && touch -t $(eval echo "$2") /tmp/upper-date && find . -path "./Library" -prune -o  -type f -a -newer /tmp/lower-date -a ! -newer /tmp/upper-date -a -size +32k -a  ! -size +1024k -print0 | xargs -0 ls -ld | egrep -iv "|ppt|doc"
}

答案1

得分: 1

修正后的代码,调用如下:

ffiles_search 201911270000 201912102359

在以下函数上:

function ffiles_search () {
    echo "usage start 201911270000 end 201912102359"
    touch -t "$1" /tmp/lower-date &&
    touch -t "$2" /tmp/upper-date &&
    find . -path "./Library" -prune -o -type f -a -newer /tmp/lower-date \
       -a ! -newer /tmp/upper-date -a -size +32k -a -size -1024k -print0 |
    xargs -0 ls -ld |
    egrep -iv -f $HOME/Scripts/egrep_exclusions/time_search.txt
}
英文:

Corrected code, invoking as

ffiles_search 201911270000 201912102359 

on the function

function ffiles_search () {
    echo "usage start 201911270000 end 201912102359 " 
    touch -t "$1" /tmp/lower-date && 
    touch -t "$2" /tmp/upper-date && 
    find . -path "./Library" -prune -o -type f -a -newer /tmp/lower-date \
       -a ! -newer /tmp/upper-date -a -size +32k -a -size -1024k -print0 | 
    xargs -0 ls -ld |
    egrep -iv -f $HOME/Scripts/egrep_exclusions/time_search.txt 
}

huangapple
  • 本文由 发表于 2020年1月3日 21:23:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定