string shell 字符串错误替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12831808/
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-09-09 01:38:23  来源:igfitidea点击:

shell string bad substitution

stringshellsubstitution

提问by cattail

I'm new to shell programming. I intend to get directory name after zip file was extracted. The print statement of it is

我是 shell 编程的新手。我打算在解压缩 zip 文件后获取目录名称。它的打印语句是

$test.sh helloworld.zip
helloworld

Let's take a look at test.sh:

我们来看一下test.sh:

#! /bin/sh
length=echo `expr index "" .zip`
a=    
echo $(a:0:length}

However I got the Bad substitution error from the compiler.

但是我从编译器那里得到了错误的替换错误。

And when I mention about 'shell'.I just talking about shell for I don't know the difference between bash or the others.I just using Ubuntu 10.04 and using the terminal. (I am using bash.)

当我提到“shell”时。我只是在谈论 shell,因为我不知道 bash 或其他人之间的区别。我只是使用 Ubuntu 10.04 并使用终端。(我正在使用 bash。)

采纳答案by Jonathan Leffler

If your shell is a sufficiently recent version of bash, that parameter expansionnotation should work.

如果您的 shell 是 的足够新版本bash,则该参数扩展表示法应该有效。

In many other shells, it will not work, and a bad substitutionerror is the way the shell says 'You asked for a parameter substitution but it does not make sense to me'.

在许多其他 shell 中,它不起作用,并且bad substitution错误是 shell 说“您要求进行参数替换,但对我来说没有意义”的方式。



Also, given the script:

另外,给定脚本:

#! /bin/sh
length=echo `expr index "" .zip`
a=    
echo $(a:0:length}

The second line exports variable lengthwith value echofor the command that is generated by running expr index "$1" .zip. It does not assign to length. That should be just:

第二行导出length带有echo通过运行生成的命令的值的变量expr index "$1" .zip。它不分配给length. 那应该只是:

length=$(expr index "${1:?}" .zip)

where the ${1:?}notation generates an error if $1is not set (if the script is invoked with no arguments).

${1:?}如果$1未设置,则表示法生成错误(如果不带参数调用脚本)。

The last line should be:

最后一行应该是:

echo ${a:0:$length}

Note that if $1holds filename.zip, the output of expr index $1 .zipis 2, because the letter iappears at index 2 in filename.zip. If the intention is to get the base name of the file without the .zipextension, then the classic way to do it is:

请注意,如果$1hold filename.zip,则输出expr index $1 .zip为 2,因为该字母i出现在 中的索引 2 处filename.zip。如果目的是获取没有.zip扩展名的文件的基本名称,那么经典的方法是:

base=$(basename  .zip)

and the more modern way is:

更现代的方式是:

base=${1%.zip}

There is a difference; if the name is /path/to/filename.zip, the classic output is filenameand the modern one is /path/to/filename. You can get the classic output with:

它们是有区别的; 如果名称为/path/to/filename.zip,则经典输出为filename,现代输出为/path/to/filename。您可以通过以下方式获得经典输出:

base=${1%.zip}
base=${base##*/}

Or, in the classic version, you can get the path with:

或者,在经典版本中,您可以通过以下方式获取路径:

base=$(dirname )/$(basename  .zip)`.)

If the file names can contain spaces, you need to think about using double quotes, especially in the invocations of basenameand dirname.

如果文件名称可以包含空格,则需要考虑使用双引号,尤其是在的调用basenamedirname

回答by Brian Davis

Try running it with bash.

尝试用 bash 运行它。

bash test.sh helloworld.zip

bash test.sh helloworld.zip

-likewise-

-同样地-

"try changing the first line to #!/bin/bash" as comment-answered by – @shellter

“尝试将第一行更改为#!/bin/bash”作为评论回答 – @shellter

回答by Gilles Quenot

Try that in bash:

试试看bash

echo 
len=$(wc -c <<< "")
a=".zip"
echo ${a:0:$len}

Adapt it to fit your needs.

调整它以满足您的需求。