Bash 101 Help Please
Matt Graham
danceswithcrows at usa.net
Thu Jul 25 12:35:12 MST 2013
From: Lisa Kachold
> createNewSambaAllow(){
> # Copy to backup the config:
> cp /etc/samba/smb.conf /etc/samba/smb.conf-$_now
> # stuff the line we need to append to into a variable called $oldline
> # this is supposed to look like this "valid users = unique thing1 thing2
thing3 alice"
> oldline = grep unique /etc/samba/smb.conf*
> # delete the old line in the smb.conf*
> sed '/unique/d' /etc/samba/smb.conf >>/etc/samba/smb.conf
> # dump the old line and the new user into the bottom of smb.conf
> echo $oldline && echo $username >> /etc/samba/smb.conf
> # restart samba
> /etc/init.d/samba restart
> echo "Samba Config Added and Daemon restarted"
> }
[snip]
> createNewSambaAllow $username
[more snip]
What I *think* you want this thing to do is to replace the line containing
"unique" with a new line that has " $username" appended to it. There are many
ways to do this. The function above won't work, or at least it didn't when I
tested it. This, OTOH:
createNewSambaAllow(){
# Copy to backup the config:
cp /etc/samba/smb.conf /etc/samba/smb.conf-$_now
# stuff the line we need to append to into a variable called $oldline
# this is supposed to look like this "valid users = unique thing1
thing2 thing3 alice"
old=`grep unique /etc/samba/smb.conf`
# delete the old line in the smb.conf
sed "/unique/d" /etc/samba/smb.conf > temp.txt
# dump the old line and the new user into the bottom of smb.conf
old=`echo $old | sed -e "s/$/ $1/"`
echo $old >> temp.txt
mv -f temp.txt /etc/samba/smb.conf
# restart samba
#/etc/init.d/samba restart
echo "Samba Config Added and Daemon restarted"
}
# end function
This appears to do what you want while retaining the structure, but using sed
in place should also work and make the function collapse into a single line:
sed -e "s/\(.*valid users.*\)/\1 $username/" -i /etc/samba/smb.conf
...which would probably be a win.
This might've been easier to do in Perl, but then most things are :-)
--
Matt G / Dances With Crows
The Crow202 Blog: http://crow202.org/wordpress/
There is no Darkness in Eternity/But only Light too dim for us to see
More information about the PLUG-discuss
mailing list