本文介绍了从在bash $ @删除第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写需要遍历传递到脚本的参数bash脚本。然而,第一个参数不应该被循环结束,而不是需要在循环前进行检查。

I'm writing a bash script that needs to loop over the arguments passed into the script. However, the first argument shouldn't be looped over, and instead needs to be checked before the loop.

如果我没有要删除第一个元素,我只是做:

If I didn't have to remove that first element I could just do:

for item in "$@" ; do
  #process item
done

我可以修改循环来检查它是否是在其第一次迭代,改变行为,但似乎太的hackish。这里一定是一个简单的方法来提取的第一个参数出来,然后遍历所有的休息,但我没能找到它。

I could modify the loop to check if it's in its first iteration and change the behavior, but that seems way too hackish. There's got to be a simple way to extract the first argument out and then loop over the rest, but I wasn't able to find it.

推荐答案

使用

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html

基本上,读 $ 1 的循环之前的第一个参数(或 $ 1,0 如果你想什么检查是脚本的名称),然后使用,然后在循环余下的 $ @

Basically, read $1 for the first argument before the loop (or $0 if what you're wanting to check is the script name), then use shift, then loop over the remaining $@.

这篇关于从在bash $ @删除第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:22