bash $f 在 shell 脚本中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34081194/
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
what does $f means in shell script?
提问by Laura
#!/bin/bash
Files=`(find . -type f)`
for f in $Files
do
if [ "$(dirname $f)" != "." ]
then
# these are files in a subdirectory
x=$(dirname $f) #get the name of the file with the directory
y=${x:2} #remove ./ from the name
echo $(basename $f) \($y\)
else
# these are files in the current directory
echo $(basename $f)
fi
done
I'm having a hard time understanding what the line
我很难理解线路是什么
Files=1(find . -type f)`
means? Is it finding each and every file in the directory? Does "type f" means finding every "file" type? Also, What's the function of $f?
方法?它是否找到目录中的每个文件?“type f”是否意味着找到每个“文件”类型?另外,$f 的功能是什么?
I'm starting out in shell so any help would be really helpful!
我从 shell 开始,所以任何帮助都会非常有帮助!
回答by user.friendly
Sorry, I don't have enough reputation yet to add a comment. Elliott has the right answer regarding your 'f' variable name. To answer your follow up a question though, the significance of the period does not have anything to do with the type of file.
抱歉,我还没有足够的声望来添加评论。Elliott 对您的“f”变量名称有正确的答案。不过,要回答您的后续问题,句点的重要性与文件类型无关。
In linux, '.' represents your pwd or present working directory. Your pwd's parent directory is represented by '..'. The find command requires a path be supplied, whether relative or absolute. The '.' accomplishes this by effectively saying, "search in the current directory (.) and all sub-directories". Find is naturally recursive IIRC.
在 linux 中,'.' 代表您的密码或当前工作目录。您的密码的父目录由“..”表示。find 命令需要提供路径,无论是相对的还是绝对的。这 '。' 通过有效地说“在当前目录 (.) 和所有子目录中搜索”来实现这一点。Find 是自然递归的 IIRC。
Happy hacking!
快乐黑客!
回答by Elliott Frisch
See for f in $Files
... The $f
referes to that for
loop variable. It could also be written
请参阅for f in $Files
...$f
引用该for
循环变量。也可以这样写
Files=`(find . -type f)`
for fred in $Files
do
if [ "$(dirname $fred)" != "." ]
then
# these are files in a subdirectory
x=$(dirname $fred) #get the name of the file with the directory
y=${x:2} #remove ./ from the name
echo $(basename $fred) \($y\)
else
# these are files in the current directory
echo $(basename $fred)
fi
done
And yes, -type f
means "files".
是的,-type f
意思是“文件”。