$TMPFILE 的某处失败了。错误是: Do-6ren">
gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:02 28 4
gpt4 key购买 nike

我已经编写了这个小的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com