I'm new to programming (/scripting?), as this will make clear.
Trying to create a re-usable script automate the install/config of new instances of linux.... as I keep breaking them. In this case I'm mucking around with /etc/pam.d/sshd
And learning... sed is definitely making me learn about quotes, breaks and all the basics.
I've got it working as you can see but have a few questions (and I suspect someone will point out how fragile this method is)
Variable file - being passed as an argument to the sedinsert.sh
sedfind="# Standard Un\*x authentication." #sedfind=".* Standard Un\*x authentication" sedinsert="I am a \nmulti line variable \nand I kind of work" sedfile=sshd
I'm trying to get this to do the heavy lifting i.e. I'll escape any characters needed in the sed command or use wildcards here as then I just have to change the variable file on updates once and it's, to me at least, a bit cleaner.
It also massively helps my understanding as I can easily see how changes affect the output/ results.
Q1. Am I right with my quotes and new lines here. It seems to work but is there another way? I get a horrible feeling I'll hit a character soon that will break this - any examples?
Q2. Is it possible to split variables onto multiple lines to make this file easier to work with, when necessary, (e.g. similar to using \ at the end of the line) - bearing in mind the sourcing and sed coming up.
Perhaps another command within the script to deal with this? (Grep? or another sed?.
set -x source $1 sed -i "\|$sedfind|a\ \n$sedinsert" $sedfile
I'm trying to keep this as clean as possible and also to not use quotes around the variables. I've currently another one for replacing at the moment but I'll probably look at passing the command as a variable next
I'm not absolutely wedded to that idea but hopefully the reasons will make sense... i.e. This will be used to alter all kinds of files with 'difficult' characters in them e.g. @#" etc
Q. Because of this I'm having to put the first new line in the command itself (|a\ /n ....). Would there be someway to put this in the variable itself in case I don't want it/ want to include multiple blank lines?
It's nice in sed that you can set the pattern delimiter (correct term?) from \ to | as I have here.
But every time I think I've found a good one to use I sure enough find that's included in the next file I look at
Q. Is there some suggested/ standard obscure delimiter to use that's rarely used elsewhere - so I don't keep finding it in a file e.g. some russian character (apologies to anyone Russian) rather than #|, etc.
I'm just a little confused why everyone uses the common ones, which seem to cause people all kinds of confusion ( e.g. / in file paths) going by other answers
Question 4: Now how should I really be going about all this?
Few lines of pam.sshd as an example
# PAM configuration for the Secure Shell service # Standard Un*x authentication. @include common-auth # Disallow non-root logins when /etc/nologin exists. account required pam_nologin.so
Sorry for the length - thought it worth explaining the whole process