从shell脚本(bash)的参数列表中删除最后一个参数
此问题涉及在automator osx中运行的bash脚本.我正在使用自动操作来从查找程序中获取和过滤一堆文件引用.然后,通过自动操作将父文件夹的名称附加到该列表中.然后,Automator将这些参数提供给一个名为运行shell脚本"的操作.我不确定确切如何自动程序调用脚本,但是在用echo "$@"
This question concerns a bash script that is run in automator osx. I am using automator actions to get and filter a bunch of file references from the finder. Then I append to that list the name of the parent folder, also via an automator action. Automator then feeds these arguments to an action called "run shell script". I am not sure exactly how automator invokes the script but the argument list looks like this when echoed with: echo "$@"
/卷/G-Raid/在线/WAV_TEST/Testbok 50/01/01000 43-001.wav/卷/卷/G-Raid/在线/WAV_TEST/Testbok 50/02/02000 43-002.wav/卷/G-Raid/Online/WAV_TEST/Testbok 50/03/03000 43-003.wav/Volumes/G-Raid/Online/WAV_TEST/Testbok 50
/Volumes/G-Raid/Online/WAV_TEST/Testbok 50/01/01000 43-001.wav /Volumes/G-Raid/Online/WAV_TEST/Testbok 50/02/02000 43-002.wav /Volumes/G-Raid/Online/WAV_TEST/Testbok 50/03/03000 43-003.wav /Volumes/G-Raid/Online/WAV_TEST/Testbok 50
在这种情况下,指向3个文件和一个文件夹的路径.
In this case path to 3 files and a folder.
在shell脚本中,我启动了一个名为ripcheckc *的应用程序,该程序使用从automator传递的args减去列表中的最后一个参数(文件夹).
In the shell script I launch an application called ripcheckc* with the args passed from automator minus the last argument(the folder) in the list.
我用它删除最后一个参数:
I use this to remove the last argument:
_args=( "$@" )
unset _args[${#_args[@]}-1]
这是echo $_args
:
/卷/G-Raid/在线/WAV_TEST/Testbok 50/01/01000 43-001.wav/卷/卷/G-Raid/在线/WAV_TEST/Testbok 50/02/02000 43-002.wav/卷/G-Raid/在线/WAV_TEST/Testbok 50/03/03000 43-003.wav
/Volumes/G-Raid/Online/WAV_TEST/Testbok 50/01/01000 43-001.wav /Volumes/G-Raid/Online/WAV_TEST/Testbok 50/02/02000 43-002.wav /Volumes/G-Raid/Online/WAV_TEST/Testbok 50/03/03000 43-003.wav
与以前相同,但没有文件夹.
Same as before but without the folder.
现在,如果我以"$@"
作为参数运行ripcheckc,它可以工作(但稍后会失败,因为参数列表中的最后一条路径).如果我使用${_args[@]}
,应用程序将无提示地中止.当我回显$@
和_args
时,除了最后一个参数外,输出看起来相同.
Now, if I run ripcheckc with "$@"
as argument it works (but fails later on because of that last path in the argument list) If I use ${_args[@]}
the application will just abort silently. When I echo $@
and _args
the output looks identical except for the last argument.
我的问题是-$ @和$ _args产生第一个有效输入而第二个无效输入有什么区别?
My question is - what is the difference between $@ and $_args that make the first valid input and the second not?
*该应用程序是 ripcheckc
我希望我的问题有意义.
I hope my question makes sense.
已解决.
假设您已经有array
,您可以说:
Assuming that you already have an array
, you can say:
unset "array[${#array[@]}-1]"
例如,如果您的脚本包含:
For example, if your script contains:
array=( "$@" )
unset "array[${#array[@]}-1]" # Removes last element -- also see: help unset
for i in "${array[@]}"; do
echo "$i"
done
调用它:bash scriptname foo bar baz
产生:
foo
bar