与直接重新分配值相比,在Shell脚本中进行移位的优势 [英] The advantage of shift in shell script over reassign value straightforward
问题描述
我不理解以下代码中的 shift
:
I don't understand the shift
in following code:
#! /usr/local/bin/bash
# process command line options
interactive=
filename=
while [[ -n $1 ]]; do
case $1 in
-f | --file) shift #don't understand the shift #No.1
filename=$1 ;;
-i | --interactive) interactive=1
;;
-h | --help) usage
exit;;
*) echo "usage >&2 exit 1";;
esac
shift # don't understand the shift #2
done
#interactive mode
if [[ -n $interactive ]]; then
echo "do something"
fi
#output html page
if [[ -n $filename ]]; then
if touch $filename && [[ -f $filename ]]; then
echo "write_html_page > $filename" #debug
else
echo "$program: Cannot write file $filename " >&2
exit 1
fi
else
echo "write_html_page to terminal" # debug
fi
测试
$ bash question.sh -f test
write_html_page > test
$ bash question.sh -f
write_html_page to terminal
当我删除 shift
并将 filename = $ 1
更改为 filename = $ 2
$ bash question.sh -f
write_html_page to terminal
# it works properly
$ bash question.sh -f test
usage >&2 exit 1
write_html_page > test
# it almost function nicely except that `usage >&2 exit 1` is executed.
所以不能完全用 filename = $ 2
代替 shift
.
底部的第二个班次(如果删除),循环会无休止地运行.
The second shift at the botton if deleted, the loop run endlessly.
我可以直观地解释 shift
吗?
在其他语言中我找不到这种神奇的命令.
Could I interpret shift
intuitively?
I did't find such a magic command in other languages.
推荐答案
shift
将删除第一个位置参数,并将所有其他参数左移.
shift
will remove the first positional argument, and shift every other argument left one.
例如,让我们考虑以下内容:
For example, let's consider the following:
#!/bin/bash
echo "$@"
shift
echo "$@"
shift
echo "$@"
鉴于 echo"$ @"
将打印所有参数,如果要运行此参数,则会发生以下情况:
Given that echo "$@"
will print all of the arguments, if you were to run this, then the following would happen:
./test.bash 1 2 3 4 5
echo "$@" # prints 1 2 3 4 5
shift # Removes 1 and shifts everything else along
echo "$@" # prints 2 3 4 5
shift # shifting again
echo "$@" # prints 3 4 5
在您的示例中,脚本正在解析所有标志. -i
和 -h
只是开关,不处理以下参数.但是, -f
需要一个文件名
.
In your example, the script is parsing all of the flags. -i
and -h
are just switches and handle no following arguments. However, -f
requires a filename
.
第二个 shift
将处理该标志,移位参数,然后再次对其进行处理.因此,您可以具有 ./program.bash -i -f文件名
. -i
将移位第二个移位,然后在下一个循环中处理文件名.
The second shift
will process the flag, shift the arguments, and then process them again. Therefore you can have ./program.bash -i -f filename
. The -i
will be shifted by the second shift, and then the filename will be processed on the next loop.
如果要运行 ./program.bash -f filename -i
,则需要将 filename
与 -f
.因此,在 -f
的case块上有一个额外的移位.在此示例中, -f
将在case块内移动,然后 filename
将移动第二个 shift
.然后循环将再次运行以处理其他任何标志.
If you were to run ./program.bash -f filename -i
, then the filename
would need to be shifted along with the -f
. Therefore, on the case block for -f
there is an extra shift. In this example, -f
would be shifted inside the case block, and then filename
would be shifted by the second shift
. Then the loop would run again to process any further flags.
由于while循环是 [[[-n $ 1]]
,因此循环将一直运行,直到没有更多参数为止.
As the while loop is [[ -n $1 ]]
, the loop will run until there are no more arguments.
这篇关于与直接重新分配值相比,在Shell脚本中进行移位的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!