Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write a BASH script that produces a unique output about all users who are logged in, and a different output if no one is logged in. I was thinking pulling the UID from /etc/passwd would be a good option here or enter a 0/n for no one logged in.
The output needs to go to a file in the home directory. So I need to pipe the STDOUT to a file in home directory.
I was told to use chmod to give it permission to run at startup. (I've read the man page and the documentation at gnu.org but can't find anything on the chmod giving a program permission to run at startup. It seems like that command is used to establish permissions for various users to access the contents of the file.) Also, I'm a little confused as to when to execute the chmod. for the most part it seems the chmod is external to the script after it is saved. From my understanding, I think I would have to set permissions to INIT for it to run at startup, and that is stored in the file header in Linux.
A copy of the script as I have developed it so far is below.
I don't get what the script is supposed to do...
Display a message like a greeting message at the terminal when an user login and other users are logged, and another greeting message when the user login and no one is logged?
"chmod" will only make the script executable. To also make it run on startup you will have to launch it from rc.local or a more complicated method.
"w" mentioned above will provide you with user information. As to being different if no one is logged on, "w" simply prints the headers then and no user data:
Code:
22:40:00 up 9:50, 0 users, load average: 0.56, 0.13, 0.04
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
If you use the -h option with "w", then you will get no headers at all and your file will be empty if there are no users. You can "sort" to get a list of names.
Code:
w -h | cut -d ' ' -f 1 | sort -u
About generating a temporary file, there is a program "tempfile" which will create a random one for you and print out its name. "tempfile" is pre-installed in most distros.
Code:
filename=$(tempfile -d ${HOME} -p _);
The home directory's name is stored in HOME. So that can be used instead.
Keefaz - the output should be to a file such that an administrator can run the script, and receive a file with the list of everyone whoi is logged onto the system and disctinct separate output if no one is on the system.
turbocapitalist and pollard - thanks for the advice.
I am new to Linux (I've only had it installed for about a month and running it in Oracle VM VirtualBox on top of Windows to try it out before unleashing it on the rest of the family) and trying to learn some basic BASH scripting and administrator type functions, so your replies are very helpful. This is my first attempt at scripting in a shell, so I was trying to keep the ideas basic and use common commands.
Keefaz - the output should be to a file such that an administrator can run the script, and receive a file with the list of everyone whoi is logged onto the system and disctinct separate output if no one is on the system.
Thanks so to resume, admin logs in and executes a script that produces a file in which its content states who are and who aren't logged in at the time the script is executed?
I've only had it installed for about a month and running it in Oracle VM VirtualBox on top of Windows to try it out
it is very likely that only 1 user is logged in - you - in this case.
i still fail to see why you need this script.
anyhow, this command, run when you log in, will paste a list of all users logged in, to a file called who_is_logged_in in your $HOME directory:
Code:
who > $HOME/who_is_logged_in
if no user is logged in, the file will be empty.
if you need to know where to put this command so it is executed at login, we need to know what sort of linux you are using.
if you really want to run this as a system service before any user logs in, we need to know what sort of linux you are using.
but there is no use for this really.
Quote:
Originally Posted by d.standel
before unleashing it on the rest of the family
if your family have been using proprietary OSs all their life, i wouldn't "unleash" anything, even with my slightly better knowledge of linux.
i twice convinced firends to install linux, with the added promise of a) keeping their data intact and b) help them; i am not likely to do ever do that again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.