nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Alphabetical file name and list logged in user script

This is a discussion on Alphabetical file name and list logged in user script within the Shell scripting forums, part of the Development/Scripting category; Hi, i couldnt figure out how to write this script. can any one help me ? please responce asap problem ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-12-2007, 05:43 PM
Junior Member
User
 
Join Date: Aug 2007
My distro: Debian
Posts: 6
Rep Power: 0
oronno is on a distinguished road
Thumbs down Alphabetical file name and list logged in user script

Hi, i couldnt figure out how to write this script. can any one help me ? please responce asap

problem 1: Write a shell script (/bin/sh) to write the names of the first (alphabetically) five files of the current directory to a new file called ‘zzz’.

problem 2: Write a shell script (/bin/sh) called 'loggedOn' which is passed two usernames as command line parameters. Using the ‘who’ command, the script should see whether both users are logged on and, if so, should indicate which of them is logged onto the system the most times. If only one or neither is logged on, an error message should be written.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-13-2007, 02:27 AM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Quote:
problem 1: Write a shell script (/bin/sh) to write the names of the first (alphabetically) five files of the current directory to a new file called ‘zzz’.
By default ls command sort out names under Linux:
Code:
#!/bin/sh
ls | head -5
Quote:
problem 2: Write a shell script (/bin/sh) called 'loggedOn' which is passed two usernames as command line parameters. Using the ‘who’ command, the script should see whether both users are logged on and, if so, should indicate which of them is logged onto the system the most times. If only one or neither is logged on, an error message should be written.
Script:
Code:
#!/bin/bash
USR1=$1
USR2=$2
u1times=0
u2times=0
if [ $# -ne 2 ]; then
 echo "Syntax: $0 user1 user2"
 exit 1
fi
who | egrep -w $USR1 > /dev/null
if [ $? -eq 0 ]; then
        u1times=$(last | egrep -w $USR1)
else
        echo "Error $USR1 not logged in"
        exit 1
fi

who | egrep -w $USR2 > /dev/null
if [ $? -eq 0 ]; then
        u2times=$(last | egrep -w $USR2)
else
        echo "Error $USR2 not logged in"
        exit 1
fi
if [ $u1times -gt $u2times ]; then
        echo "$USR1 logged most of the time"
else
        echo "$USR2 logged most of the time"
fi

Hope you are able to explain it to your tutor
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #3 (permalink)  
Old 08-13-2007, 07:38 AM
Junior Member
User
 
Join Date: Aug 2007
My distro: Debian
Posts: 6
Rep Power: 0
oronno is on a distinguished road
Default to rockdalinux

thanks for your help buddy. but the first problem u solved is not clear for me. because there is something i need to make, such as creat a new file and u didnt complete the whole thing . can you please give me full script for problem 1 ? thanks
Reply With Quote
  #4 (permalink)  
Old 08-16-2007, 12:40 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Code:
#!/bin/sh
ls | head -5 > zzz
there I fixed for you
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 08-16-2007, 06:08 PM
Junior Member
User
 
Join Date: Jul 2007
Location: Cedar Falls, IA
My distro: Fedora
Posts: 3
Rep Power: 0
A.L.E.X is on a distinguished road
Default

don't you guys think that

Code:
ls | egrep -v '^[0-9]' | head -5 > zzz
will be better solution ? I really don't know ... "alphabetically" confuse me
Reply With Quote
  #6 (permalink)  
Old 08-22-2007, 01:10 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Quote:
Originally Posted by A.L.E.X View Post
don't you guys think that

Code:
ls | egrep -v '^[0-9]' | head -5 > zzz
will be better solution ? I really don't know ... "alphabetically" confuse me
Yes alphabetically means ignore out digits .. I guess your solution is correct
__________________
May the force with you!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Getting the list of directory names into a file hiddenrider007 Shell scripting 2 04-09-2008 10:59 AM
AIX /etc/security/user like file for Linux ftengcheng Getting started tutorials 4 03-25-2008 02:28 AM
script for mailing to user the o/p of the script ricc Shell scripting 3 07-11-2007 05:59 AM
list every user's crontab script marinm Shell scripting 5 07-20-2005 12:24 AM
shell script for finding users not logged on for last 10 day ganes Shell scripting 4 07-06-2005 12:01 PM


All times are GMT +5.5. The time now is 05:35 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36