Whilst in the users home directory, here is a script that will run a command, and then remove itself. In the code below, the command "touch newfile" will create a file called "newfile" if the file ".profile" exists. You could write this code into a file called "disappear", give it execute permissions, and then run it in your home directory, and it will remove itself upon execution. You can make the command whatever you like.
Code:
#!/bin/bash
if [ -e $HOME/.profile ]
then
touch $HOME/newfile
rm $0
fi
To accomplish the rest of your wishes, to run a command once when the new user logs in for the first time, you will need to do few other things. There are going to be a number of possible methods. Here is one route.
Add the "disappear" script (with modifications like those suggested below) to /etc/skel with execute permissions. You need to be root to do that. The files and dotfiles in /etc/skel are placed in the new user's directory when the new user is created by the adduser command. You will see that .profile file among others in /etc/skel. All these files will be placed in the new users home directory with the user's permissions.
Place the command "./disappear" at the end of the /etc/skel/.profile file, say, for example, on line 29.
The "disappear" script above needs to be modified so that it not only runs the command you want to run (in the example above it's "touch newfile"), but also removes that line 29 in .profile with the "./disappear" command and restores the .profile to its original code so that no trace is left of that "./disappear" command. To do that you could add to the code above, after the touch command a couple of lines like:
Code:
sed '29d' $HOME/.profile > /tmp/profile
cat /tmp/profile > $HOME/.profile
This code will remove from .profile the "./disappear" command if it's on line 29, and return it to it's original code, and remove itself. No traces will be visible to the user in their home directory. There's perhaps some inelegance to this, but it worked. I have hard coded the line 29 which in normal scripting I wouldn't do but would need more code to find the appropriate line in .profile to delete. I was more concerned with proof of concept for the moment.