LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH Scripting Questions (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-questions-4175581878/)

d.standel 06-09-2016 02:25 PM

BASH Scripting Questions
 
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.

#!/bin/bash
FILENAME=/home/`whoami`/_${RANDOM}
touch ${FILENAME}
echo cut -d -f1,3,4 >> ${FILENAME}
exit 0

Any help improving or correcting my efforts would be greatly appreciated.

Thank you,
Dustin

keefaz 06-09-2016 02:45 PM

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?

jpollard 06-09-2016 02:48 PM

You are looking at the wrong command... whoami only tells you who you are, not who is logged in (including you)

That would be the "who" command...

As for the chmod command... Obviously it has to be done AFTER the file exists.

"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"

Nope. No one is logged in at that time.

Turbocapitalist 06-09-2016 10:00 PM

Quote:

Originally Posted by d.standel (Post 5558546)
Code:

#!/bin/bash
FILENAME=/home/`whoami`/_${RANDOM}
touch ${FILENAME}
echo cut -d -f1,3,4 >> ${FILENAME}
exit 0


"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.

d.standel 06-10-2016 11:11 AM

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.

Thanks for your inputs.
Dustin

keefaz 06-10-2016 11:37 AM

Quote:

Originally Posted by d.standel (Post 5558938)
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?

ondoho 06-11-2016 03:02 AM

Quote:

Originally Posted by d.standel (Post 5558938)
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 (Post 5558938)
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.


All times are GMT -5. The time now is 11:47 PM.