在bash脚本中,我想定义一个commandstring
,它基于传递给脚本的参数来指定find
的选项和主键。然后,我想使用exec
来调用带有这些选项和主键的find
。
通常,我希望能够定义find
的any调用,我可以在命令行上运行它,然后将其exec
定义在子shell中。特别是,我希望能够排除包含linefeed = LF = newline = $'\n'
的文件名。
当我使用参数easy
调用下面的示例脚本时,它将打印所有包含空格的文件名,并且exec
可以正常工作。但是,如果我使用参数hard
调用它,则exec
无法按预期工作。
正如您在下面看到的,如果我对命令find . -name *$'\n'*
进行了硬编码,它将按预期工作。但是,如果我将其放入commandstring
,则不会。有什么办法可以解决这个问题?
我没有可用的更高版本的bash,在macOS上停留在版本3上。
脚本,称为junk
:
#!/bin/bash
set -f
bash --version
if [[ ${1} = easy ]]
then
printf '***%s***\n' "${1}"
commandstring="find . -name *[[:space:]]* "
elif [[ ${1} = hard ]]
then
printf '***%s***\n' "${1}"
commandstring="find . -name *$'\n'* "
else
printf '%s\n' "first argument=${1} but must be either easy or hard."
exit 1
fi
declare -p commandstring; printf '%s\n' "commandstring=${commandstring}";
printf -- '-------------------------------\n%s\n' "hard-coded hard commandstring inside this script:"
find . -name *$'\n'*
printf -- '-------------------------------\n%s\n' "hard-coded easy commandstring inside this script:"
find . -name *[[:space:]]*
printf -- '-------------------------------\n%s\n' 'Now test exec with the commandstring, inside $( subshell )'
junk=$(
{
set -f
printf '%s\n' "Now inside subshell, will exec"
declare -p commandstring; printf '%s\n' "commandstring=${commandstring}";
exec $commandstring
} > /dev/stderr ;
)
printf '%s\n' 'got past $( subshell ); now try to exec it inside the script'
exec $commandstring
printf '%s\n' 'should never get here, because it comes after exec'
其行为方式:
> find . -type f
./easy
./newlineBEF
newlineAFT
./tabBEF tabAFT
./okay
./spaceBEF AFTspace
./zzz
> ls -lT ./*
-rw-r--r-- 1 BNW staff 10 Oct 19 20:15:36 2019 ./easy
-rw-r--r-- 1 BNW staff 11 Oct 20 12:08:24 2019 ./newlineBEF?newlineAFT
-rw-r--r-- 1 BNW staff 13 Oct 19 20:15:43 2019 ./okay
-rw-r--r-- 1 BNW staff 14 Oct 20 12:37:19 2019 ./spaceBEF AFTspace
-rw-r--r-- 1 BNW staff 10 Oct 19 20:14:01 2019 ./tabBEF?tabAFT
-rw-r--r-- 1 BNW staff 0 Feb 26 11:30:54 2019 ./zzz
> junk easy
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
***easy***
declare -- commandstring="find . -name *[[:space:]]* "
commandstring=find . -name *[[:space:]]*
-------------------------------
hard-coded hard commandstring inside this script:
./newlineBEF
newlineAFT
-------------------------------
hard-coded easy commandstring inside this script:
./newlineBEF
newlineAFT
./tabBEF tabAFT
./spaceBEF AFTspace
-------------------------------
Now test exec with the commandstring, inside $( subshell )
Now inside subshell, will exec
declare -- commandstring="find . -name *[[:space:]]* "
commandstring=find . -name *[[:space:]]*
./newlineBEF
newlineAFT
./tabBEF tabAFT
./spaceBEF AFTspace
got past $( subshell ); now try to exec it inside the script
./newlineBEF
newlineAFT
./tabBEF tabAFT
./spaceBEF AFTspace
> junk hard
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
***hard***
declare -- commandstring="find . -name *\$'\\n'* "
commandstring=find . -name *$'\n'*
-------------------------------
hard-coded hard commandstring inside this script:
./newlineBEF
newlineAFT
-------------------------------
hard-coded easy commandstring inside this script:
./newlineBEF
newlineAFT
./tabBEF tabAFT
./spaceBEF AFTspace
-------------------------------
Now test exec with the commandstring, inside $( subshell )
Now inside subshell, will exec
declare -- commandstring="find . -name *\$'\\n'* "
commandstring=find . -name *$'\n'*
got past $( subshell ); now try to exec it inside the script
>
感谢答案:以下脚本的行为符合预期。
#!/bin/bash
set -f
bash --version
argARRAY=( "$@" )
declare -p argARRAY
find "${argARRAY[@]}"
两次调用:
> junk .
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
declare -a argARRAY='([0]=".")'
.
./easy
./newlineBEF
newlineAFT
./tabBEF tabAFT
./okay
./spaceBEF AFTspace
./zzz
> junk . -name *$'\n'*
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
declare -a argARRAY='([0]="." [1]="-name" [2]="*
*")'
./newlineBEF
newlineAFT