0

我正在编写一个小型 bash 脚本,并尝试通过以下方式创建一个目录:

mkdir ~/deploy.$1

我认为它应该产生 deploy.scriptFoo 或 1 美元的价值。

它只是产生“部署”。并省略 $1 变量。我已经在输出中测试了 $1 变量,并且我很肯定它正在被传递到脚本中。有任何想法吗?

4

1 回答 1

0

The Problem

The $1 position parameter is the first argument to your script, not the name of the script itself.

The Solution

If you want the script name, use $0. For example, given this sample script stored in /tmp/param_test.sh:

#!/bin/bash
mkdir "/tmp/deploy.$(basename "$0" .sh)"
ls -d /tmp/deploy*

the script ignores any arguments, but correctly returns the following output:

/tmp/deploy.param_test

If you want to vary the name, then you have to use a positional parameter in your script and call the script with an argument. For example:

#!/bin/bash
mkdir "/tmp/deploy.$1"
ls -d /tmp/deploy*

On the command line, you pass the argument to your script. For example:

$ bash /tmp/param_test.sh foo
/tmp/deploy.foo

See Also

http://www.gnu.org/software/bash/manual/bashref.html#Positional-Parameters

于 2012-06-24T22:07:34.293 回答