내 OpenWrt 라우터는 Public
내 Samba 공유에 대한 몇 가지 기본 매개변수로 구성되었습니다 /etc/samba/smb.conf
. 이제 이 공유에 몇 가지 추가 매개변수를 추가해야 하므로 이를 Public
구성의 공유 섹션 마지막 줄 에 추가하고 싶습니다 .
해당 섹션 뒤에 텍스트를 추가하는 방법을 알아냈지만 이렇게 하면 텍스트가 첫 번째 줄에 표시됩니다.
[Public]
path = /srv/public
create mask = 0700
directory mask = 0700
read only = yes
guest ok = yes
sed -i '/\[Public\]/a hello world' /tmp/sed.sh
산출
[Public]
hello world <--
path = /srv/public
create mask = 0700
directory mask = 0700
read only = yes
guest ok = yes
그러나 섹션의 마지막 줄 뒤에 텍스트를 배치하고 싶습니다.
원하는 출력
[Public]
path = /srv/public
create mask = 0700
directory mask = 0700
read only = yes
guest ok = yes
hello world <--
또한 모든 코드를 한 줄에 넣지 않고도 여러 줄을 추가할 수 있도록 heredoc(EOF)를 추가하는 방법이 있습니까?
전체 구성
[global]
netbios name = OpenWrt-AP1
interfaces = br-lan
server string = Samba on OpenWRT
unix charset = UTF-8
workgroup = WORKGROUP
## This global parameter allows the Samba admin to limit what interfaces on a machine will serve SMB requests.
bind interfaces only = yes
## time for inactive connections to-be closed in minutes
deadtime = 15
## disable core dumps
enable core files = no
## set security (auto, user, domain, ads)
security = user
## This parameter controls whether a remote client is allowed or required to use SMB encryption.
## It has different effects depending on whether the connection uses SMB1 or SMB2 and newer:
## If the connection uses SMB1, then this option controls the use of a Samba-specific extension to the SMB protocol introduced in Samba 3.2 that makes use of the Unix extensions.
## If the connection uses SMB2 or newer, then this option controls the use of the SMB-level encryption that is supported in SMB version 3.0 and above and available in Windows 8 and newer.
## (default/auto,desired,required,off)
#smb encrypt = default
## set invalid users
invalid users = root
## map unknow users to guest
map to guest = Bad User
## allow client access to accounts that have null passwords.
null passwords = yes
## The old plaintext passdb backend. Some Samba features will not work if this passdb backend is used. (NOTE: enabled for size reasons)
## (tdbsam,smbpasswd,ldapsam)
passdb backend = smbpasswd
## Set location of smbpasswd ('smbd -b' will show default compiled location)
#smb passwd file = /etc/samba/smbpasswd
## LAN (IPTOS_LOWDELAY TCP_NODELAY) WAN (IPTOS_THROUGHPUT) WiFi (SO_KEEPALIVE) try&error for buffer sizes (SO_RCVBUF=65536 SO_SNDBUF=65536)
socket options = IPTOS_LOWDELAY TCP_NODELAY
## If this integer parameter is set to a non-zero value, Samba will read from files asynchronously when the request size is bigger than this value.
## Note that it happens only for non-chained and non-chaining reads and when not using write cache.
## The only reasonable values for this parameter are 0 (no async I/O) and 1 (always do async I/O).
## (1/0)
#aio read size = 0
#aio write size = 0
## If Samba has been built with asynchronous I/O support, Samba will not wait until write requests are finished before returning the result to the client for files listed in this parameter.
## Instead, Samba will immediately return that the write request has been finished successfully, no matter if the operation will succeed or not.
## This might speed up clients without aio support, but is really dangerous, because data could be lost and files could be damaged.
#aio write behind = /*.tmp/
## lower CPU useage if supported and aio is disabled (aio read size = 0 ; aio write size = 0)
## is this still broken? issue is from 2019 (NOTE: see https://bugzilla.samba.org/show_bug.cgi?id=14095 )
## (no, yes)
#use sendfile = yes
## samba will behave as previous versions of Samba would and will fail the lock request immediately if the lock range cannot be obtained.
#blocking locks = No
## disable loading of all printcap printers by default (iprint, cups, lpstat)
load printers = No
printcap name = /dev/null
## Enabling this parameter will disable Samba's support for the SPOOLSS set of MS-RPC's.
disable spoolss = yes
## This parameters controls how printer status information is interpreted on your system.
## (BSD, AIX, LPRNG, PLP, SYSV, HPUX, QNX, SOFTQ)
printing = bsd
## Disable that nmbd is acting as a WINS server for unknow netbios names
#dns proxy = No
## win/unix user mapping backend
#idmap config * : backend = tdb
## Allows the server name that is advertised through MDNS to be set to the hostname rather than the Samba NETBIOS name.
## This allows an administrator to make Samba registered MDNS records match the case of the hostname rather than being in all capitals.
## (netbios, mdns)
mdns name = mdns
## Clients that only support netbios won't be able to see your samba server when netbios support is disabled.
#disable netbios = Yes
## Setting this value to no will cause nmbd never to become a local master browser.
#local master = no
## (auto, yes) If this is set to yes, on startup, nmbd will force an election, and it will have a slight advantage in winning the election. It is recommended that this parameter is used in conjunction with domain master = yes, so that nmbd can guarantee becoming a domain master.
#preferred master = yes
## (445 139) Specifies which ports the server should listen on for SMB traffic.
## 139 is netbios/nmbd
#smb ports = 445 139
## This is a list of files and directories that are neither visible nor accessible.
## Each entry in the list must be separated by a '/', which allows spaces to be included in the entry. '*' and '?' can be used to specify multiple files or directories as in DOS wildcards.
veto files = /Thumbs.db/.DS_Store/._.DS_Store/.apdisk/
## If a directory that is to be deleted contains nothing but veto files this deletion will fail unless you also set the delete veto files parameter to yes.
delete veto files = yes
################ Filesystem and creation rules ################
## reported filesystem type (NTFS,Samba,FAT)
#fstype = FAT
## Allows a user who has write access to the file (by whatever means, including an ACL permission) to modify the permissions (including ACL) on it.
#dos filemode = Yes
## file/dir creating rules
#create mask = 0666
#directory mask = 0777
#force group = root
#force user = root
#inherit owner = windows and unix
################################################################
######### Dynamic written config options #########
[Public]
path = /srv/public
create mask = 0700
directory mask = 0700
read only = yes
guest ok = yes
[Multimedia]
path = /srv/multimedia
create mask = 0700
directory mask = 0700
read only = yes
guest ok = yes
답변1
구성 파일에 추가할 행이 있는 heredoc과 함께 gnu sed를 사용하십시오.
add=$(sed -e 's:^:\t:;s:\\:&&:g;$!s:$:\\:' <<!
---> Hello, world! <---
---> ok tata by by <---
!
)
sed -e '
/^\[Public]/,/^$/!b
/./{
$a\
'"$add"'
}
//!i\
'"$add"'
' /etc/samba/smb.conf
- 들여쓰기 수준은 탭입니다.
답변2
awk
나는 이것을 사용할 것이다
awk '/^\[Public\]/ {toggle=1} toggle && $0=="" {toggle=0; printf "\t%s\n", "hello world <--"} 1' /tmp/sed
일치하면 [Public]
토글을 설정한 다음 길이가 0인 첫 번째 줄에서 토글을 재설정하고 원하는 텍스트를 삽입합니다.
awk '
/^\[Public\]/ {toggle=1} # or: $0=="[Public]"
toggle && $0=="" {toggle=0; printf "\t%s\n", "hello world <--"}
1
' /tmp/sed
답변3
약간의 트릭을 사용하면 awk
다음 블록이 이 블록 다음에 시작하여 레코드 구분 기호 역할을 한다고 가정합니다 \n[
. 마지막 레코드 구분 기호로 인해 마지막 행을 제거합니다 head
.
awk 'BEGIN {RS="\n\\[";ORS="\n["}
/Block 1/ {$0 = $0"\toption3 = value3\n"} {print}' 1 |
head -n-2
네펠레
other config
#and a comment
[Block 1]
option1 = value1
option2 = value2
[Block 2]
option1 = value1
option2 = value2
산출
other config
#and a comment
[Block 1]
option1 = value1
option2 = value2
option3 = value3
[Block 2]
option1 = value1
option2 = value2