linux - 为什么 sed 不接受 shell 变量?

标签 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

最佳答案

您标记了问题 ,所以使用 GNU sed 你可以这样做:

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

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

关于linux - 为什么 sed 不接受 shell 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18215143/

相关文章:

php - 使用 .htaccess 将所有请求重定向到 index.php

linux - 查找服务器上的所有 htaccess 文件

android - 来自具有特殊字符的 macOS 应用程序的 ADB 命令

正则表达式只匹配数字

linux - 使用awk从行尾删除字符串

linux - 为什么 Linux 有手册页和信息页

linux - 我可以检查我安装的是哪个版本的 OpenMP 吗?

php - 如何将 json 或类对象传递给命令行 php 脚本

bash - 替代 for 循环构造

string - 想要更改前缀字符,然后在更改的字符串中添加后缀字符