简体   繁体   English

查找命令可从命令行运行,但不能在脚本中运行

[英]Find command works from command line but not in a script

I am working on a process that needs to run every night and will tar.gz a large list of files (too large to do with the tar command). 我正在研究一个需要每晚运行的进程,它将tar.gz包含大量文件(对于tar命令而言太大)。 To workaround this, I can create a list of files I want tar to archive and pass it in. I usually do this manually, but I'd like to automate it using a crontab. 要解决此问题,我可以创建一个我希望tar存档并传递给它的文件列表。我通常手动执行此操作,但是我想使用crontab将其自动化。

The OS in this case is RedHat Enterprise 6.3 (Santiago). 在这种情况下,操作系统为RedHat Enterprise 6.3(圣地亚哥)。

I have the following bash shell script: 我有以下bash shell脚本:

#!/bin/bash

now=$(date +"%Y%m%d")

TARGET_DIR=/usr/share/data

FILE_LIST=/home/user/txt-files/data-as-of-$now.txt

if [ -e $FILE_LIST ];
then
    rm -rf $FILE_LIST
fi

CMD="find $TARGET_DIR -regextype posix-extended -regex \".*/$now.*\" -fprint $FILE_LIST"

echo $CMD

findCmd=`$CMD`

if [ $? -ne 0 ];
then
    echo "command failed; $findCmd"
fi

The script runs fine but doesn't write anything to the file specified in the -fprint argument. 该脚本可以正常运行,但不会向-fprint参数中指定的文件写入任何内容。 If I take the command that is echo'd out and copy and paste it to cli, it works like I expect and my file has the list of files I want. 如果我使用已回显的命令并将其复制并粘贴到cli,它的工作方式将与我期望的一样,并且我的文件包含了我想要的文件列表。

find /usr/share/data -regextype posix-extended -regex ".*/20140624.*" -fprint /home/user/txt-files/data-as-of-20140624.txt

I am guessing that there is something funky about the regex that gets interpreted incorrectly when running as a script but not as a command from the CLI. 我猜想正则表达式有些时髦,当作为脚本而不是CLI的命令运行时,正则表达式会被错误地解释。 I've read in other threads about the need to enclose the asterisk in quotes, and I've tried the single tick and double tick, neither seem to work. 我在其他主题中读过有关将星号括在引号中的需要,并且我尝试了单刻度和双刻度,但似乎都没有用。 I've also tried the back tick way of executing the command and the $() way, both yield the same result. 我还尝试了执行命令的后退标记方式和$()方式,两者均产生相同的结果。

If I were getting any errors or messages this would be less perplexing. 如果我收到任何错误或消息,这将减少混乱。 If anyone could look and see if I am doing something blatantly wrong, I'd appreciate the pointer! 如果有人可以看看我是否在做明显地错误的事情,我将非常感谢您的指导!

If I run the command with bash -x, I get this output: 如果我使用bash -x运行命令,则会得到以下输出:

[user@data-provider bin]$ bash -x tgz2.sh
++ date +%Y%m%d
+ now=20140624
+ TARGET_DIR=/usr/share/data
+ FILE_LIST=/home/user/txt-files/data-as-of-20140624.txt
+ '[' -e /home/user/txt-files/data-as-of-20140624.txt ']'
+ rm -rf /home/user/txt-files/data-as-of-20140624.txt
+ CMD='find /usr/share/data -regextype posix-extended -regex '\''.*/20140624.*'\'' -fprint /home/user/txt-files/data-as-of-20140624.txt'
+ echo find /usr/share/data -regextype posix-extended -regex ''\''.*/20140624.*'\''' -fprint /home/user/txt-files/data-as-of-20140624.txt
find /usr/share/data -regextype posix-extended -regex '.*/20140624.*' -fprint /home/user/txt-files/data-as-of-20140624.txt
++ find /usr/share/data -regextype posix-extended -regex ''\''.*/20140624.*'\''' -fprint /home/user/txt-files/data-as-of-20140624.txt
+ findCmd=
+ '[' 0 -ne 0 ']'

Switching to just the below syntax in the script, without any variable assignment, fixed the issue. 在脚本中切换至仅以下语法,无需分配任何变量,即可解决此问题。

find $TARGET_DIR -regextype posix-extended -regex ".*/$now.*" -fprint $FILE_LIST

The problem with the original syntax, which I tested with 我测试过的原始语法有问题

cmd="find -regex '.*/test.*' -fprint out.txt
$cmd

is that it gets run as 是它像

find -regex ''\''.*/test.*'\''' -fprint out.txt

but not entirely sure why variable substitution adds the erroneous single quotes. 但不能完全确定为什么变量替换会添加错误的单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM