求助用sed删除首字符失败

sh/bash/dash/ksh/zsh等Shell脚本
回复
chunhe
帖子: 45
注册时间: 2007-04-05 13:26

求助用sed删除首字符失败

#1

帖子 chunhe » 2010-05-09 18:15

我有个文本文件是这种格式

2| interface g 0/0/1
8| shut
1| exit

第一列数字表示命令发送到设备需要暂停的时间,后面的表示要在路由器上执行的命令。

我用一个shell脚本来生成expect脚本

cat commandlistfile | while read commands

do
pausetime=`echo $commands | awk -F"|" '{print $1}'`

command = `echo $commands| sed -e s/$pausetime//g | sed -e s/|//g `

echo "expect "*#" {send \"$command\r\r\"}" >>$ssh_tmp$ip

echo "sleep "$pausetime"; " >> $ssh_tmp$ip
done

期望发送到$ssh_tmp$ip里面的字符串为:

expect *# {send "interface g 0/0/1\r\r"}
sleep 2;
expect *# {send "shut\r\r"}
sleep 8;
expect *# {send "exit\r\r"}
sleep 1;

实际发送过去的为:

expect *# {send "\r\r"}
sleep 2;
expect *# {send "\r\r"}
sleep 8;
expect *# {send "\r\r"}
sleep 1;

请大家帮忙看看到底是哪里错了
taijirobot
帖子: 13
注册时间: 2008-10-16 7:41

Re: 求助用sed删除首字符失败

#2

帖子 taijirobot » 2010-05-13 10:04

代码: 全选

command=`echo $commands| sed -e s/$pausetime//g | sed -e s/|//g `
换成这样应该就行了吧:

代码: 全选

command=`echo $commands | sed -e 's/^[0-9]| //g'`
taijirobot
帖子: 13
注册时间: 2008-10-16 7:41

Re: 求助用sed删除首字符失败

#3

帖子 taijirobot » 2010-05-13 10:09

或者这样(刚才少了个星号):

代码: 全选

command=`echo $commands | sed -e 's/^[0-9]*| //g'`
回复