如何在 Bash 中将目录设置为参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29018944/
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
How to set a Directory as an Argument in Bash
提问by Robert
I am having trouble finding out how to set a directory as an argument in bash.
我无法找到如何在 bash 中将目录设置为参数。
The directory I am trying to have as an argument is /home/rrodriguez/Documents/one.
我试图作为参数的目录是 /home/rrodriguez/Documents/one.
Anywhere I try to look for an answer I see examples like dir = $1 but I cant seem to find an explanation of what this means or how to set it up so that it references my specific file location. Could anyone show me how to set up my variable for my path directory?
在我尝试寻找答案的任何地方,我都会看到 dir = $1 之类的示例,但我似乎无法解释这意味着什么或如何设置它以引用我的特定文件位置。谁能告诉我如何为我的路径目录设置变量?
Adding my code for a better understanding of what im trying to do:
添加我的代码以更好地理解我正在尝试做什么:
#!bin/bash
== 'home/rrodriguez/Documents/one/'
dir =
touch -c $dir/*
ls -la $dir
wc$dir/*
采纳答案by John1024
Consider:
考虑:
#!bin/bash
dir=
touch -c "$dir"/*
ls -la "$dir"
This script takes one argument, a directory name, and touches files in that directory and then displays a directory listing. You can run it via:
该脚本采用一个参数,即目录名称,并访问该目录中的文件,然后显示目录列表。您可以通过以下方式运行它:
bash script.sh 'home/rrodriguez/Documents/one/'
Since home/rrodriguez/Documents/one/
is the first argument to the script, it is assigned to $1
in the script.
由于home/rrodriguez/Documents/one/
是脚本的第一个参数,因此在脚本中分配给它$1
。
Notes
笔记
In shell, never put spaces on either side of the =
in an assignment.
在 shell 中,永远不要=
在赋值的两边放置空格。
I omitted the line wc$dir/*
because it wasn't clear to me what the purpose of it was.
我省略了这一行,wc$dir/*
因为我不清楚它的目的是什么。
I put double-quotes around $dir
to prevent the shell from, among other things, performing word-splitting. This would matter if dir
contains spaces.
我在周围加上双引号是$dir
为了防止 shell 执行分词等操作。如果dir
包含空格,这很重要。