shell之运用sed将其值存到变量

背景:使用shell脚本,使用sed将字符/替换为\。

一直报错,搜了下资料,终于好了,记录下

错误1:

#!/bin/bash

var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers"
var=${var#*application}
string=$var | sed 's#\/#\\#g'
echo $string

错误2:

#!/bin/bash

var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers"
var=${var#*application}
string=`$var | sed 's:\/:\\:g'`
echo $string

错误3:

#!/bin/bash

var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers"
var=${var#*application}
string=`echo $var | sed 's:\/:\\:g'`
echo $string

正确姿势:

var="/media/sf_Project/self/smarty-frame/application/ApiLoveHouse/Controllers"
var=${var#*application}
echo $var | sed 's:\/:\\:g'
string=$(echo $var | sed 's#\/#\\#g')
echo $string

返回

[root@vbox-nginx shell_command]# ./mkdemo.sh 
\ApiLoveHouse\Controllers
\ApiLoveHouse\Controllers

 

上一篇:杭电oj 1001


下一篇:1001 害死人不偿命的(3n+1)猜想 (15 分)