为什么sed不接受shell变量?

时间:2013-08-13 17:06:30

标签: linux shell sed

我编写了这个小shell函数来修改samba conf文件。 但似乎,它在这一行sed -e "$t1,$t2 d" $CFGFILE > $TMPFILE的某个地方失败了。错误是:

Doing Samba customization...
/etc/samba/smb.conf
/etc/samba/smb.conf.tmp
sed: -e expression #1, char 1: unknown command: `,'

我在上一个sed面临同样的问题: sed -e "$t1, $ d" $CFGFILE > $TMPFILE //目的从行号t1删除到结束

这是shell函数:

updateSmbConf()
{
SMBCFG="$DIR_ETCSAMBA/smb.conf"
SMBCFGTMP="$SMBCFG.tmp"

echo $SMBCFG
echo $SMBCFGTMP

# Fix for bug #3147
sed "s/INSITEONE-CIFS/DELL-CIFS/" $SMBCFG > $SMBCFGTMP
sed "s/InSiteOne CIFS Archive/DCCA CIFS Archive/" $SMBCFG > $SMBCFGTMP
cp $SMBCFGTMP $SMBCFG
rm $SMBCFGTMP

t1=` grep -n "\[Archive\]" $CFGFILE | cut -f1 -d:`
t2=$(($t1+11-1)) # Remove the next 11 lines
sed -e "$t1,$t2 d" $CFGFILE > $TMPFILE
cp $TMPFILE $CFGFILE
rm $TMPFILE

#[SampleHA]
#t1=` grep -n "\[SampleHA\]" $CFGFILE | cut -f1 -d:`
#sed -e "$t1, $ d" $CFGFILE > $TMPFILE
#cp $TMPFILE $CFGFILE
#rm $TMPFILE
}

这是我想修改的smb.conf:

------------------------------------------------------------------------------------
                     COMPLETE SMB.CONF
------------------------------------------------------------------------------------
cat /etc/samba/smb.conf
# smb.conf is the main Samba configuration file. You find a full commented
# version at /usr/share/doc/packages/samba/examples/smb.conf.SUSE if the
# samba-doc package is installed.
# Date: 2006-07-13
[global]
       guest account = insite1
       oplocks = no
       msdfs root = yes
       workgroup     = INSITEONE-CIFS
       server string = "InSiteOne CIFS Archive"
       security      = SHARE
       syslog        = 0
       max smbd processes = 15
       interfaces = eth* lo
       bind interfaces only = no

#[Archive]
#       hosts allow = 127.0.0.1 10.135.30.135 10.135.30.135
#       hosts deny = ALL
#       vfs objects  = relay:"host = localhost; \
#                             port = 1025;      \
#                             log_level  = 0"
#       comment = InSiteOne Archive File System
#       path = /opt/samba
#       read only = No
#       guest ok = Yes
#       locking = no
[DCCAArchive]
        comment = DCCA Archive File System
        path = /opt/insiteone/fuse-mount/ifm
        read only = No
        public = yes
        writeable = yes
        create mask=0777
        guest ok = Yes
#[SampleHA]
#        comment = High Availability Share
#        path = <Path of Local Archive e.g. /opt/samba3/archive>
#        read only = No
#        guest ok = Yes
#        locking = no
#        hosts allow = 127.0.0.1 OTHERPROXYIP
#        hosts deny = ALL
#        directory mode = 0775
#        create mode = 0664

1 个答案:

答案 0 :(得分:2)

您标记了问题,因此使用GNU sed,您可以执行以下操作:

sed -i '/\[Archive\]/,+11 d' $SMBCFG

这会就地修改文件,因此不需要tmp文件,并匹配包含[Archive]的行,因此不需要grep,并删除它和接下来的11行。