2017-05-09 47 views
0

我正在編寫一個linux腳本,它將替換文件的字符串。這裏是我的腳本:
如何將包含路徑的變量插入sed

#!/bin/sh 
var=./video/example.mp4 
`sed -i '' 's/sceneFilePath:.*/sceneFilePath: "$var",/g' test` 

在名爲test的文件中,有這樣一行:

sceneFilePath: "t.mp4", 

我需要的是與下面的線替換此行:

sceneFilePath: "./video/example.mp4", 

這意味着雙引號之間的事情應該由$var所取代。

然而,當我執行我的劇本,我得到sceneFilePath: "$var",

我讀過這樣的回答:Replace a string in shell script using a variable

但是,我得到一個錯誤:

sed: 1: "s/sceneFilePath:.*/scen ...": bad flag in substitute command: 'v'

+0

使用在sed其中不同的分隔符唐不會出現在你的文件路徑/名稱中。單曲#財產以後#$#變種'。你可能不需要'g'標誌。你也不需要使用''''''在子shell中運行它。您可以按原樣運行該命令。 – 123

回答

0
var='.\/video\/example.mp4' 
sed -i '' "s/sceneFilePath:.*/sceneFilePath: \"$var\",/g" test 

否則:

var='./video/example.mp4' 
sed -i '' "s#sceneFilePath:.*#sceneFilePath: \"$var\",#g" test