将文件路径输入bash函数时,我不断获得host = dynamic [英] I keep on getting host=dynamic when inputting the filepath into bash function

查看:45
本文介绍了将文件路径输入bash函数时,我不断获得host = dynamic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我正在尝试创建一个bash函数,该函数将检查文件中是否包含文本.如果文本在文件中,则不发送文本.如果是,请添加文本.我的代码是

I am trying to create a bash function that will check if a file has text in it. If the text is in the file do not text. If it is add the text. My code is

#!/bin/bash
#Function that checks if text (ARGV1) is in a document (ARGV2). Please make ARGV1 an array of strings, with each new line a new entry in the array.

declare -a inputText=("[test]" "host=dynamic" "disallow=all" "allow=alaw" "allow=ulaw" "type=friend" "context=test" "secret=test")

function docCheckNReplace {
    local text=$1
    local document=$2
    echo $document
    local textLen=${#text[@]}
    for ((i=0; i<textLen; i++)); do
        if grep -q "${text[$i]}" $document; then
            echo 'found'
            echo ${test[$i]} 'was found in' $document
        else
            echo 'not found'
            echo ${test[$i]} >> $document
        fi
    done
}
docCheckNReplace ${inputText[@]} /home/kam/Documents/TextingSed.txt

现在,每当我回显输入文件路径参数时,它都会返回"host = dynamic".

Now, whenever I echo out the input filepath argument it returns "host=dynamic".

当我将第一个参数设置为inputText而不是$ {inputText [@]}时,它可以正常工作.

When I set the first argument to inputText and not ${inputText[@]} it works fine.

有人有什么主意吗?

谢谢:)

推荐答案

与其将搜索字符串作为第一个参数传递,不如将其作为vararg传递给参数2及以上.无论使用哪种编程语言,可变数量的参数都必须是最后一个.

Instead of passing search strings as first argument, pass it as a vararg for argument 2 and above. Variable number of arguments has to be last regardless of the programming language.

字符串文字用单引号引起来,从而有效地避免了对潜在的变量/子外壳扩展语法的解析.

String literals are written in single-quote for efficiently avoiding parsing for potential variable/sub-shell expansions syntax.

在变量周围添加双引号.

Adds double quotes around variables.

格式化带有混合文字和变量的字符串时,使用 printf 而不是 echo .

Uses printf rather than echo when formatting strings with mixed literals and variables.

grep 中使用 -F 选项来搜索纯文本,而不是将搜索字符串解释为正则表达式.

Uses the -F option with grep to search for plain text rather than interpreting the search string as a Regex.

#!/usr/bin/env bash

#Function that checks if texts (vararg ARGV2) is in a document (ARGV1).
#Please make ARGV2 an array of strings, with each new line a new entry in the array.

declare -a inputText=('[test]' 'host=dynamic' 'disallow=all' 'allow=alaw' 'allow=ulaw' 'type=friend' 'context=test' 'secret=test')

function docCheckNReplace {
    local document="$1"
    # Shift out document from arguments array
    # Now it only contains vararg search strings
    shift

    echo "$document"
    for search_string; do
        if grep -qF "$search_string" "$document"; then
            echo 'found'
            printf '%s was found in %s.\n' "$search_string" "$document"
        else
            echo 'not found'
            echo "$search_string" >> "$document"
        fi
    done
}

docCheckNReplace '/home/kam/Documents/TextingSed.txt' "${inputText[@]}"

这篇关于将文件路径输入bash函数时,我不断获得host = dynamic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆