Linux “$1/*”在“for file in $1/*”中是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8778017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:58:14  来源:igfitidea点击:

What does "$1/*" mean in "for file in $1/*"

linuxbashshell

提问by louxiu

The short bash script below list all files and dirs in given directory and its sub. What does the $1/*mean in the script? Please give me some references about it. Thanks

下面的简短 bash 脚本列出了给定目录及其子目录中的所有文件和目录。$1/*脚本中的意思是什么?请给我一些关于它的参考。谢谢

#!/bin/sh

list_alldir(){
    for file in /*
    do
        if [ -d $file ]; then
            echo $file
            list_alldir $file
        else
            echo $file
        fi
    done
}   

if [ $# -gt 0 ]; then 
    list_alldir ""
else
    list_alldir "."
fi

采纳答案by zellio

It's the glob of the first argument considered as a directory

这是被视为目录的第一个参数的 glob

In bash scripts the arguments to a file are passed into the script as $0( which is the script name ), then $1, $2, $3... To access all of them you either use their label or you use one of the group constructs. For group constructs there are $*and $@. ($*considers all of the arguments as one block where as $@considers them delimited by $IFS)

在 bash 脚本中,文件的参数作为$0(即脚本名称)传递到脚本中,然后$1, $2, $3... 要访问所有这些参数,您可以使用它们的标签或使用组结构之一。对于组结构,有$*$@。($*将所有参数视为一个块,其中 as$@认为它们由 分隔$IFS

回答by Bohemian

$1means the first parameter.
for file in $1/*means loop with the variable filehaving the value of the name of each file in the directory named in the first parameter.

$1表示第一个参数。
for file in $1/*表示使用file具有第一个参数命名的目录中每个文件的名称值的变量进行循环。

回答by Rahul sawant

$1 is the first commandline argument. If you run ./asdf.sh a b c d e, then $1 will be a, $2 will be b, etc. In shells with functions, $1 may serve as the first function parameter, and so forth.

$1 是第一个命令行参数。如果您运行 ./asdf.sh abcde,则 $1 将是 a,$2 将是 b,等等。在带有函数的 shell 中,$1 可以作为第一个函数参数,依此类推。