I do like and enjoy writing scripts, it feels challenging to me.
Don't get me wrong: I would never tell anybody how to write their scripts. I just wanted to say, there was another (my) solution.
I once had the issues of this thread myself: ~ is filling with not deleted serverauth-files. So I realized this work-around of deleting them until I found out why and set my X server to terminate correctly (but I forgot - it's many years ago.)
Also, you never know when you need to reuse what you've written once for another case.
I guess you already have some kind of a system where and how you organize your scripts.
Well structured, judicious named directories, a versioning control system and a good way of how to document is not to be underestimated in its value. Sooner or later the stuff you produce grows
large. The earlier you have a sophisticated - your needs suiting system - the better. Otherwise you risk to lose survey.
For example I have a experimenting area where I have small scripts doing one thing, only, answering myself questions like "how can I calculate...?" or "how to measure time" and such things. You not only can learn a lot this way, but it's also a good "toolbox" you may find useful prewritten things in it you may need for "real" scripts later.
Of course, "you can google it all" ... yes. But besides seldom you find the exact thing you actually need, where is the fun in that?
Also i think that it would delete them too after you ran Xorg session and open a term (spawn a shell).
No. ~/.login is executed only once when you login the first time. ~/.cshrc is run every time you spawn a new shell.
dunno what '-e' implies here but this seems to be delete all the .serverauth files, also i don't use bash but csh, the reason i'm writing scripts in csh is because i want to learn more about my shell that i daily drive.
"if file exists" - it's sh file test condition; see
Table B-3. TEST Operators: Files on Advanced Bash-Scripting Guide
It's to prevent an error message for executing rm blank if there weren't any file.
Of course it deletes all. That's the whole point.
Instead of find and separate the recent one from all others, I simply delete all severauth-files
before a X session is started. This way only the one exists the X session produces, which - when not be deleted by a clean terminated X session, will be deleted before the next X session.
Of course it may no solution for every situation (I have no idea how this behaves with a display ["login"] manager), maybe not so good...)
It was just what I did.
The better solution anyway is to make the X server terminate correctly, so no extra serverauths are produced in the first place.
The one or the other serverauth file produced by seldom non clean termination events I simply delete by hand.
However, writing scripts is not only for fun but a very important competence in unix[like] systems.
Personally I also use tcsh for users and root. But - and again this is my very personal taste, absolutely not telling you what to do or not - I also once started myself learning scripting in csh - "If I use this shell, then why not learn to script in it", right? I learned to program in C long before I started scripting. I took a first glance at bash scripting and thought: "They must be nuts! No, I prefer to stick to my familar C syntax."
Well, as understandable as this sounds - the field of application of both languages are completely different. The target area of shell scripting is doing things with files, while C not only plays in another league, but it's a complete other game. I admit: To get into sh/bash scripting seems to be a bit odd for the start (especially testing is kind of weird (those spaces, especially after [ and before ] are important {space is shell's default delimiter}), and quoting is another special mission to be understood, but you'll find out quickly: There is a good reason why sh's syntnax is the way it is.
On scripting in csh I gave up pretty quickly. Scripting in csh is kind of masochism, at least to me, and I would not recommend to learn it first - if even at all. Quickly one faces its limits, which means you would need way less effort doing the same in sh (again, I only run scripts in its language) -
if it's even possible within csh with an acceptable effort.
Frankly, before I'd torture myself with csh scripting I recommend to learn first scripting in sh, Python, or Perl.
Some things I also really do in C.
But I also do things on my machine myself just for fun, don't care if it's always most efficient, or even useful.
And of course everbody find the fun elsewhere.

Enjoy!