LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-09-2016, 03:25 PM   #1
d.standel
LQ Newbie
 
Registered: Jun 2016
Posts: 6

Rep: Reputation: Disabled
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
 
Old 06-09-2016, 03:45 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
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?
 
Old 06-09-2016, 03:48 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
Old 06-09-2016, 11:00 PM   #4
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 967
Blog Entries: 3

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
Quote:
Originally Posted by d.standel View Post
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.
 
Old 06-10-2016, 12:11 PM   #5
d.standel
LQ Newbie
 
Registered: Jun 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-10-2016, 12:37 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Quote:
Originally Posted by d.standel View Post
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?
 
Old 06-11-2016, 04:02 AM   #7
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,176

Rep: Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835
Quote:
Originally Posted by d.standel View Post
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 View Post
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.

Last edited by ondoho; 06-11-2016 at 04:05 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting questions: Geminias Programming 3 08-06-2007 09:58 AM
Bash Scripting questions (2) rjcrews Programming 3 04-05-2006 01:14 PM
Bash scripting questions stryka Programming 3 11-25-2005 12:46 AM
bash scripting questions nix101 Linux - Newbie 2 09-04-2005 10:26 AM
Bash scripting questions irfanhab Programming 2 06-07-2004 01:13 PM


All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration