相关疑难解决方法(0)

连接所有参数并用双引号将它们包装起来

function foo() {
A=$@...
echo $A
}

foo bla "hello ppl"
Run Code Online (Sandbox Code Playgroud)

我希望输出为:
"bla""hello ppl"

我需要做什么而不是省略号?

string bash shell concatenation

18
推荐指数
3
解决办法
2万
查看次数

为什么我的shell脚本在第二次尝试失败?

此脚本应接受一组搜索字词,并返回格式化的网址以搜索Google.

$ ./google_search.sh albert einstein
https://www.google.com/search?q=albert+einstein
Run Code Online (Sandbox Code Playgroud)

它做得很好,所以我决定添加一个选项来搜索特定网站,或者使用-s或-S标记忽略该网站.

$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein+site%3Awikipedia.org
Run Code Online (Sandbox Code Playgroud)

这在您第一次运行脚本时有效,但在每次后续尝试时都会失败.

$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein
$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein
Run Code Online (Sandbox Code Playgroud)

打开新的终端窗口或重新启动终端都可以清除此问题,并在失败前再尝试一次.

剧本:

#!/bin/bash

# original source of concatenate_args function by Tyilo:
# http://stackoverflow.com/questions/9354847/concatenate-inputs-in-bash-script
function concatenate_args
{
    string=""
    ignorenext=0
    for a in "$@" # Loop over arguments
    do
        if [[ "${a:0:1}" != "-" && $ignorenext = 0 ]] # Ignore flags (first character is -)
        then
            if [[ …
Run Code Online (Sandbox Code Playgroud)

bash darwin getopts

3
推荐指数
1
解决办法
186
查看次数

标签 统计

bash ×2

concatenation ×1

darwin ×1

getopts ×1

shell ×1

string ×1