Saturday, 28 May 2011

Bash script for login details logs

Hello,

I want to create a bash script that upon invocation shows the time and date, lists
all logged-in users, and gives the system uptime. The script then saves this information to a logfile.

I have searched a lot to create the bash file and I'm unable to find the script. I'm newbie to linux trying to learn about bash scripting. can you explain in detail.

Sol:
Step 1: create a file using vi 'filename'     eg: vi bash_logindetails
Step 2: Paste this script in the file, save it and exit


#!/bin/bash
logfile=~/homework.txt
echo `date` > $logfile 
echo `who` >> $logfile
echo `uptime` >> $logfile
exit


Step 3: Then change it to executable mode chmod +x <filename> 
            eg: chmod +x bash_logindetails
Step 4: Then run the file using ./filename       eg: ./bash_logindetails
Step 5: That's it your  shell script has completed. 


To check the output of your script just cat the file in the same directory.


[root@vps ~]# cat homework.txt
Sat May 28 20:40:00 MSD 2011
root pts/0 2011-05-28 20:17 (wing432)
20:40:00 up 4 days, 6:14, 1 user, load average: 0.00, 0.00, 0.00





No comments: