gpt4 book ai didi

linux - 脚本和空格

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:34 26 4
gpt4 key购买 nike

我有一个简单的 bash 查找和替换脚本“script.sh”:

#!/bin/bash 
find . -type f -name '.' -exec sed -i '' "s/$1/$2/" {} +

当我运行我的命令 ./script.sh foo bar 时,它起作用了。假设现在我的两个输入字符串 $1 和 $2 是句子(带有空格),我怎样才能让脚本将它们中的每一个识别为整个字符串?

最佳答案

在引号中添加每个句子 "

#!/bin/bash
echo $1
echo $2

输出

$ ./tmp.sh "first sentence" "second sentence"
first sentence
second sentence

编辑:

试试这个:

#!/bin/bash
find . -type f -exec sed -i "s/$1/$2/" {} +

输出:

$ cat test1.txt 
ola sdfd
$ ./tmp.sh "ola sdfd" "hello world"
$ cat test1.txt
hello world
$ ./tmp.sh "hello world" "ols asdf"
$ cat test1.txt
ols asdf

关于linux - 脚本和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com